Skip to content

Commit ed9929b

Browse files
committed
Fix some corner cases.
1 parent 2552c85 commit ed9929b

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

BTDB/Collections/SpanByteLruCache.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ ref TValue AddKey(in ReadOnlySpan<byte> key, int bucketIndex, int hash)
317317
_buckets[oldbucketIndex] = candidate.Next + 1;
318318
}
319319

320-
entries[entryIndex] = default;
320+
_freeBytes += candidate.Length;
321+
candidate = default;
321322
_count--;
322323
break;
323324
}
@@ -338,9 +339,6 @@ ref TValue AddKey(in ReadOnlySpan<byte> key, int bucketIndex, int hash)
338339
}
339340

340341
ref var entry = ref entries[entryIndex];
341-
entry.Hash = hash;
342-
entry.Length = key.Length;
343-
entry.Offset = _usedBytes;
344342
if (_bytes.Length < _usedBytes + key.Length)
345343
{
346344
if (_freeBytes >= _usedBytes >> 1 && _usedBytes - _freeBytes + key.Length <= _bytes.Length)
@@ -350,7 +348,7 @@ ref TValue AddKey(in ReadOnlySpan<byte> key, int bucketIndex, int hash)
350348
_usedBytes = 0;
351349
_freeBytes = 0;
352350
var newBytes = new byte[_bytes.Length];
353-
for (var i = 0; i < _count; i++)
351+
for (var i = 0; i <= _count; i++)
354352
{
355353
ref var e = ref entries[i];
356354
if (e.Next < -1)
@@ -370,9 +368,11 @@ ref TValue AddKey(in ReadOnlySpan<byte> key, int bucketIndex, int hash)
370368
}
371369
}
372370

371+
entry.Hash = hash;
372+
entry.Length = key.Length;
373+
entry.Offset = _usedBytes;
373374
key.CopyTo(_bytes.AsSpan(_usedBytes));
374375
_usedBytes += key.Length;
375-
376376
entry.Next = _buckets[bucketIndex] - 1;
377377
entry.UsagePrev = -1;
378378
entry.UsageNext = UsageHead;

0 commit comments

Comments
 (0)