Skip to content

Commit 7399f3f

Browse files
committed
Optimize stackalloc in FirstBy, GatherBy
1 parent 3483e8c commit 7399f3f

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

BTDB/ODBLayer/RelationDBManipulator.cs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1032,13 +1032,13 @@ public IEnumerator<TItem> FindByPrimaryKeyPrefix<TItem>(in ReadOnlySpan<byte> ke
10321032
public TItem FirstByPrimaryKey<TItem>(int loaderIndex, ConstraintInfo[] constraints, ICollection<TItem> target,
10331033
IOrderer[]? orderers, bool hasOrDefault) where TItem : class
10341034
{
1035-
MemWriter keyBytes = new();
1035+
var keyBytes = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[4096]);
10361036
keyBytes.WriteBlock(_relationInfo.Prefix);
10371037

10381038
var relationVersionInfo = _relationInfo.ClientRelationVersionInfo;
10391039
var primaryKeyFields = relationVersionInfo.PrimaryKeyFields.Span;
10401040

1041-
Span<byte> buffer = stackalloc byte[1024];
1041+
Span<byte> buffer = stackalloc byte[4096];
10421042
var writer = MemWriter.CreateFromStackAllocatedSpan(buffer);
10431043
if (orderers == null || orderers.Length == 0)
10441044
{
@@ -1085,12 +1085,11 @@ public TItem FirstByPrimaryKey<TItem>(int loaderIndex, ConstraintInfo[] constrai
10851085
public TItem FirstBySecondaryKey<TItem>(int loaderIndex, ConstraintInfo[] constraints, uint secondaryKeyIndex,
10861086
IOrderer[]? orderers, bool hasOrDefault) where TItem : class
10871087
{
1088-
var keyBytes = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[16]);
1088+
var keyBytes = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[4096]);
10891089
keyBytes.WriteBlock(_relationInfo.PrefixSecondary);
10901090
var remappedSecondaryKeyIndex = RemapPrimeSK(secondaryKeyIndex);
10911091
keyBytes.WriteUInt8((byte)remappedSecondaryKeyIndex);
1092-
Span<byte> keyBuffer = stackalloc byte[4096];
1093-
var writer = MemWriter.CreateFromStackAllocatedSpan(keyBuffer);
1092+
var writer = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[4096]);
10941093
if (orderers == null || orderers.Length == 0)
10951094
{
10961095
using var enumerator = new RelationConstraintSecondaryKeyEnumerator<TItem>(_transaction, _relationInfo,
@@ -1258,7 +1257,7 @@ public ulong GatherByPrimaryKey<TItem>(int loaderIndex, ConstraintInfo[] constra
12581257
skip = 0;
12591258
}
12601259

1261-
var writer = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[1024]);
1260+
var writer = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[4096]);
12621261
var relationVersionInfo = _relationInfo.ClientRelationVersionInfo;
12631262
var primaryKeyFields = relationVersionInfo.PrimaryKeyFields.Span;
12641263

@@ -1363,7 +1362,7 @@ public ulong GatherBySecondaryKey<TItem>(int loaderIndex, ConstraintInfo[] const
13631362

13641363
var fastIteration = IsFastIterable(constraints, constraints.Length);
13651364

1366-
var writer = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[1024]);
1365+
var writer = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[4096]);
13671366
if (orderers == null || orderers.Length == 0)
13681367
{
13691368
using var enumerator = new RelationConstraintSecondaryKeyEnumerator<TItem>(_transaction, _relationInfo,

0 commit comments

Comments
 (0)