Simplest method cache in run.cc
authorTony Garnock-Jones <tonygarnockjones@gmail.com>
Tue, 07 Aug 2018 17:14:14 +0100
changeset 436 f519b01f759b
parent 435 8cad65af7913
child 437 f3b04d3ef973
Simplest method cache in run.cc
experiments/little-smalltalk/run.cc
--- a/experiments/little-smalltalk/run.cc	Sun Aug 05 20:06:00 2018 +0100
+++ b/experiments/little-smalltalk/run.cc	Tue Aug 07 17:14:14 2018 +0100
@@ -115,6 +115,10 @@
 
   primitive_handler_t primitiveTable[256];
 
+  static unsigned const methodCacheCount = 2039;
+  static unsigned const methodCacheLimit = 3 * methodCacheCount;
+  obj methodCache[methodCacheLimit];
+
   struct timespec mutatorStart;
 
   inline obj objClass(obj o) {
@@ -569,7 +573,16 @@
     // cerr << "Sending " << bvString(_j) << " via " << className(c) << " to ";
     // print(cerr, slotAt(_i, 0));
     // cerr << endl;
-    obj method = lookupMethod(c, _j);
+    unsigned probe = ((((intptr_t) c) * 5 + ((intptr_t) _j)) % methodCacheCount) * 3;
+    obj method;
+    if ((methodCache[probe] == c) && (methodCache[probe + 1] == _j)) {
+      method = methodCache[probe + 2];
+    } else {
+      method = lookupMethod(c, _j);
+      methodCache[probe] = c;
+      methodCache[probe + 1] = _j;
+      methodCache[probe + 2] = method;
+    }
     if (method == 0) {
       cerr << "DNU ";
       print(cerr, slotAt(_i, 0));