diff --git a/convex-core/src/main/java/convex/core/data/ACell.java b/convex-core/src/main/java/convex/core/data/ACell.java index 17723f066..4d115bff8 100644 --- a/convex-core/src/main/java/convex/core/data/ACell.java +++ b/convex-core/src/main/java/convex/core/data/ACell.java @@ -107,8 +107,8 @@ public int hashCode() { @Override public final boolean equals(Object a) { - if (a==this) return true; - if (!(a instanceof ACell)) return false; + if (a==this) return true; // Fast path, avoids cast + if (!(a instanceof ACell)) return false; // Handles null return equals((ACell)a); } @@ -270,7 +270,8 @@ protected long calcMemorySize() { if (!isEmbedded()) { // We need to count this cell's own encoding length // Plus overhead for storage of non-embedded cell - result=Utils.memoryAdd(result,getEncodingLength()+Constants.MEMORY_OVERHEAD); + long encodingLength=getEncodingLength(); + result=Utils.memoryAdd(result,encodingLength+Constants.MEMORY_OVERHEAD); } return result; } @@ -311,6 +312,7 @@ public final long getMemorySize() { * @return true if Cell is embedded, false otherwise */ public boolean isEmbedded() { + if (memorySize==Format.FULL_EMBEDDED_MEMORY_SIZE) return true; if (cachedRef!=null) { int flags=cachedRef.flags; if ((flags&Ref.KNOWN_EMBEDDED_MASK)!=0) return true; @@ -555,6 +557,7 @@ public static Ref createPersisted(T value) { * @return true if completely encoded, false otherwise */ public boolean isCompletelyEncoded() { + if (memorySize==Format.FULL_EMBEDDED_MEMORY_SIZE) return true; // fast path if (!isCanonical()) { throw new Error("Checking whether a non-canonical cell is encoded. Not a good idea, any ref assumptions may be invalid: "+this.getType()); } diff --git a/convex-core/src/main/java/convex/core/data/ASymbolic.java b/convex-core/src/main/java/convex/core/data/ASymbolic.java index 1cf4e9614..758b5b2ef 100644 --- a/convex-core/src/main/java/convex/core/data/ASymbolic.java +++ b/convex-core/src/main/java/convex/core/data/ASymbolic.java @@ -12,6 +12,7 @@ public abstract class ASymbolic extends ACell { protected ASymbolic(StringShort name) { this.name = name; + this.memorySize=Format.FULL_EMBEDDED_MEMORY_SIZE; } @Override diff --git a/convex-core/src/main/java/convex/core/data/AccountKey.java b/convex-core/src/main/java/convex/core/data/AccountKey.java index 001b66459..aa3709e8b 100644 --- a/convex-core/src/main/java/convex/core/data/AccountKey.java +++ b/convex-core/src/main/java/convex/core/data/AccountKey.java @@ -26,6 +26,7 @@ public class AccountKey extends AArrayBlob { private AccountKey(byte[] data, int offset, int length) { super(data, offset, length); + this.memorySize=0; if (length != LENGTH) throw new IllegalArgumentException("AccountKey length must be " + LENGTH + " bytes"); } diff --git a/convex-core/src/main/java/convex/core/data/Format.java b/convex-core/src/main/java/convex/core/data/Format.java index 7cb5ab0a8..a0ad9791b 100644 --- a/convex-core/src/main/java/convex/core/data/Format.java +++ b/convex-core/src/main/java/convex/core/data/Format.java @@ -80,6 +80,11 @@ public class Format { */ public static final long MAX_MESSAGE_LENGTH = 20000000; + /** + * Memory size of a fully embedded value (zero) + */ + public static final long FULL_EMBEDDED_MEMORY_SIZE = 0L; + /** * Gets the length in bytes of VLC encoding for the given long value * @param x Long value to encode diff --git a/convex-core/src/main/java/convex/core/data/prim/CVMBigInteger.java b/convex-core/src/main/java/convex/core/data/prim/CVMBigInteger.java index c4fd0ffe1..5495ea6cb 100644 --- a/convex-core/src/main/java/convex/core/data/prim/CVMBigInteger.java +++ b/convex-core/src/main/java/convex/core/data/prim/CVMBigInteger.java @@ -8,6 +8,7 @@ import convex.core.data.Blob; import convex.core.data.BlobBuilder; import convex.core.data.Blobs; +import convex.core.data.Format; import convex.core.data.Strings; import convex.core.data.Tag; import convex.core.exceptions.BadFormatException; @@ -229,7 +230,7 @@ protected long calcMemorySize() { @Override public boolean isEmbedded() { - if (memorySize==0) return true; + if (memorySize==Format.FULL_EMBEDDED_MEMORY_SIZE) return true; return blob().isEmbedded(); } diff --git a/convex-core/src/main/java/convex/core/data/prim/CVMChar.java b/convex-core/src/main/java/convex/core/data/prim/CVMChar.java index bac497622..d01f9ef49 100644 --- a/convex-core/src/main/java/convex/core/data/prim/CVMChar.java +++ b/convex-core/src/main/java/convex/core/data/prim/CVMChar.java @@ -7,6 +7,7 @@ import convex.core.data.AString; import convex.core.data.Blob; import convex.core.data.BlobBuilder; +import convex.core.data.Format; import convex.core.data.Strings; import convex.core.data.Tag; import convex.core.data.type.AType; @@ -47,6 +48,7 @@ public final class CVMChar extends APrimitive implements Comparable { private CVMChar(int value) { this.value=value; + this.memorySize=Format.FULL_EMBEDDED_MEMORY_SIZE; } @Override diff --git a/convex-core/src/main/java/convex/core/data/prim/CVMLong.java b/convex-core/src/main/java/convex/core/data/prim/CVMLong.java index bbd0dba0f..f22ec31c8 100644 --- a/convex-core/src/main/java/convex/core/data/prim/CVMLong.java +++ b/convex-core/src/main/java/convex/core/data/prim/CVMLong.java @@ -49,6 +49,7 @@ public final class CVMLong extends AInteger { public CVMLong(long value) { this.value=value; + this.memorySize=Format.FULL_EMBEDDED_MEMORY_SIZE; } /**