Skip to content

Commit 373c4c3

Browse files
authored
Speed up scan (#916)
* 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
1 parent b5997ae commit 373c4c3

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

Version.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<!-- Versioning property for builds and packages -->
33
<PropertyGroup>
4-
<VersionPrefix>1.0.50</VersionPrefix>
4+
<VersionPrefix>1.0.51</VersionPrefix>
55
</PropertyGroup>
66
</Project>

libs/server/Storage/Session/Common/ArrayKeyIterationFunctions.cs

+4-8
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,9 @@ internal sealed class MainStoreGetDBSize : IScanIteratorFunctions<SpanByte, Span
312312

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

341339
public bool SingleReader(ref byte[] key, ref IGarnetObject value, RecordMetadata recordMetadata, long numberOfRecords, out CursorRecordResult cursorRecordResult)
342340
{
343-
if (value.Expiration > 0 && ObjectSessionFunctions.CheckExpiry(value))
344-
cursorRecordResult = CursorRecordResult.Skip;
345-
else
341+
cursorRecordResult = CursorRecordResult.Skip;
342+
if (value.Expiration == 0 || !ObjectSessionFunctions.CheckExpiry(value))
346343
{
347-
cursorRecordResult = CursorRecordResult.Accept;
348344
++info.count;
349345
}
350346
return true;

libs/storage/Tsavorite/cs/src/core/Allocator/SpanByteScanIterator.cs

+13-3
Original file line numberDiff line numberDiff line change
@@ -182,18 +182,28 @@ public unsafe bool GetNext(out RecordInfo recordInfo)
182182
// We will return control to the caller, which means releasing epoch protection, and we don't want the caller to lock.
183183
// Copy the entire record into bufferPool memory, so we do not have a ref to log data outside epoch protection.
184184
// Lock to ensure no value tearing while copying to temp storage.
185-
memory?.Return();
186-
memory = null;
187185
if (currentAddress >= headAddress || forceInMemory)
188186
{
189187
OperationStackContext<SpanByte, SpanByte, TStoreFunctions, SpanByteAllocator<TStoreFunctions>> stackCtx = default;
190188
try
191189
{
190+
if (memory == null)
191+
{
192+
memory = hlog.bufferPool.Get(recordSize);
193+
}
194+
else
195+
{
196+
if (memory.AlignedTotalCapacity < recordSize)
197+
{
198+
memory.Return();
199+
memory = hlog.bufferPool.Get(recordSize);
200+
}
201+
}
202+
192203
// GetKey() should work but for safety and consistency with other allocators use physicalAddress.
193204
if (currentAddress >= headAddress && store is not null)
194205
store.LockForScan(ref stackCtx, ref hlog._wrapper.GetKey(physicalAddress));
195206

196-
memory = hlog.bufferPool.Get(recordSize);
197207
unsafe
198208
{
199209
Buffer.MemoryCopy((byte*)currentPhysicalAddress, memory.aligned_pointer, recordSize, recordSize);

libs/storage/Tsavorite/cs/src/core/Utilities/BufferPool.cs

+5
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ public void Return()
136136
pool?.Return(this);
137137
}
138138

139+
/// <summary>
140+
/// Get the total aligned memory capacity of the buffer
141+
/// </summary>
142+
public int AlignedTotalCapacity => buffer.Length - offset;
143+
139144
/// <summary>
140145
/// Get valid pointer
141146
/// </summary>

0 commit comments

Comments
 (0)