From 3939aee4c382550e5234f6c313d4e6e976931b5b Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Thu, 27 Aug 2020 04:40:08 -0700 Subject: [PATCH] Introduced concept of `dirty depth` `dirty depth` used to track earliest tree divergence for children --- .../com/riiablo/logger/RiiabloEncoder.java | 2 ++ core/src/com/riiablo/logger/StringMap.java | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/core/src/com/riiablo/logger/RiiabloEncoder.java b/core/src/com/riiablo/logger/RiiabloEncoder.java index 5569f279..5419c1e5 100644 --- a/core/src/com/riiablo/logger/RiiabloEncoder.java +++ b/core/src/com/riiablo/logger/RiiabloEncoder.java @@ -65,6 +65,8 @@ public class RiiabloEncoder extends SimpleEncoder { } if (!mdc.equals(this.mdc)) { + final int dirty = mdc.getDirtyDepth(); + if (dirty < depth && dirty < this.depth) this.depth = dirty; encodeCompactMDC(mdc, buffer, depth); this.mdc = mdc; this.depth = depth; diff --git a/core/src/com/riiablo/logger/StringMap.java b/core/src/com/riiablo/logger/StringMap.java index 97f62ba2..eb323b0f 100644 --- a/core/src/com/riiablo/logger/StringMap.java +++ b/core/src/com/riiablo/logger/StringMap.java @@ -16,21 +16,25 @@ public class StringMap { private boolean immutable; private StringMap immutableCopy; private String toString; + private int dirtyDepth; public StringMap() { keys = ArrayUtils.EMPTY_STRING_ARRAY; vals = ArrayUtils.EMPTY_STRING_ARRAY; indexes = new ObjectIntMap<>(DEFAULT_CAPACITY); + dirtyDepth = Integer.MAX_VALUE; } private StringMap(StringMap other) { assert other.keys.length == other.vals.length; assert other.size == other.indexes.size; size = other.size; + dirtyDepth = Math.min(other.dirtyDepth, other.size); keys = Arrays.copyOf(other.keys, size); vals = Arrays.copyOf(other.vals, size); indexes = new ObjectIntMap<>(other.indexes); immutable = true; + other.dirtyDepth = Integer.MAX_VALUE; } private void inflateTable(final int size) { @@ -51,6 +55,12 @@ public class StringMap { } } + private void updateDirtyDepth(final int index) { + if (index < dirtyDepth) { + dirtyDepth = index < 0 ? Integer.MAX_VALUE : index; + } + } + private void assertMutable() { if (immutable) { throw new UnsupportedOperationException("StringMap has been frozen."); @@ -86,6 +96,7 @@ public class StringMap { indexes.put(key, index); size++; } + updateDirtyDepth(index); assert size == indexes.size; assert size > 0; } @@ -117,6 +128,7 @@ public class StringMap { System.arraycopy(vals, index + 1, vals, index, size - index - 1); keys[size] = vals[size] = null; indexes.remove(key, -1); + updateDirtyDepth(index - 1); size--; assert size == indexes.size; } @@ -132,6 +144,7 @@ public class StringMap { Arrays.fill(vals, null); indexes.clear(); size = 0; + dirtyDepth = 0; assert size == indexes.size; } @@ -182,6 +195,16 @@ public class StringMap { } } + int dirtyDepth() { + return dirtyDepth; + } + + public int getDirtyDepth() { + assertImmutable(); + assert dirtyDepth < Integer.MAX_VALUE; + return dirtyDepth; + } + public StringMapIterator iterator() { return iterator(0); }