diff --git a/src/Instance.cs b/src/Instance.cs index 84631b4..ecb98ff 100644 --- a/src/Instance.cs +++ b/src/Instance.cs @@ -516,6 +516,12 @@ public Instance(Store store, Module module, params object[] imports) /// Returns the function if a function of that name was exported or null if not. public Function? GetFunction(string name) { + lock (_functionCache) + { + if (_functionCache.TryGetValue(name, out var func)) + return func; + } + var context = _store.Context; if (!TryGetExtern(context, name, out var ext) || ext.kind != ExternKind.Func) { @@ -524,7 +530,13 @@ public Instance(Store store, Module module, params object[] imports) GC.KeepAlive(_store); - return _store.GetCachedExtern(ext.of.func); + var result = _store.GetCachedExtern(ext.of.func); + lock (_functionCache) + { + _functionCache[name] = result; + } + + return result; } /// @@ -552,6 +564,12 @@ public Instance(Store store, Module module, params object[] imports) /// Returns the memory if a memory of that name was exported or null if not. public Memory? GetMemory(string name) { + lock (_memoryCache) + { + if (_memoryCache.TryGetValue(name, out var memory)) + return memory; + } + if (!TryGetExtern(_store.Context, name, out var ext) || ext.kind != ExternKind.Memory) { return null; @@ -559,7 +577,13 @@ public Instance(Store store, Module module, params object[] imports) GC.KeepAlive(_store); - return _store.GetCachedExtern(ext.of.memory); + var result = _store.GetCachedExtern(ext.of.memory); + lock (_memoryCache) + { + _memoryCache[name] = result; + } + + return result; } /// @@ -727,5 +751,8 @@ private static class Native private readonly Store _store; internal readonly ExternInstance instance; + + private readonly Dictionary _functionCache = new(); + private readonly Dictionary _memoryCache = new(); } }