Skip to content

Commit

Permalink
Optimisations around memory size calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Jul 19, 2023
1 parent 92a4e26 commit f7695f8
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 4 deletions.
9 changes: 6 additions & 3 deletions convex-core/src/main/java/convex/core/data/ACell.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -555,6 +557,7 @@ public static <T extends ACell> Ref<T> 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());
}
Expand Down
1 change: 1 addition & 0 deletions convex-core/src/main/java/convex/core/data/ASymbolic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions convex-core/src/main/java/convex/core/data/AccountKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
5 changes: 5 additions & 0 deletions convex-core/src/main/java/convex/core/data/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
2 changes: 2 additions & 0 deletions convex-core/src/main/java/convex/core/data/prim/CVMChar.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,6 +48,7 @@ public final class CVMChar extends APrimitive implements Comparable<CVMChar> {

private CVMChar(int value) {
this.value=value;
this.memorySize=Format.FULL_EMBEDDED_MEMORY_SIZE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public final class CVMLong extends AInteger {

public CVMLong(long value) {
this.value=value;
this.memorySize=Format.FULL_EMBEDDED_MEMORY_SIZE;
}

/**
Expand Down

0 comments on commit f7695f8

Please sign in to comment.