Skip to content

Commit

Permalink
Optimize stackalloc in FirstBy, GatherBy
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobris committed Dec 27, 2024
1 parent 3483e8c commit 7399f3f
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions BTDB/ODBLayer/RelationDBManipulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,13 +1032,13 @@ public IEnumerator<TItem> FindByPrimaryKeyPrefix<TItem>(in ReadOnlySpan<byte> ke
public TItem FirstByPrimaryKey<TItem>(int loaderIndex, ConstraintInfo[] constraints, ICollection<TItem> target,
IOrderer[]? orderers, bool hasOrDefault) where TItem : class
{
MemWriter keyBytes = new();
var keyBytes = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[4096]);
keyBytes.WriteBlock(_relationInfo.Prefix);

var relationVersionInfo = _relationInfo.ClientRelationVersionInfo;
var primaryKeyFields = relationVersionInfo.PrimaryKeyFields.Span;

Span<byte> buffer = stackalloc byte[1024];
Span<byte> buffer = stackalloc byte[4096];
var writer = MemWriter.CreateFromStackAllocatedSpan(buffer);
if (orderers == null || orderers.Length == 0)
{
Expand Down Expand Up @@ -1085,12 +1085,11 @@ public TItem FirstByPrimaryKey<TItem>(int loaderIndex, ConstraintInfo[] constrai
public TItem FirstBySecondaryKey<TItem>(int loaderIndex, ConstraintInfo[] constraints, uint secondaryKeyIndex,
IOrderer[]? orderers, bool hasOrDefault) where TItem : class
{
var keyBytes = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[16]);
var keyBytes = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[4096]);
keyBytes.WriteBlock(_relationInfo.PrefixSecondary);
var remappedSecondaryKeyIndex = RemapPrimeSK(secondaryKeyIndex);
keyBytes.WriteUInt8((byte)remappedSecondaryKeyIndex);
Span<byte> keyBuffer = stackalloc byte[4096];
var writer = MemWriter.CreateFromStackAllocatedSpan(keyBuffer);
var writer = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[4096]);
if (orderers == null || orderers.Length == 0)
{
using var enumerator = new RelationConstraintSecondaryKeyEnumerator<TItem>(_transaction, _relationInfo,
Expand Down Expand Up @@ -1258,7 +1257,7 @@ public ulong GatherByPrimaryKey<TItem>(int loaderIndex, ConstraintInfo[] constra
skip = 0;
}

var writer = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[1024]);
var writer = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[4096]);
var relationVersionInfo = _relationInfo.ClientRelationVersionInfo;
var primaryKeyFields = relationVersionInfo.PrimaryKeyFields.Span;

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

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

var writer = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[1024]);
var writer = MemWriter.CreateFromStackAllocatedSpan(stackalloc byte[4096]);
if (orderers == null || orderers.Length == 0)
{
using var enumerator = new RelationConstraintSecondaryKeyEnumerator<TItem>(_transaction, _relationInfo,
Expand Down

0 comments on commit 7399f3f

Please sign in to comment.