Skip to content

Commit

Permalink
Experimenting with blobs
Browse files Browse the repository at this point in the history
  • Loading branch information
pachanga committed Nov 21, 2024
1 parent 9947671 commit 9bc8523
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/vm/util/blob.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Buffers;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

Expand Down Expand Up @@ -28,6 +27,9 @@ static class PoolHolder<T1> where T1 : unmanaged
});
}

internal Blob()
{}

static public Blob<T> New(ref T val)
{
var pool = PoolHolder<T>.pool.Value;
Expand All @@ -37,20 +39,17 @@ static public Blob<T> New(ref T val)
{
++pool.miss;
blob = new Blob<T>();
blob.data = new byte[Size];
}
else
{
++pool.hits;
blob = pool.stack.Pop();
}

var data = ArrayPool<byte>.Shared.Rent(Size);

ref var valRef = ref Unsafe.As<byte, T>(ref data[0]);
valRef = val;
Unsafe.As<byte, T>(ref blob.data[0]) = val;

blob._refs = 1;
blob.data = data;
blob.pool = pool;

return blob;
Expand Down Expand Up @@ -83,7 +82,6 @@ static void Del(Blob<T> blob)
throw new Exception("Freeing invalid object, refs " + blob._refs);

blob._refs = -1;
ArrayPool<byte>.Shared.Return(blob.data);

blob.pool.stack.Push(blob);
}
Expand Down

0 comments on commit 9bc8523

Please sign in to comment.