Dedicated Module.containsUUID method with a cache (#5945)

Fixes #5781 by caching all UUIDs in each module in a `Map`.
This commit is contained in:
Jaroslav Tulach 2023-03-15 18:14:59 +01:00 committed by GitHub
parent d77d08358e
commit e666d797c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 7 deletions

View File

@ -338,13 +338,7 @@ public class EnsoContext {
*/
public Optional<Module> findModuleByExpressionId(UUID expressionId) {
return getTopScope().getModules().stream()
.filter(
module ->
module.getIr() != null
&& module
.getIr()
.preorder()
.exists(ir -> ir.getExternalId().contains(expressionId)))
.filter(m -> m.containsUUID(expressionId))
.findFirst();
}

View File

@ -17,8 +17,10 @@ import com.oracle.truffle.api.source.SourceSection;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.WeakHashMap;
import java.util.logging.Level;
@ -93,6 +95,7 @@ public final class Module implements TruffleObject {
private CompilationStage compilationStage = CompilationStage.INITIAL;
private boolean isIndexed = false;
private IR.Module ir;
private Map<UUID, IR> uuidsMap;
private QualifiedName name;
private final ModuleCache cache;
private boolean wasLoadedFromCache;
@ -285,6 +288,7 @@ public final class Module implements TruffleObject {
};
var copy = this.ir.mapExpressions(fn);
this.ir = copy;
this.uuidsMap = null;
return;
}
}
@ -407,6 +411,28 @@ public final class Module implements TruffleObject {
return ir;
}
public boolean containsUUID(UUID id) {
var map = uuidsMap;
if (map == null) {
var newMap = new HashMap<UUID, IR>();
var localIr = getIr();
if (localIr != null) {
localIr
.preorder()
.foreach(
(v1) -> {
if (v1.getExternalId().isDefined()) {
newMap.put(v1.getExternalId().get(), v1);
}
return null;
});
}
uuidsMap = newMap;
map = newMap;
}
return map.containsKey(id);
}
/** @return the current compilation stage of this module. */
public CompilationStage getCompilationStage() {
return compilationStage;
@ -434,6 +460,7 @@ public final class Module implements TruffleObject {
*/
public void unsafeSetIr(IR.Module ir) {
this.ir = ir;
this.uuidsMap = null;
}
/** @return the runtime scope of this module. */