Skip to content

Commit

Permalink
Fix some corner cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobris committed Mar 16, 2024
1 parent 2552c85 commit ed9929b
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions BTDB/Collections/SpanByteLruCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ public void Clear()
_buckets[oldbucketIndex] = candidate.Next + 1;
}

entries[entryIndex] = default;
_freeBytes += candidate.Length;
candidate = default;
_count--;
break;
}
Expand All @@ -338,9 +339,6 @@ public void Clear()
}

ref var entry = ref entries[entryIndex];
entry.Hash = hash;
entry.Length = key.Length;
entry.Offset = _usedBytes;
if (_bytes.Length < _usedBytes + key.Length)
{
if (_freeBytes >= _usedBytes >> 1 && _usedBytes - _freeBytes + key.Length <= _bytes.Length)
Expand All @@ -350,7 +348,7 @@ public void Clear()
_usedBytes = 0;
_freeBytes = 0;
var newBytes = new byte[_bytes.Length];
for (var i = 0; i < _count; i++)
for (var i = 0; i <= _count; i++)
{
ref var e = ref entries[i];
if (e.Next < -1)
Expand All @@ -370,9 +368,11 @@ public void Clear()
}
}

entry.Hash = hash;
entry.Length = key.Length;
entry.Offset = _usedBytes;
key.CopyTo(_bytes.AsSpan(_usedBytes));
_usedBytes += key.Length;

entry.Next = _buckets[bucketIndex] - 1;
entry.UsagePrev = -1;
entry.UsageNext = UsageHead;
Expand Down

0 comments on commit ed9929b

Please sign in to comment.