Skip to content

Commit 807e2b3

Browse files
committed
Migrating to local implementation of Usafe.As; Using [ThreadStatic] instead of ThreadLocal due to lower overhead
1 parent b64e73e commit 807e2b3

File tree

6 files changed

+58
-36
lines changed

6 files changed

+58
-36
lines changed

src/vm/coro.cs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,18 @@ public virtual void Cleanup(VM.Frame frm, VM.ExecState exec) {}
3030

3131
public class CoroutinePool
3232
{
33-
//TODO: add debug inspection for concrete types
33+
//TODO: add debug inspection for concrete types?
3434
static class PoolHolder<T> where T : Coroutine
3535
{
36-
//TODO: alternative implemenation, check if it's simpler and faster?
37-
//[ThreadStatic]
38-
//static public VM.Pool<Coroutine> _pool;
39-
//static public VM.Pool<Coroutine> pool {
40-
// get {
41-
// if(_pool == null)
42-
// _pool = new VM.Pool<Coroutine>();
43-
// return _pool;
44-
// }
45-
//}
46-
47-
public static System.Threading.ThreadLocal<Pool<Coroutine>> pool =
48-
new System.Threading.ThreadLocal<Pool<Coroutine>>(() =>
49-
{
50-
return new Pool<Coroutine>();
51-
});
36+
[ThreadStatic]
37+
static public Pool<Coroutine> _pool;
38+
static public Pool<Coroutine> pool {
39+
get {
40+
if(_pool == null)
41+
_pool = new Pool<Coroutine>();
42+
return _pool;
43+
}
44+
}
5245
}
5346

5447
internal int hits;
@@ -73,12 +66,12 @@ public int NewCount {
7366
}
7467

7568
#if BHL_TEST
76-
public HashSet<VM.Pool<Coroutine>> pools_tracker = new HashSet<VM.Pool<Coroutine>>();
69+
public HashSet<Pool<Coroutine>> pools_tracker = new HashSet<Pool<Coroutine>>();
7770
#endif
7871

7972
static public T New<T>(VM vm) where T : Coroutine, new()
8073
{
81-
var pool = PoolHolder<T>.pool.Value;
74+
var pool = PoolHolder<T>.pool;
8275

8376
Coroutine coro = null;
8477
if(pool.stack.Count == 0)

src/vm/util/extensions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,27 @@ public static void SetFuncAttrib(this FuncSignatureAttrib attrib, ref byte other
9595
// these bits in the target value and 'or' the bits we want to set
9696
other = (byte)((other & (byte)~FuncSignatureAttrib.FuncAttribMask) | (byte)attrib);
9797
}
98+
99+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
100+
public static unsafe int SizeOf<T>() where T : unmanaged
101+
{
102+
return sizeof(T);
103+
}
104+
105+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
106+
public static unsafe ref T UsafeAsRef<T>(void* source) where T : unmanaged
107+
{
108+
return ref *(T*)source;
109+
}
110+
111+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
112+
public static unsafe ref TTo UnsafeAs<TFrom, TTo>(ref TFrom source) where TTo : unmanaged where TFrom : unmanaged
113+
{
114+
fixed(void* p = &source)
115+
{
116+
return ref UsafeAsRef<TTo>(p);
117+
}
118+
}
98119
}
99120

100121
}

src/vm/util/refc_list.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,21 @@ public class RefcList<T> : List<T>, IValRefcounted, IDisposable
1414
public int refs => _refs;
1515

1616
static class PoolHolder<T1>
17-
{
18-
public static System.Threading.ThreadLocal<Pool<RefcList<T1>>> pool =
19-
new System.Threading.ThreadLocal<Pool<RefcList<T1>>>(() =>
20-
{
21-
return new Pool<RefcList<T1>>();
22-
});
17+
{
18+
[ThreadStatic]
19+
static public Pool<RefcList<T1>> _pool;
20+
public static Pool<RefcList<T1>> pool {
21+
get {
22+
if(_pool == null)
23+
_pool = new Pool<RefcList<T1>>();
24+
return _pool;
25+
}
26+
}
2327
}
2428

2529
static public RefcList<T> New()
2630
{
27-
var pool = PoolHolder<T>.pool.Value;
31+
var pool = PoolHolder<T>.pool;
2832

2933
RefcList<T> list = null;
3034
if(pool.stack.Count == 0)

src/vm/util/val_list_adapter.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,20 @@ public class ValList<T> : IList<T>, IValRefcounted, IDisposable
2626

2727
static class PoolHolder<T1>
2828
{
29-
public static System.Threading.ThreadLocal<Pool<ValList<T1>>> pool =
30-
new System.Threading.ThreadLocal<Pool<ValList<T1>>>(() =>
31-
{
32-
return new Pool<ValList<T1>>();
33-
});
29+
[ThreadStatic]
30+
static public Pool<ValList<T1>> _pool;
31+
public static Pool<ValList<T1>> pool {
32+
get {
33+
if(_pool == null)
34+
_pool = new Pool<ValList<T1>>();
35+
return _pool;
36+
}
37+
}
3438
}
3539

3640
static public ValList<T> New(ValList lst, Func<Val, T> val2native)
3741
{
38-
var pool = PoolHolder<T>.pool.Value;
42+
var pool = PoolHolder<T>.pool;
3943

4044
ValList<T> vls = null;
4145
if(pool.stack.Count == 0)

src/vm/val.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,11 @@ public void SetBlob<T>(ref T val, IType type) where T : unmanaged
409409
{
410410
Reset();
411411

412-
int size = Marshal.SizeOf<T>();
412+
int size = Extensions.SizeOf<T>();
413413

414414
var data = ArrayPool<byte>.Shared.Rent(size);
415415

416-
Unsafe.As<byte, T>(ref data[0]) = val;
416+
Extensions.UnsafeAs<byte, T>(ref data[0]) = val;
417417

418418
this.type = type;
419419
_blob_size = size;
@@ -430,7 +430,7 @@ public void SetBlob<T>(T val, IType type) where T : unmanaged
430430
public ref T GetBlob<T>() where T : unmanaged
431431
{
432432
byte[] data = (byte[])_obj;
433-
return ref Unsafe.As<byte, T>(ref data[0]);
433+
return ref Extensions.UnsafeAs<byte, T>(ref data[0]);
434434
}
435435

436436
[MethodImpl(MethodImplOptions.AggressiveInlining)]

src/vm/version.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace bhl {
22

33
public static class Version
44
{
5-
public static string Name = "v2.0.0-beta192";
5+
public static string Name = "v2.0.0-beta193";
66
}
77

88
}

0 commit comments

Comments
 (0)