@@ -516,6 +516,12 @@ public Instance(Store store, Module module, params object[] imports)
516
516
/// <returns>Returns the function if a function of that name was exported or null if not.</returns>
517
517
public Function ? GetFunction ( string name )
518
518
{
519
+ lock ( _functionCache )
520
+ {
521
+ if ( _functionCache . TryGetValue ( name , out var func ) )
522
+ return func ;
523
+ }
524
+
519
525
var context = _store . Context ;
520
526
if ( ! TryGetExtern ( context , name , out var ext ) || ext . kind != ExternKind . Func )
521
527
{
@@ -524,7 +530,13 @@ public Instance(Store store, Module module, params object[] imports)
524
530
525
531
GC . KeepAlive ( _store ) ;
526
532
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 ;
528
540
}
529
541
530
542
/// <summary>
@@ -552,14 +564,26 @@ public Instance(Store store, Module module, params object[] imports)
552
564
/// <returns>Returns the memory if a memory of that name was exported or null if not.</returns>
553
565
public Memory ? GetMemory ( string name )
554
566
{
567
+ lock ( _memoryCache )
568
+ {
569
+ if ( _memoryCache . TryGetValue ( name , out var memory ) )
570
+ return memory ;
571
+ }
572
+
555
573
if ( ! TryGetExtern ( _store . Context , name , out var ext ) || ext . kind != ExternKind . Memory )
556
574
{
557
575
return null ;
558
576
}
559
577
560
578
GC . KeepAlive ( _store ) ;
561
579
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 ;
563
587
}
564
588
565
589
/// <summary>
@@ -715,5 +739,8 @@ private static class Native
715
739
716
740
private readonly Store _store ;
717
741
internal readonly ExternInstance instance ;
742
+
743
+ private readonly Dictionary < string , Function ? > _functionCache = new ( ) ;
744
+ private readonly Dictionary < string , Memory ? > _memoryCache = new ( ) ;
718
745
}
719
746
}
0 commit comments