Skip to content

Commit 9d28525

Browse files
committed
even faster
1 parent 02fce3c commit 9d28525

27 files changed

Lines changed: 1110 additions & 85 deletions

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/AbstractLmdbCompiledLftjIteration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
// Some portions generated by Codex
1212
package org.eclipse.rdf4j.sail.lmdb;
1313

14+
import org.eclipse.rdf4j.common.annotation.Experimental;
1415
import org.eclipse.rdf4j.common.iteration.LookAheadIteration;
1516
import org.eclipse.rdf4j.query.BindingSet;
1617
import org.eclipse.rdf4j.query.QueryEvaluationException;
1718
import org.eclipse.rdf4j.query.algebra.evaluation.impl.QueryEvaluationContext;
1819

20+
@Experimental
1921
public abstract class AbstractLmdbCompiledLftjIteration extends LookAheadIteration<BindingSet> {
2022

2123
private final LmdbLftjPlan plan;

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/AbstractLmdbFullStackCompiledLftjIteration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
// Some portions generated by Codex
1212
package org.eclipse.rdf4j.sail.lmdb;
1313

14+
import org.eclipse.rdf4j.common.annotation.Experimental;
1415
import org.eclipse.rdf4j.common.iteration.LookAheadIteration;
1516
import org.eclipse.rdf4j.query.BindingSet;
1617
import org.eclipse.rdf4j.query.QueryEvaluationException;
1718
import org.eclipse.rdf4j.query.algebra.evaluation.impl.QueryEvaluationContext;
1819

20+
@Experimental
1921
public abstract class AbstractLmdbFullStackCompiledLftjIteration extends LookAheadIteration<BindingSet> {
2022

2123
private final LmdbLftjPlan plan;

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/LmdbCompiledLftjFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
// Some portions generated by Codex
1212
package org.eclipse.rdf4j.sail.lmdb;
1313

14+
import org.eclipse.rdf4j.common.annotation.Experimental;
1415
import org.eclipse.rdf4j.common.iteration.CloseableIteration;
1516
import org.eclipse.rdf4j.query.BindingSet;
1617
import org.eclipse.rdf4j.query.algebra.evaluation.impl.QueryEvaluationContext;
1718

19+
@Experimental
1820
public interface LmdbCompiledLftjFactory {
1921

2022
CloseableIteration<BindingSet> create(LmdbLftjPlan plan, LmdbLftjExecutionShape shape,

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/LmdbLftjBindingState.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
import java.util.Map;
1818
import java.util.function.BiConsumer;
1919

20+
import org.eclipse.rdf4j.common.annotation.Experimental;
2021
import org.eclipse.rdf4j.model.Value;
2122
import org.eclipse.rdf4j.query.BindingSet;
2223
import org.eclipse.rdf4j.query.MutableBindingSet;
2324
import org.eclipse.rdf4j.query.algebra.evaluation.impl.QueryEvaluationContext;
2425
import org.eclipse.rdf4j.sail.lmdb.model.LmdbValue;
2526

27+
@Experimental
2628
public final class LmdbLftjBindingState {
2729

2830
private final LmdbLftjPlan plan;

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/LmdbLftjCodegenCompiler.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,19 @@ String cacheKey(LmdbLftjPlan plan, LmdbLftjExecutionShape shape, boolean include
3131

3232
LmdbCompiledLftjFactory compile(LmdbLftjPlan plan, LmdbLftjExecutionShape shape) {
3333
String simpleClassName = "GeneratedLmdbLftjFactory" + CLASS_COUNTER.incrementAndGet();
34-
String source = sourceFor(simpleClassName, plan, shape, false);
34+
String source = sourceFor(simpleClassName, plan, shape, false, null);
3535
return compileSource(plan.executionKey(), simpleClassName, source);
3636
}
3737

3838
LmdbCompiledLftjFactory compile(LmdbLftjPlan plan, LmdbLftjExecutionShape shape, boolean includeInferred) {
3939
return compile(plan, shape);
4040
}
4141

42+
LmdbCompiledLftjFactory compile(LmdbLftjPlan plan, LmdbLftjExecutionShape shape, boolean includeInferred,
43+
LmdbQueryAccess queryAccess) {
44+
return compile(plan, shape, includeInferred);
45+
}
46+
4247
protected final LmdbCompiledLftjFactory compileSource(String executionKey, String simpleClassName, String source) {
4348
try {
4449
SimpleCompiler compiler = new SimpleCompiler();
@@ -54,21 +59,37 @@ protected final LmdbCompiledLftjFactory compileSource(String executionKey, Strin
5459
}
5560

5661
String sourceFor(LmdbLftjPlan plan, LmdbLftjExecutionShape shape, boolean includeInferred) {
57-
return sourceFor("GeneratedLmdbLftjSource", plan, shape, includeInferred);
62+
return sourceFor(plan, shape, includeInferred, null);
63+
}
64+
65+
String sourceFor(LmdbLftjPlan plan, LmdbLftjExecutionShape shape, boolean includeInferred,
66+
LmdbQueryAccess queryAccess) {
67+
return sourceFor("GeneratedLmdbLftjSource", plan, shape, includeInferred, queryAccess);
5868
}
5969

6070
Path dumpSourceFor(Path outputFile, LmdbLftjPlan plan, LmdbLftjExecutionShape shape, boolean includeInferred)
6171
throws IOException {
72+
return dumpSourceFor(outputFile, plan, shape, includeInferred, null);
73+
}
74+
75+
Path dumpSourceFor(Path outputFile, LmdbLftjPlan plan, LmdbLftjExecutionShape shape, boolean includeInferred,
76+
LmdbQueryAccess queryAccess)
77+
throws IOException {
6278
Path parent = outputFile.getParent();
6379
if (parent != null) {
6480
Files.createDirectories(parent);
6581
}
66-
Files.writeString(outputFile, sourceFor(plan, shape, includeInferred));
82+
Files.writeString(outputFile, sourceFor(plan, shape, includeInferred, queryAccess));
6783
return outputFile;
6884
}
6985

7086
protected String sourceFor(String simpleClassName, LmdbLftjPlan plan, LmdbLftjExecutionShape shape,
7187
boolean includeInferred) {
88+
return sourceFor(simpleClassName, plan, shape, includeInferred, null);
89+
}
90+
91+
protected String sourceFor(String simpleClassName, LmdbLftjPlan plan, LmdbLftjExecutionShape shape,
92+
boolean includeInferred, LmdbQueryAccess queryAccess) {
7293
return new SourceBuilder(simpleClassName, plan, shape).build();
7394
}
7495

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/LmdbLftjCursor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// Some portions generated by Codex
1212
package org.eclipse.rdf4j.sail.lmdb;
1313

14+
import org.eclipse.rdf4j.common.annotation.Experimental;
15+
16+
@Experimental
1417
public interface LmdbLftjCursor extends AutoCloseable {
1518

1619
boolean open(int bindingSlot);

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/LmdbLftjExecutionShape.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import java.util.List;
1818
import java.util.Map;
1919

20+
import org.eclipse.rdf4j.common.annotation.Experimental;
21+
22+
@Experimental
2023
public final class LmdbLftjExecutionShape {
2124

2225
private static final int FULL_STACK_TEMPLATE_VERSION = 4;

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/LmdbLftjExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private LmdbCompiledLftjFactory compiledFactory(LmdbQueryAccess queryAccess, Lmd
8080
}
8181

8282
try {
83-
LmdbCompiledLftjFactory factory = compiler.compile(plan, shape, queryAccess.includeInferred());
83+
LmdbCompiledLftjFactory factory = compiler.compile(plan, shape, queryAccess.includeInferred(), queryAccess);
8484
queryAccess.cacheCompiledPlanSuccess(cacheKey, factory);
8585
return factory;
8686
} catch (RuntimeException e) {

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/LmdbLftjFullCodegenCompiler.java

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,20 @@ String cacheKey(LmdbLftjPlan plan, LmdbLftjExecutionShape shape, boolean include
2828

2929
@Override
3030
protected String sourceFor(String simpleClassName, LmdbLftjPlan plan, LmdbLftjExecutionShape shape,
31-
boolean includeInferred) {
32-
return new SourceBuilder(simpleClassName, plan, shape, includeInferred).build();
31+
boolean includeInferred, LmdbQueryAccess queryAccess) {
32+
return new SourceBuilder(simpleClassName, plan, shape, includeInferred, queryAccess).build();
3333
}
3434

3535
@Override
3636
LmdbCompiledLftjFactory compile(LmdbLftjPlan plan, LmdbLftjExecutionShape shape, boolean includeInferred) {
37+
return compile(plan, shape, includeInferred, null);
38+
}
39+
40+
@Override
41+
LmdbCompiledLftjFactory compile(LmdbLftjPlan plan, LmdbLftjExecutionShape shape, boolean includeInferred,
42+
LmdbQueryAccess queryAccess) {
3743
String simpleClassName = "GeneratedLmdbFullStackLftjFactory" + System.nanoTime();
38-
String source = sourceFor(simpleClassName, plan, shape, includeInferred);
44+
String source = sourceFor(simpleClassName, plan, shape, includeInferred, queryAccess);
3945
return compileSource(cacheKey(plan, shape, includeInferred), simpleClassName, source);
4046
}
4147

@@ -47,15 +53,17 @@ private static final class SourceBuilder {
4753
private final LmdbLftjPlan plan;
4854
private final LmdbLftjExecutionShape shape;
4955
private final boolean includeInferred;
56+
private final long[][] constantIds;
5057
private final int[] relationGroupByPattern;
5158
private final RelationGroup[] relationGroups;
5259

5360
private SourceBuilder(String simpleClassName, LmdbLftjPlan plan, LmdbLftjExecutionShape shape,
54-
boolean includeInferred) {
61+
boolean includeInferred, LmdbQueryAccess queryAccess) {
5562
this.simpleClassName = simpleClassName;
5663
this.plan = plan;
5764
this.shape = shape;
5865
this.includeInferred = includeInferred;
66+
this.constantIds = resolveConstantIds(queryAccess);
5967
this.relationGroupByPattern = new int[shape.patternCount()];
6068
for (int i = 0; i < relationGroupByPattern.length; i++) {
6169
relationGroupByPattern[i] = -1;
@@ -597,11 +605,9 @@ private void appendRelationGroupAccessor(StringBuilder source, RelationGroup rel
597605
source.append(" return relationGroup").append(relationGroup.groupId).append(";\n");
598606
source.append(" }\n");
599607
source.append(" metrics().recordRelationLoad();\n");
600-
source.append(" long predicateId = state().fixedIdForComponent(")
601-
.append(patternOrdinal)
602-
.append(", ")
603-
.append(TripleStore.PRED_IDX)
604-
.append(");\n");
608+
source.append(" long predicateId = ")
609+
.append(fixedIdExpression(patternOrdinal, TripleStore.PRED_IDX))
610+
.append(";\n");
605611
source.append(" relationGroup")
606612
.append(relationGroup.groupId)
607613
.append(" = loadDerivedRelation(")
@@ -1466,18 +1472,7 @@ private String componentFieldName(int component) {
14661472
private String componentValueExpression(int patternOrdinal, LmdbLftjExecutionShape.PatternShape patternShape,
14671473
int component) {
14681474
if (patternShape.isConstantComponent(component)) {
1469-
switch (component) {
1470-
case TripleStore.SUBJ_IDX:
1471-
return "state().fixedIdForComponent(" + patternOrdinal + ", " + TripleStore.SUBJ_IDX + ")";
1472-
case TripleStore.PRED_IDX:
1473-
return "state().fixedIdForComponent(" + patternOrdinal + ", " + TripleStore.PRED_IDX + ")";
1474-
case TripleStore.OBJ_IDX:
1475-
return "state().fixedIdForComponent(" + patternOrdinal + ", " + TripleStore.OBJ_IDX + ")";
1476-
case TripleStore.CONTEXT_IDX:
1477-
return "state().fixedIdForComponent(" + patternOrdinal + ", " + TripleStore.CONTEXT_IDX + ")";
1478-
default:
1479-
throw new IllegalArgumentException("Unknown LMDB component: " + component);
1480-
}
1475+
return fixedIdExpression(patternOrdinal, component);
14811476
}
14821477
int slot = patternShape.slotForComponent(component);
14831478
if (slot >= 0) {
@@ -1486,6 +1481,14 @@ private String componentValueExpression(int patternOrdinal, LmdbLftjExecutionSha
14861481
return "0L";
14871482
}
14881483

1484+
private String fixedIdExpression(int patternOrdinal, int component) {
1485+
long resolvedId = constantIds[patternOrdinal][component];
1486+
if (resolvedId > 0L) {
1487+
return resolvedId + "L";
1488+
}
1489+
return "state().fixedIdForComponent(" + patternOrdinal + ", " + component + ")";
1490+
}
1491+
14891492
private String upperBoundExpression(int patternOrdinal, LmdbLftjExecutionShape.PatternShape patternShape,
14901493
int slot,
14911494
int component) {
@@ -1526,6 +1529,28 @@ private RelationGroup[] collectRelationGroups() {
15261529
return ordered.toArray(new RelationGroup[0]);
15271530
}
15281531

1532+
private long[][] resolveConstantIds(LmdbQueryAccess queryAccess) {
1533+
long[][] resolved = new long[shape.patternCount()][4];
1534+
if (queryAccess == null) {
1535+
return resolved;
1536+
}
1537+
for (int patternOrdinal = 0; patternOrdinal < shape.patternCount(); patternOrdinal++) {
1538+
LmdbLftjPatternPlan patternPlan = plan.patternPlans().get(patternOrdinal);
1539+
resolveConstantId(resolved[patternOrdinal], patternPlan.subjectTerm(), queryAccess);
1540+
resolveConstantId(resolved[patternOrdinal], patternPlan.predicateTerm(), queryAccess);
1541+
resolveConstantId(resolved[patternOrdinal], patternPlan.objectTerm(), queryAccess);
1542+
resolveConstantId(resolved[patternOrdinal], patternPlan.termForComponent(TripleStore.CONTEXT_IDX),
1543+
queryAccess);
1544+
}
1545+
return resolved;
1546+
}
1547+
1548+
private void resolveConstantId(long[] resolved, LmdbLftjPatternPlan.TermRef term, LmdbQueryAccess queryAccess) {
1549+
if (term.isConstant()) {
1550+
resolved[term.component()] = queryAccess.resolveId(term.constantValue());
1551+
}
1552+
}
1553+
15291554
private static final class RelationGroup {
15301555
private final int groupId;
15311556
private final int representativePatternOrdinal;

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/LmdbLftjMetrics.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// Some portions generated by Codex
1212
package org.eclipse.rdf4j.sail.lmdb;
1313

14+
import org.eclipse.rdf4j.common.annotation.Experimental;
15+
16+
@Experimental
1417
public final class LmdbLftjMetrics {
1518

1619
private long candidateScans;

0 commit comments

Comments
 (0)