Skip to content

Commit

Permalink
New SpanByteLruCache and BonBuilder speedup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobris committed Mar 16, 2024
1 parent 9b0394c commit 2552c85
Show file tree
Hide file tree
Showing 6 changed files with 628 additions and 14 deletions.
32 changes: 21 additions & 11 deletions BTDB/Bon/Bon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,9 @@ enum State
StructList<ulong> _objKeys = new();
MemWriter _topData;
uint _items = 0;
readonly LruCache<string, ulong> _strCache = new(512);
readonly LruCache<(bool IsClass, StructList<ulong> ObjKeys), ulong> _objKeysCache = new(64);
LruCache<string, ulong>? _strCache = null;
SpanByteLruCache<ulong>? _u8Cache = null;
LruCache<(bool IsClass, StructList<ulong> ObjKeys), ulong>? _objKeysCache = null;
HashSet<string>? _objKeysSet;

public BonBuilder(in MemWriter memWriter)
Expand Down Expand Up @@ -640,16 +641,16 @@ public void FinishObject()
rootData = ref _stack[0].Item2;
}

if (!_objKeysCache.TryGetValue((false, objKeys), out var posKeys))
_objKeysCache ??= new();
ref var posKeys = ref _objKeysCache.GetOrAddValueRef((false, objKeys), out var added);
if (added)
{
posKeys = (ulong)rootData.GetCurrentPosition();
rootData.WriteVUInt32(items);
foreach (var keyOfs in objKeys)
{
rootData.WriteVUInt64(keyOfs);
}

_objKeysCache[(false, objKeys)] = posKeys;
}

var pos = rootData.GetCurrentPosition();
Expand Down Expand Up @@ -695,16 +696,16 @@ public void FinishClass()
rootData = ref _stack[0].Item2;
}

if (!_objKeysCache.TryGetValue((true, objKeys), out var posKeys))
_objKeysCache ??= new();
ref var posKeys = ref _objKeysCache.GetOrAddValueRef((true, objKeys), out var added);
if (added)
{
posKeys = (ulong)rootData.GetCurrentPosition();
rootData.WriteVUInt32(items);
foreach (var keyOfs in objKeys)
{
rootData.WriteVUInt64(keyOfs);
}

_objKeysCache[(true, objKeys)] = posKeys;
}

var pos = rootData.GetCurrentPosition();
Expand Down Expand Up @@ -846,7 +847,9 @@ void ThrowWrongState()

ulong WriteDedupString(string value)
{
if (_strCache.TryGetValue(value, out var pos))
_strCache ??= new(512);
ref var pos = ref _strCache.GetOrAddValueRef(value, out var added);
if (!added)
{
return pos;
}
Expand All @@ -859,21 +862,28 @@ ulong WriteDedupString(string value)

pos = (ulong)writer.GetCurrentPosition();
writer.WriteStringInUtf8(value);
_strCache[value] = pos;
return pos;
}

ulong WriteDedupString(ReadOnlySpan<byte> value)
{
_u8Cache ??= new(512);
ref var pos = ref _u8Cache.GetOrAddValueRef(value, out var added);
if (!added)
{
return pos;
}

ref var writer = ref _topData;
if (_stack.Count > 0)
{
writer = ref _stack[0].Data;
}

var pos = (ulong)writer.GetCurrentPosition();
pos = (ulong)writer.GetCurrentPosition();
writer.WriteVUInt32((uint)value.Length);
writer.WriteBlock(value);
_u8Cache[value] = pos;
return pos;
}

Expand Down
16 changes: 14 additions & 2 deletions BTDB/Collections/LruCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,20 @@ public bool Remove(TKey key)
return false;
}

public ref TValue this[TKey key] => ref GetOrAddValueRef(key);
public void Clear()
{
Array.Clear(_buckets);
Array.Clear(_entries, 0, _count);
_count = 0;
_freeList = -1;
UsageHead = -1;
UsageTail = -1;
}

public ref TValue this[TKey key] => ref GetOrAddValueRef(key, out _);

// Not safe for concurrent _reads_ (at least, if either of them add)
public ref TValue GetOrAddValueRef(TKey key)
public ref TValue GetOrAddValueRef(TKey key, out bool added)
{
if (key == null) HashHelpers.ThrowKeyArgumentNullException();
var entries = _entries;
Expand Down Expand Up @@ -216,6 +226,7 @@ public bool Remove(TKey key)
UsageHead = i;
}

added = false;
return ref entries[i].Value;
}

Expand All @@ -229,6 +240,7 @@ public bool Remove(TKey key)
collisionCount++;
}

added = true;
return ref AddKey(key, bucketIndex, hash);
}

Expand Down

0 comments on commit 2552c85

Please sign in to comment.