Skip to content

Commit

Permalink
Speed up scan (microsoft#916)
Browse files Browse the repository at this point in the history
* Speed up scan

Avoid buffer pool allocation per GetNext and reuse memory to speed up scan

* nit

* avoid two counters for DBScan.

* update version

* Revert "update version"

This reverts commit 718059a.

* update version
  • Loading branch information
badrishc authored Jan 15, 2025
1 parent b5997ae commit 373c4c3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Version.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<!-- Versioning property for builds and packages -->
<PropertyGroup>
<VersionPrefix>1.0.50</VersionPrefix>
<VersionPrefix>1.0.51</VersionPrefix>
</PropertyGroup>
</Project>
12 changes: 4 additions & 8 deletions libs/server/Storage/Session/Common/ArrayKeyIterationFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,9 @@ internal sealed class MainStoreGetDBSize : IScanIteratorFunctions<SpanByte, Span

public bool SingleReader(ref SpanByte key, ref SpanByte value, RecordMetadata recordMetadata, long numberOfRecords, out CursorRecordResult cursorRecordResult)
{
if (value.MetadataSize != 0 && MainSessionFunctions.CheckExpiry(ref value))
cursorRecordResult = CursorRecordResult.Skip;
else
cursorRecordResult = CursorRecordResult.Skip;
if (value.MetadataSize == 0 || !MainSessionFunctions.CheckExpiry(ref value))
{
cursorRecordResult = CursorRecordResult.Accept;
++info.count;
}
return true;
Expand All @@ -340,11 +338,9 @@ internal sealed class ObjectStoreGetDBSize : IScanIteratorFunctions<byte[], IGar

public bool SingleReader(ref byte[] key, ref IGarnetObject value, RecordMetadata recordMetadata, long numberOfRecords, out CursorRecordResult cursorRecordResult)
{
if (value.Expiration > 0 && ObjectSessionFunctions.CheckExpiry(value))
cursorRecordResult = CursorRecordResult.Skip;
else
cursorRecordResult = CursorRecordResult.Skip;
if (value.Expiration == 0 || !ObjectSessionFunctions.CheckExpiry(value))
{
cursorRecordResult = CursorRecordResult.Accept;
++info.count;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,28 @@ public unsafe bool GetNext(out RecordInfo recordInfo)
// We will return control to the caller, which means releasing epoch protection, and we don't want the caller to lock.
// Copy the entire record into bufferPool memory, so we do not have a ref to log data outside epoch protection.
// Lock to ensure no value tearing while copying to temp storage.
memory?.Return();
memory = null;
if (currentAddress >= headAddress || forceInMemory)
{
OperationStackContext<SpanByte, SpanByte, TStoreFunctions, SpanByteAllocator<TStoreFunctions>> stackCtx = default;
try
{
if (memory == null)
{
memory = hlog.bufferPool.Get(recordSize);
}
else
{
if (memory.AlignedTotalCapacity < recordSize)
{
memory.Return();
memory = hlog.bufferPool.Get(recordSize);
}
}

// GetKey() should work but for safety and consistency with other allocators use physicalAddress.
if (currentAddress >= headAddress && store is not null)
store.LockForScan(ref stackCtx, ref hlog._wrapper.GetKey(physicalAddress));

memory = hlog.bufferPool.Get(recordSize);
unsafe
{
Buffer.MemoryCopy((byte*)currentPhysicalAddress, memory.aligned_pointer, recordSize, recordSize);
Expand Down
5 changes: 5 additions & 0 deletions libs/storage/Tsavorite/cs/src/core/Utilities/BufferPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ public void Return()
pool?.Return(this);
}

/// <summary>
/// Get the total aligned memory capacity of the buffer
/// </summary>
public int AlignedTotalCapacity => buffer.Length - offset;

/// <summary>
/// Get valid pointer
/// </summary>
Expand Down

0 comments on commit 373c4c3

Please sign in to comment.