Skip to content

Commit

Permalink
niti: cache lookup_first_definition
Browse files Browse the repository at this point in the history
Signed-off-by: Jean Privat <[email protected]>
  • Loading branch information
privat committed Aug 22, 2024
1 parent f3fe23b commit 74c47e8
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/interpreter/naive_interpreter.nit
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class NaiveInterpreter
fun clear_caches
do
anchor_to_cache.clear
lookup_first_definition_cache.clear
end

# Subtype test in the context of the mainmodule
Expand Down Expand Up @@ -645,10 +646,23 @@ class NaiveInterpreter
var mtype = recv.mtype
var ret = send_commons(mproperty, args, mtype)
if ret != null then return ret
var propdef = mproperty.lookup_first_definition(self.mainmodule, mtype)
var propdef = lookup_first_definition(mtype, mproperty)
return self.call(propdef, args)
end

private var lookup_first_definition_cache = new HashMap2[MType, MMethod, MMethodDef]

# Cached version of lookup_first_definition for the main module
fun lookup_first_definition(mtype: MType, mproperty: MMethod): MMethodDef
do
if mproperty.mpropdefs.length == 1 then return mproperty.mpropdefs.first
var res = lookup_first_definition_cache[mtype, mproperty]
if res != null then return res
res = mproperty.lookup_first_definition(self.mainmodule, mtype)
lookup_first_definition_cache[mtype, mproperty] = res
return res
end

# Read the attribute `mproperty` of an instance `recv` and return its value.
# If the attribute in not yet initialized, then aborts with an error message.
fun read_attribute(mproperty: MAttribute, recv: Instance): Instance
Expand Down

0 comments on commit 74c47e8

Please sign in to comment.