Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java performance improvements from Apache Lucene #195

Merged
merged 6 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions compiler/generator_java.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ static void generate_AE(struct generator * g, struct node * p) {
break;
case c_len: /* Same as size() for Java. */
case c_size:
w(g, "current.length()");
w(g, "length");
break;
}
}
Expand Down Expand Up @@ -977,9 +977,12 @@ static void generate_define(struct generator * g, struct node * p) {
* be required to allow the SnowballProgram base class to invoke them.
* FIXME: Is this avoidable?
*/
if (q->type == t_routine && !q->used_in_among) {
if (q->used_in_among) {
g->S[0] = "public";
} else if (q->type == t_routine) {
g->S[0] = "private";
} else {
w(g, "~N~M@Override");
g->S[0] = "public";
}
g->V[0] = q;
Expand Down Expand Up @@ -1192,6 +1195,7 @@ static void generate_class_begin(struct generator * g) {
w(g, " {~+~N"
"~N"
"~Mprivate static final long serialVersionUID = 1L;~N"
"~Mprivate static final java.lang.invoke.MethodHandles.Lookup methodObject = java.lang.invoke.MethodHandles.lookup();~N"
ojwb marked this conversation as resolved.
Show resolved Hide resolved
"~N");
}

Expand Down Expand Up @@ -1238,7 +1242,7 @@ static void generate_among_table(struct generator * g, struct among * x) {
if (v->function != 0) {
w(g, ", \"");
write_varname(g, v->function);
w(g, "\", ~n.class");
w(g, "\", methodObject");
}
w(g, ")~S0~N");
v++;
Expand Down
39 changes: 28 additions & 11 deletions java/org/tartarus/snowball/Among.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package org.tartarus.snowball;

import java.lang.reflect.Method;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Locale;

/**
* Internal class used by Snowball stemmers
*/
public class Among {
public Among (String s, int substring_i, int result) {
this.s = s.toCharArray();
Expand All @@ -11,19 +17,30 @@ public Among (String s, int substring_i, int result) {
}

public Among (String s, int substring_i, int result, String methodname,
Class<? extends SnowballProgram> programclass) {
MethodHandles.Lookup methodobject) {
this.s = s.toCharArray();
this.substring_i = substring_i;
this.result = result;
try {
this.method = programclass.getDeclaredMethod(methodname);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
final Class<? extends SnowballProgram> clazz = methodobject.lookupClass().asSubclass(SnowballProgram.class);
if (methodname.length() > 0) {
try {
this.method = methodobject.findVirtual(clazz, methodname, MethodType.methodType(boolean.class))
.asType(MethodType.methodType(boolean.class, SnowballProgram.class));
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new RuntimeException(String.format(Locale.ENGLISH,
"Snowball program '%s' is broken, cannot access method: boolean %s()",
clazz.getSimpleName(), methodname
), e);
}
} else {
this.method = null;
}
}

public final char[] s; /* search string */
public final int substring_i; /* index to longest matching substring */
public final int result; /* result of the lookup */
public final Method method; /* method to use if substring matches */
final char[] s; /* search string */
final int substring_i; /* index to longest matching substring */
final int result; /* result of the lookup */

// Make sure this is not accessible outside package for Java security reasons!
final MethodHandle method; /* method to use if substring matches */
};
Loading
Loading