Skip to content

Commit

Permalink
Java version of a function that was in the hot path
Browse files Browse the repository at this point in the history
  • Loading branch information
DavyLandman committed Jun 19, 2024
1 parent 84b0dd7 commit 7c4c62b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
47 changes: 47 additions & 0 deletions src/org/rascalmpl/core/library/lang/rascalcore/Performance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.rascalmpl.core.library.lang.rascalcore;

import java.util.Objects;

import io.usethesource.vallang.IBool;
import io.usethesource.vallang.ISourceLocation;
import io.usethesource.vallang.IValueFactory;

public class Performance {
private final IValueFactory vf;

public Performance(IValueFactory vf) {
this.vf = vf;
}

private static boolean isSameFile(ISourceLocation inner, ISourceLocation outer) {
return Objects.equals(inner.getScheme(), outer.getScheme())
&& Objects.equals(inner.getAuthority(), outer.getAuthority())
&& Objects.equals(inner.getPath(), outer.getPath())
&& Objects.equals(inner.getQuery(), outer.getQuery())
;
}

public IBool isStrictlyContainedIn2(ISourceLocation inner, ISourceLocation outer) {
if (!isSameFile(inner, outer)) {
return vf.bool(false);
}
// original code would also do full equality, but we don't need that due to the logic in the outer & inner offset compares
if (inner.hasOffsetLength()) {
int innerOffset = inner.getOffset();
int innerLength = inner.getLength();
if (outer.hasOffsetLength()) {
int outerOffset = outer.getOffset();
int outerLength = outer.getLength();
return vf.bool(
innerOffset == outerOffset && innerOffset + innerLength < outerOffset + outerLength
|| innerOffset > outerOffset && innerOffset + innerLength <= outerOffset + outerLength
);
}
else {
return vf.bool(innerOffset > 0);
}
}
return vf.bool(false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ void addCommonKeywordFields(Solver s){
list[&T <: node ] unsetRec(list[&T <: node] args) = [unsetRec(a) | a <- args];
bool isManualLayout(AProduction p) = (p has attributes && atag("manual"()) in p.attributes);
@javaClass{org.rascalmpl.core.library.lang.rascalcore.Performance}
java bool isStrictlyContainedIn2(loc a, loc b);
tuple[TModel, ModuleStatus] addGrammar(str qualifiedModuleName, set[str] imports, set[str] extends, map[str,TModel] transient_tms, ModuleStatus ms){
try {
Expand All @@ -155,7 +157,7 @@ tuple[TModel, ModuleStatus] addGrammar(str qualifiedModuleName, set[str] imports
prodLocs1 = { k | loc k <- facts, aprod(_) := facts[k] };
// filter out productions contained in priority/associativity declarations
prodLocs2 = { k | k <- prodLocs1, !any(l <- prodLocs1, k != l, isStrictlyContainedIn(k, l)) };
prodLocs2 = { k | k <- prodLocs1, !any(l <- prodLocs1, k != l, isStrictlyContainedIn2(k, l)) };
definedProductions += {<p.def, p> | loc k <- prodLocs2, aprod(p) := facts[k] };
//definedProductions += {<p1.def, p1> | loc k <- prodLocs2, aprod(p) := facts[k], p1 := p[def=unset(p.def)] };/*syn*/
Expand Down

0 comments on commit 7c4c62b

Please sign in to comment.