noir icon indicating copy to clipboard operation
noir copied to clipboard

Avoid `O(N^2)` scan through globals during name resolution

Open michaeljklein opened this issue 9 months ago • 2 comments

Problem

the following code in the resolver scans through all previous global's whenever a global is added, resulting in O(num_globals^2):

        // This check is necessary to maintain the same definition ids in the interner. Currently, each function uses a new resolver that has its own ScopeForest and thus global scope.
        // We must first check whether an existing definition ID has been inserted as otherwise there will be multiple definitions for the same global statement.
        // This leads to an error in evaluation where the wrong definition ID is selected when evaluating a statement using the global. The check below prevents this error.
        let mut global_id = None;
        let global = self.interner.get_all_globals();
        for global_info in global {
            if global_info.ident == name
                && global_info.local_id == self.path_resolver.local_module_id()
            {
                global_id = Some(global_info.id);
            }
        }

Happy Case

Store global variables in a set or (hash)map to allow looking them up quickly

Project Impact

Nice-to-have

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

michaeljklein avatar May 08 '24 19:05 michaeljklein