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

Minor optimizations and cleanup in decoding/encoding #208

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 7 additions & 8 deletions core/src/main/java/eu/ostrzyciel/jelly/core/EncoderLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public LookupEntry(int getId, int setId, boolean newEntry) {
// This will monotonically increase until it reaches the maximum size.
private int used;
// The last id that was set in the table.
private int lastSetId;
private int lastSetId = -1000;
// Names of the entries. Entry 0 is always null.
private final String[] names;
// Whether to use serials for the entries.
// Whether to maintain serial numbers for the entries.
private final boolean useSerials;

private final LookupEntry entryForReturns = new LookupEntry(0, 0, true);
Expand Down Expand Up @@ -133,13 +133,11 @@ public LookupEntry getOrAddEntry(String key) {
names[id] = key;
map.put(key, new LookupEntry(id, id));
// setId is 0 because we are adding a new entry sequentially
entryForReturns.setId = 0;
// .serial is already 1 by default
// entryForReturns.serial = 1;
// We don't need to set it because it's 0 by default
// entryForReturns.setId = 0;
} else {
// The table is full, evict the least recently used entry.
int base = table[1];
id = base / 2;
id = table[1] / 2;
// Remove the entry from the map
LookupEntry oldEntry = map.remove(names[id]);
// Insert the new entry
Expand All @@ -148,6 +146,8 @@ public LookupEntry getOrAddEntry(String key) {
// Update the table
onAccess(id);
entryForReturns.setId = lastSetId + 1 == id ? 0 : id;
// We only update lastSetId in this case, because in the sequential case we don't check it anyway
lastSetId = id;
}
if (this.useSerials) {
// Increment the serial number
Expand All @@ -156,7 +156,6 @@ public LookupEntry getOrAddEntry(String key) {
++serials[id];
}
entryForReturns.getId = id;
lastSetId = id;
return entryForReturns;
}
}
49 changes: 20 additions & 29 deletions core/src/main/java/eu/ostrzyciel/jelly/core/NameDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ private static final class PrefixLookupEntry {
private int lastPrefixIdReference = 0;
private int lastNameIdReference = 0;

private int lastPrefixIdSet = -1;
private int lastNameIdSet = -1;
private int lastPrefixIdSet = 0;
private int lastNameIdSet = 0;

private final Function<String, TIri> iriFactory;

Expand All @@ -45,13 +45,13 @@ private static final class PrefixLookupEntry {
*/
public NameDecoder(int prefixTableSize, int nameTableSize, Function<String, TIri> iriFactory) {
this.iriFactory = iriFactory;
nameLookup = new NameLookupEntry[nameTableSize];
prefixLookup = new PrefixLookupEntry[prefixTableSize];
nameLookup = new NameLookupEntry[nameTableSize + 1];
prefixLookup = new PrefixLookupEntry[prefixTableSize + 1];

for (int i = 0; i < nameTableSize; i++) {
for (int i = 1; i < nameTableSize + 1; i++) {
nameLookup[i] = new NameLookupEntry();
}
for (int i = 0; i < prefixTableSize; i++) {
for (int i = 1; i < prefixTableSize + 1; i++) {
prefixLookup[i] = new PrefixLookupEntry();
}
}
Expand All @@ -63,11 +63,11 @@ public NameDecoder(int prefixTableSize, int nameTableSize, Function<String, TIri
*/
public void updateNames(RdfNameEntry nameEntry) {
int id = nameEntry.id();
if (id == 0) {
lastNameIdSet++;
} else {
lastNameIdSet = id - 1;
}
// Branchless! Equivalent to:
// if (id == 0) lastNameIdSet++;
// else lastNameIdSet = id;
// Same code is used in the methods below.
lastNameIdSet = ((lastNameIdSet + 1) & ((id - 1) >> 31)) + id;
NameLookupEntry entry = nameLookup[lastNameIdSet];
entry.name = nameEntry.value();
// Enough to invalidate the last IRI – we don't have to touch the serial number.
Expand All @@ -83,11 +83,7 @@ public void updateNames(RdfNameEntry nameEntry) {
*/
public void updatePrefixes(RdfPrefixEntry prefixEntry) {
int id = prefixEntry.id();
if (id == 0) {
lastPrefixIdSet++;
} else {
lastPrefixIdSet = id - 1;
}
lastPrefixIdSet = ((lastPrefixIdSet + 1) & ((id - 1) >> 31)) + id;
PrefixLookupEntry entry = prefixLookup[lastPrefixIdSet];
entry.prefix = prefixEntry.value();
entry.serial++;
Expand All @@ -104,23 +100,18 @@ public void updatePrefixes(RdfPrefixEntry prefixEntry) {
@SuppressWarnings("unchecked")
public TIri decode(RdfIri iri) {
int nameId = iri.nameId();

NameLookupEntry nameEntry;
if (nameId == 0) {
nameEntry = nameLookup[lastNameIdReference];
lastNameIdReference++;
} else {
nameEntry = nameLookup[nameId - 1];
lastNameIdReference = nameId;
}
lastNameIdReference = ((lastNameIdReference + 1) & ((nameId - 1) >> 31)) + nameId;
NameLookupEntry nameEntry = nameLookup[lastNameIdReference];

int prefixId = iri.prefixId();
if (prefixId == 0) prefixId = lastPrefixIdReference;
else lastPrefixIdReference = prefixId;

// Branchless way to update the prefix ID
// Equivalent to:
// if (prefixId == 0) prefixId = lastPrefixIdReference;
// else lastPrefixIdReference = prefixId;
lastPrefixIdReference = prefixId = (((prefixId - 1) >> 31) & lastPrefixIdReference) + prefixId;
if (prefixId != 0) {
// Name and prefix
PrefixLookupEntry prefixEntry = prefixLookup[prefixId - 1];
PrefixLookupEntry prefixEntry = prefixLookup[prefixId];
if (nameEntry.lastPrefixId != prefixId || nameEntry.lastPrefixSerial != prefixEntry.serial) {
// Update the last prefix
nameEntry.lastPrefixId = prefixId;
Expand Down
9 changes: 5 additions & 4 deletions core/src/main/java/eu/ostrzyciel/jelly/core/NodeEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,13 @@ public UniversalTerm encodeIri(String iri, ArrayBuffer<RdfStreamRow> rowsBuffer)
new RdfNameEntry(nameEntry.setId, iri)
));
}
if (lastIriNameId + 1 == nameEntry.getId) {
lastIriNameId = nameEntry.getId;
int nameId = nameEntry.getId;
if (lastIriNameId + 1 == nameId) {
lastIriNameId = nameId;
return zeroIri;
} else {
lastIriNameId = nameEntry.getId;
return nameOnlyIris[lastIriNameId];
lastIriNameId = nameId;
return nameOnlyIris[nameId];
}
}

Expand Down
22 changes: 18 additions & 4 deletions core/src/test/scala/eu/ostrzyciel/jelly/core/NameDecoderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,20 @@ class NameDecoderSpec extends AnyWordSpec, Matchers:
}
}

"not accept a new prefix ID lower than 0" in {
"not accept a new prefix ID lower than 0 (-1)" in {
val dec = makeDecoder(smallOptions)
intercept[ArrayIndexOutOfBoundsException] {
intercept[NullPointerException] {
dec.updatePrefixes(RdfPrefixEntry(-1, "https://test.org/"))
}
}

"not accept a new prefix ID lower than 0 (-2)" in {
val dec = makeDecoder(smallOptions)
intercept[ArrayIndexOutOfBoundsException] {
dec.updatePrefixes(RdfPrefixEntry(-2, "https://test.org/"))
}
}

"not retrieve a prefix ID larger than table size" in {
val dec = makeDecoder(smallOptions)
intercept[ArrayIndexOutOfBoundsException] {
Expand All @@ -136,13 +143,20 @@ class NameDecoderSpec extends AnyWordSpec, Matchers:
}
}

"not accept a new name ID lower than 0" in {
"not accept a new name ID lower than 0 (-1)" in {
val dec = makeDecoder(smallOptions)
intercept[ArrayIndexOutOfBoundsException] {
intercept[NullPointerException] {
dec.updateNames(RdfNameEntry(-1, "Cake"))
}
}

"not accept a new name ID lower than 0 (-2)" in {
val dec = makeDecoder(smallOptions)
intercept[ArrayIndexOutOfBoundsException] {
dec.updateNames(RdfNameEntry(-2, "Cake"))
}
}

"not retrieve a name ID larger than table size" in {
val dec = makeDecoder(smallOptions)
intercept[ArrayIndexOutOfBoundsException] {
Expand Down