-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experiment: again try a lighter impl for encoder node cache (#212)
- Loading branch information
1 parent
b64515a
commit c2bad54
Showing
5 changed files
with
116 additions
and
47 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
core/src/main/java/eu/ostrzyciel/jelly/core/EncoderNodeCache.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; | ||
|
||
/** | ||
* A terrifyingly simple cache. | ||
*/ | ||
abstract class EncoderNodeCache<V> { | ||
|
||
protected final Object[] keys; | ||
|
||
protected final int sizeMinusOne; | ||
|
||
protected EncoderNodeCache(int minimumSize) { | ||
var size = Integer.highestOneBit(minimumSize); | ||
if (size < minimumSize) { | ||
size <<= 1; | ||
} | ||
this.sizeMinusOne = size - 1; | ||
keys = new Object[size]; | ||
} | ||
|
||
protected int calcIndex(Object key) { | ||
int h = key.hashCode(); | ||
// Spread bits to avoid collisions for hashes that differ only in the upper bits. | ||
// Trick from HashMap.hash() | ||
return (h ^ h >>> 16) & sizeMinusOne; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
core/src/main/java/eu/ostrzyciel/jelly/core/EncoderNodeCacheDependent.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,47 @@ | ||
package eu.ostrzyciel.jelly.core; | ||
|
||
import eu.ostrzyciel.jelly.core.proto.v1.UniversalTerm; | ||
|
||
/** | ||
* A cached node that depends on other lookups (RdfIri and RdfLiteral in the datatype variant). | ||
*/ | ||
final class DependentNode { | ||
// The actual cached node | ||
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. | ||
int lookupPointer1; | ||
int lookupSerial1; | ||
// 2: IRI prefixes | ||
int lookupPointer2; | ||
int lookupSerial2; | ||
} | ||
|
||
class EncoderNodeCacheDependent extends EncoderNodeCache<DependentNode> { | ||
private final DependentNode[] values; | ||
|
||
EncoderNodeCacheDependent(int minimumSize) { | ||
super(minimumSize); | ||
DependentNode[] x = new DependentNode[sizeMinusOne + 1]; | ||
values = x; | ||
for (int i = 0; i < values.length; i++) { | ||
values[i] = new DependentNode(); | ||
} | ||
} | ||
|
||
DependentNode getOrClearIfAbsent(Object key) { | ||
final int idx = calcIndex(key); | ||
final Object storedKey = keys[idx]; | ||
final DependentNode node = values[idx]; | ||
if (storedKey != null && (storedKey == key || storedKey.equals(key))) { | ||
return node; | ||
} else { | ||
node.encoded = null; | ||
node.lookupPointer1 = 0; | ||
node.lookupPointer2 = 0; | ||
keys[idx] = key; | ||
return node; | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
core/src/main/java/eu/ostrzyciel/jelly/core/EncoderNodeCacheSimple.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,28 @@ | ||
package eu.ostrzyciel.jelly.core; | ||
|
||
import eu.ostrzyciel.jelly.core.proto.v1.UniversalTerm; | ||
|
||
import java.util.function.Function; | ||
|
||
class EncoderNodeCacheSimple extends EncoderNodeCache<UniversalTerm> { | ||
private final UniversalTerm[] values; | ||
|
||
EncoderNodeCacheSimple(int minimumSize) { | ||
super(minimumSize); | ||
UniversalTerm[] x = new UniversalTerm[sizeMinusOne + 1]; | ||
values = x; | ||
} | ||
|
||
UniversalTerm getOrComputeIfAbsent(Object key, Function<Object, UniversalTerm> f) { | ||
final int idx = calcIndex(key); | ||
final Object storedKey = keys[idx]; | ||
if (storedKey != null && (storedKey == key || storedKey.equals(key))) { | ||
return values[idx]; | ||
} else { | ||
keys[idx] = key; | ||
UniversalTerm newTerm = f.apply(key); | ||
values[idx] = newTerm; | ||
return newTerm; | ||
} | ||
} | ||
} |
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