You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by us...@apache.org on 2025/05/01 18:00:26 UTC
(lucene) branch main updated: Refactor the expressions compiler to use official ClassData BSM with indexed lookup (#14602)
This is an automated email from the ASF dual-hosted git repository.
uschindler pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git
The following commit(s) were added to refs/heads/main by this push:
new 87df2c8dc50 Refactor the expressions compiler to use official ClassData BSM with indexed lookup (#14602)
87df2c8dc50 is described below
commit 87df2c8dc50315ce3aeaeef90d54e8a8521ea1a9
Author: Uwe Schindler <us...@apache.org>
AuthorDate: Thu May 1 20:00:20 2025 +0200
Refactor the expressions compiler to use official ClassData BSM with indexed lookup (#14602)
---
lucene/CHANGES.txt | 3 ++
.../lucene/expressions/js/JavascriptCompiler.java | 54 +++++++---------------
.../lucene/expressions/js/TestCustomFunctions.java | 5 ++
3 files changed, 24 insertions(+), 38 deletions(-)
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 3b9e4aedbe2..6af1ca3c0eb 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -78,6 +78,9 @@ Improvements
* GITHUB#14476: Removing unnecessary splitX in ComponentTree (Ankit Jain)
+* GITHUB#14602: Refactor the expressions compiler to use official ClassData BSM with indexed lookup
+ (Uwe Schindler)
+
Optimizations
---------------------
* GITHUB#14418: Quick exit on filter query matching no docs when rewriting knn query. (Pan Guixin)
diff --git a/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java b/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java
index 46775f4393a..cbcf2992506 100644
--- a/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java
+++ b/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java
@@ -32,7 +32,6 @@ import java.lang.classfile.instruction.BranchInstruction;
import java.lang.classfile.instruction.OperatorInstruction;
import java.lang.constant.ClassDesc;
import java.lang.constant.ConstantDescs;
-import java.lang.constant.DirectMethodHandleDesc;
import java.lang.constant.DynamicConstantDesc;
import java.lang.constant.MethodTypeDesc;
import java.lang.invoke.MethodHandle;
@@ -126,13 +125,6 @@ public final class JavascriptCompiler {
MTD_PATCH_STACK =
MethodTypeDesc.of(ConstantDescs.CD_Throwable, ConstantDescs.CD_Throwable, CD_Expression);
- private static final DirectMethodHandleDesc BSM_COMPILER_DYNAMIC_CONSTANT =
- ConstantDescs.ofConstantBootstrap(
- CD_JavascriptCompiler,
- "dynamicConstantBootstrap",
- ConstantDescs.CD_MethodHandle,
- ConstantDescs.CD_String);
-
private static final ExceptionsAttribute ATTR_THROWS_IOEXCEPTION =
ExceptionsAttribute.ofSymbols(IOException.class.describeConstable().orElseThrow());
@@ -231,7 +223,8 @@ public final class JavascriptCompiler {
* @throws ParseException on failure to compile
*/
private Expression compileExpression() throws ParseException {
- final Map<String, Integer> externalsMap = new LinkedHashMap<>();
+ final Map<String, Integer> externalsMap = new LinkedHashMap<>(),
+ constantsMap = new LinkedHashMap<>();
final byte[] classFile;
try {
classFile =
@@ -239,7 +232,7 @@ public final class JavascriptCompiler {
ClassHierarchyResolverOption.of(ClassHierarchyResolver.ofClassLoading(LOOKUP)))
.build(
CD_CompiledExpression,
- writer -> generateClass(getAntlrParseTree(), writer, externalsMap));
+ writer -> generateClass(getAntlrParseTree(), writer, externalsMap, constantsMap));
} catch (RuntimeException re) {
// Unwrap the ParseException from RuntimeException
if (re.getCause() instanceof ParseException cause) {
@@ -249,7 +242,9 @@ public final class JavascriptCompiler {
}
try {
- final Lookup lookup = LOOKUP.defineHiddenClassWithClassData(classFile, functions, true);
+ final Lookup lookup =
+ LOOKUP.defineHiddenClassWithClassData(
+ classFile, constantsMap.keySet().stream().map(functions::get).toList(), true);
return invokeConstructor(lookup, lookup.lookupClass(), externalsMap);
} catch (ReflectiveOperationException exception) {
throw new IllegalStateException(
@@ -327,7 +322,8 @@ public final class JavascriptCompiler {
private void generateClass(
final ParseTree parseTree,
final ClassBuilder builder,
- final Map<String, Integer> externalsMap) {
+ final Map<String, Integer> externalsMap,
+ final Map<String, Integer> constantsMap) {
builder.withFlags(ClassFile.ACC_PUBLIC | ClassFile.ACC_FINAL).withSuperclass(CD_Expression);
// constructor:
@@ -346,7 +342,7 @@ public final class JavascriptCompiler {
// evaluate method:
final Consumer<BlockCodeBuilder> mainBlock =
gen -> {
- newClassFileGeneratorVisitor(gen, externalsMap).visit(parseTree);
+ newClassFileGeneratorVisitor(gen, externalsMap, constantsMap).visit(parseTree);
gen.dreturn();
};
final Consumer<BlockCodeBuilder> rethrowBlock =
@@ -365,11 +361,12 @@ public final class JavascriptCompiler {
}
private JavascriptBaseVisitor<Void> newClassFileGeneratorVisitor(
- CodeBuilder gen, final Map<String, Integer> externalsMap) {
+ CodeBuilder gen,
+ final Map<String, Integer> externalsMap,
+ final Map<String, Integer> constantsMap) {
// to completely hide the ANTLR visitor we use an anonymous impl:
return new JavascriptBaseVisitor<Void>() {
private final Deque<TypeKind> typeStack = new ArrayDeque<>();
- private final Map<String, Integer> constantsMap = new HashMap<>();
@Override
public Void visitCompile(JavascriptParser.CompileContext ctx) {
@@ -428,12 +425,13 @@ public final class JavascriptCompiler {
}
// place dynamic constant with MethodHandle on top of stack
+ final int index = constantsMap.computeIfAbsent(text, _ -> constantsMap.size());
final var constantDesc =
DynamicConstantDesc.ofNamed(
- BSM_COMPILER_DYNAMIC_CONSTANT,
- "func" + constantsMap.computeIfAbsent(text, _ -> constantsMap.size()),
+ ConstantDescs.BSM_CLASS_DATA_AT,
+ ConstantDescs.DEFAULT_NAME,
ConstantDescs.CD_MethodHandle,
- text);
+ index);
gen.loadConstant(constantDesc);
// add arguments:
@@ -896,26 +894,6 @@ public final class JavascriptCompiler {
}
}
- /**
- * Bootstrap method for dynamic constants. This returns a {@link MethodHandle} for the {@code
- * functionName} from the class data passed via {@link Lookup#defineHiddenClassWithClassData}. The
- * {@code constantName} is ignored.
- */
- static MethodHandle dynamicConstantBootstrap(
- Lookup lookup, String constantName, Class<?> type, String functionName)
- throws IllegalAccessException {
- if (type != MethodHandle.class) {
- throw new IllegalArgumentException("Invalid type of constant: " + type.getName());
- }
- final var classData =
- Objects.requireNonNull(
- MethodHandles.classData(lookup, ConstantDescs.DEFAULT_NAME, Map.class),
- "Missing class data for " + lookup);
- return (MethodHandle)
- Objects.requireNonNull(
- classData.get(functionName), "Function does not exist: " + functionName);
- }
-
/**
* Method called from try/catch handler in compiled expression. This patches the stack trace and
* adds back a hidden frame (including the source code of script as filename).
diff --git a/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java b/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java
index 5fee6c58103..b61c61880bb 100644
--- a/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java
+++ b/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java
@@ -293,6 +293,11 @@ public class TestCustomFunctions extends CompilerTestCase {
assertEquals(16, expr.evaluate(null), DELTA);
}
+ public void testSameFunctionTwoTimes() throws Exception {
+ Expression expr = compile("sqrt(20)+abs(-7)+sqrt(30)");
+ assertEquals(Math.sqrt(20) + 7 + Math.sqrt(30), expr.evaluate(null), DELTA);
+ }
+
public void testLegacyFunctions() throws Exception {
var functions =
Map.of("foo", TestCustomFunctions.class.getMethod("oneArgMethod", double.class));