@@ -516,6 +516,12 @@ public Instance(Store store, Module module, params object[] imports)
516516 /// <returns>Returns the function if a function of that name was exported or null if not.</returns>
517517 public Function ? GetFunction ( string name )
518518 {
519+ lock ( _functionCache )
520+ {
521+ if ( _functionCache . TryGetValue ( name , out var func ) )
522+ return func ;
523+ }
524+
519525 var context = _store . Context ;
520526 if ( ! TryGetExtern ( context , name , out var ext ) || ext . kind != ExternKind . Func )
521527 {
@@ -524,7 +530,13 @@ public Instance(Store store, Module module, params object[] imports)
524530
525531 GC . KeepAlive ( _store ) ;
526532
527- return _store . GetCachedExtern ( ext . of . func ) ;
533+ var result = _store . GetCachedExtern ( ext . of . func ) ;
534+ lock ( _functionCache )
535+ {
536+ _functionCache [ name ] = result ;
537+ }
538+
539+ return result ;
528540 }
529541
530542 /// <summary>
@@ -552,14 +564,26 @@ public Instance(Store store, Module module, params object[] imports)
552564 /// <returns>Returns the memory if a memory of that name was exported or null if not.</returns>
553565 public Memory ? GetMemory ( string name )
554566 {
567+ lock ( _memoryCache )
568+ {
569+ if ( _memoryCache . TryGetValue ( name , out var memory ) )
570+ return memory ;
571+ }
572+
555573 if ( ! TryGetExtern ( _store . Context , name , out var ext ) || ext . kind != ExternKind . Memory )
556574 {
557575 return null ;
558576 }
559577
560578 GC . KeepAlive ( _store ) ;
561579
562- return _store . GetCachedExtern ( ext . of . memory ) ;
580+ var result = _store . GetCachedExtern ( ext . of . memory ) ;
581+ lock ( _memoryCache )
582+ {
583+ _memoryCache [ name ] = result ;
584+ }
585+
586+ return result ;
563587 }
564588
565589 /// <summary>
@@ -715,5 +739,8 @@ private static class Native
715739
716740 private readonly Store _store ;
717741 internal readonly ExternInstance instance ;
742+
743+ private readonly Dictionary < string , Function ? > _functionCache = new ( ) ;
744+ private readonly Dictionary < string , Memory ? > _memoryCache = new ( ) ;
718745 }
719746}
0 commit comments