Skip to content

Commit a55218a

Browse files
authored
Revert the changes to the cache of externs in Store (#323)
* Revert "Use separate dictionaries for caching `Function`, `Memory`, and `Global` objects in the `Store`, which avoids having to explicitly accessing the `__private` field (because the whole struct is now compared)." This reverts commit f4e6e57. * Follow-Up: Add comment. * Follow-Up: Rename tuple element to reflect the struct field name.
1 parent 5e1b0e8 commit a55218a

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/Externs.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,35 @@ namespace Wasmtime
77
internal struct ExternFunc
88
{
99
public ulong store;
10-
private nuint __private;
10+
public nuint __private;
1111
}
1212

1313
[StructLayout(LayoutKind.Sequential)]
1414
internal struct ExternTable
1515
{
1616
public ulong store;
17-
private nuint __private;
17+
public nuint __private;
1818
}
1919

2020
[StructLayout(LayoutKind.Sequential)]
2121
internal struct ExternMemory
2222
{
2323
public ulong store;
24-
private nuint __private;
24+
public nuint __private;
2525
}
2626

2727
[StructLayout(LayoutKind.Sequential)]
2828
internal struct ExternInstance
2929
{
3030
public ulong store;
31-
private nuint __private;
31+
public nuint __private;
3232
}
3333

3434
[StructLayout(LayoutKind.Sequential)]
3535
internal struct ExternGlobal
3636
{
3737
public ulong store;
38-
private nuint __private;
38+
public nuint __private;
3939
}
4040

4141
internal enum ExternKind : byte

src/Store.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -340,41 +340,51 @@ private static class Native
340340

341341
private static readonly Native.Finalizer Finalizer = (p) => GCHandle.FromIntPtr(p).Free();
342342

343-
private readonly ConcurrentDictionary<ExternFunc, Function> _externFuncCache = new();
344-
private readonly ConcurrentDictionary<ExternMemory, Memory> _externMemoryCache = new();
345-
private readonly ConcurrentDictionary<ExternGlobal, Global> _externGlobalCache = new();
343+
private readonly ConcurrentDictionary<(ExternKind kind, ulong store, nuint __private), object> _externCache = new();
346344

347345
internal Function GetCachedExtern(ExternFunc @extern)
348346
{
349-
if (!_externFuncCache.TryGetValue(@extern, out var func))
347+
// We use a `ValueTuple` as key, consisting of the extern type and the both
348+
// struct fields, which works since all `Extern...` structs have the same
349+
// fields.
350+
// Even though the second field is named "__private", it should be Ok to
351+
// access it since we won't interpret the value in any way, but just use it
352+
// to compare it to other values.
353+
var key = (ExternKind.Func, @extern.store, @extern.__private);
354+
355+
if (!_externCache.TryGetValue(key, out var func))
350356
{
351357
func = new Function(this, @extern);
352-
func = _externFuncCache.GetOrAdd(@extern, func);
358+
func = _externCache.GetOrAdd(key, func);
353359
}
354360

355-
return func;
361+
return (Function)func;
356362
}
357363

358364
internal Memory GetCachedExtern(ExternMemory @extern)
359365
{
360-
if (!_externMemoryCache.TryGetValue(@extern, out var mem))
366+
var key = (ExternKind.Memory, @extern.store, @extern.__private);
367+
368+
if (!_externCache.TryGetValue(key, out var mem))
361369
{
362370
mem = new Memory(this, @extern);
363-
mem = _externMemoryCache.GetOrAdd(@extern, mem);
371+
mem = _externCache.GetOrAdd(key, mem);
364372
}
365373

366-
return mem;
374+
return (Memory)mem;
367375
}
368376

369377
internal Global GetCachedExtern(ExternGlobal @extern)
370378
{
371-
if (!_externGlobalCache.TryGetValue(@extern, out var global))
379+
var key = (ExternKind.Global, @extern.store, @extern.__private);
380+
381+
if (!_externCache.TryGetValue(key, out var global))
372382
{
373383
global = new Global(this, @extern);
374-
global = _externGlobalCache.GetOrAdd(@extern, global);
384+
global = _externCache.GetOrAdd(key, global);
375385
}
376386

377-
return global;
387+
return (Global)global;
378388
}
379389
}
380390
}

0 commit comments

Comments
 (0)