Skip to content

Commit 74c47e8

Browse files
committed
niti: cache lookup_first_definition
Signed-off-by: Jean Privat <[email protected]>
1 parent f3fe23b commit 74c47e8

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/interpreter/naive_interpreter.nit

+15-1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class NaiveInterpreter
116116
fun clear_caches
117117
do
118118
anchor_to_cache.clear
119+
lookup_first_definition_cache.clear
119120
end
120121

121122
# Subtype test in the context of the mainmodule
@@ -645,10 +646,23 @@ class NaiveInterpreter
645646
var mtype = recv.mtype
646647
var ret = send_commons(mproperty, args, mtype)
647648
if ret != null then return ret
648-
var propdef = mproperty.lookup_first_definition(self.mainmodule, mtype)
649+
var propdef = lookup_first_definition(mtype, mproperty)
649650
return self.call(propdef, args)
650651
end
651652

653+
private var lookup_first_definition_cache = new HashMap2[MType, MMethod, MMethodDef]
654+
655+
# Cached version of lookup_first_definition for the main module
656+
fun lookup_first_definition(mtype: MType, mproperty: MMethod): MMethodDef
657+
do
658+
if mproperty.mpropdefs.length == 1 then return mproperty.mpropdefs.first
659+
var res = lookup_first_definition_cache[mtype, mproperty]
660+
if res != null then return res
661+
res = mproperty.lookup_first_definition(self.mainmodule, mtype)
662+
lookup_first_definition_cache[mtype, mproperty] = res
663+
return res
664+
end
665+
652666
# Read the attribute `mproperty` of an instance `recv` and return its value.
653667
# If the attribute in not yet initialized, then aborts with an error message.
654668
fun read_attribute(mproperty: MAttribute, recv: Instance): Instance

0 commit comments

Comments
 (0)