-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encoder: maybe speed up the node cache (2)
For jelly-big I saw similar performance, but for jelly-small much worse... Here I've increased the node cache size significantly (hey, it's just an array of pointers, what does that cost in the era of Google Chrome) and made optimized implementations for both caches to avoid doing useless operations.
- Loading branch information
1 parent
31e19e1
commit dbd2395
Showing
5 changed files
with
100 additions
and
55 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
core/src/main/java/eu/ostrzyciel/jelly/core/DependentNodeCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package eu.ostrzyciel.jelly.core; | ||
|
||
import eu.ostrzyciel.jelly.core.proto.v1.UniversalTerm; | ||
|
||
import java.util.function.Function; | ||
|
||
/** | ||
* A cached node that depends on other lookups (RdfIri and RdfLiteral in the datatype variant). | ||
*/ | ||
final class DependentNode { | ||
// The actual cached node | ||
public UniversalTerm encoded; | ||
// 1: datatypes and IRI names | ||
// The pointer is the index in the lookup table, the serial is the serial number of the entry. | ||
// The serial in the lookup table must be equal to the serial here for the entry to be valid. | ||
public int lookupPointer1; | ||
public int lookupSerial1; | ||
// 2: IRI prefixes | ||
public int lookupPointer2; | ||
public int lookupSerial2; | ||
} | ||
|
||
final class DependentNodeCache extends EncoderNodeCache<Object, DependentNode> { | ||
private final DependentNode[] values; | ||
|
||
public DependentNodeCache(int minimumSize) { | ||
super(minimumSize); | ||
|
||
DependentNode[] x = new DependentNode[sizeMinusOne + 1]; | ||
values = x; | ||
|
||
for (int i = 0; i < values.length; i++) { | ||
values[i] = new DependentNode(); | ||
} | ||
} | ||
|
||
public DependentNode getOrClearIfAbsent(Object key) { | ||
final int idx = calcIndex(key); | ||
final DependentNode node = (DependentNode) values[idx]; | ||
if (node != null && node.equals(key)) { | ||
return node; | ||
} else { | ||
node.encoded = null; | ||
node.lookupPointer1 = 0; | ||
node.lookupPointer2 = 0; | ||
keys[idx] = key; | ||
return node; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
core/src/main/java/eu/ostrzyciel/jelly/core/OtherNodeCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package eu.ostrzyciel.jelly.core; | ||
|
||
import eu.ostrzyciel.jelly.core.proto.v1.*; | ||
import java.util.function.Function; | ||
|
||
public class OtherNodeCache extends EncoderNodeCache<Object, UniversalTerm> { | ||
private final UniversalTerm[] values; | ||
|
||
public OtherNodeCache(int minimumSize) { | ||
super(minimumSize); | ||
|
||
UniversalTerm[] x = new UniversalTerm[sizeMinusOne + 1]; | ||
values = x; | ||
} | ||
|
||
public UniversalTerm computeIfAbsent(Object key, Function<Object, UniversalTerm> function) { | ||
final int idx = calcIndex(key); | ||
if (keys[idx] != null && keys[idx].equals(key)) { | ||
return values[idx]; | ||
} else { | ||
final var value = function.apply(key); | ||
values[idx] = value; | ||
keys[idx] = key; | ||
return value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters