Skip to content

Commit

Permalink
Edits and improvements for memory sizing
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Apr 24, 2024
1 parent d832343 commit 08ddaa1
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 14 deletions.
7 changes: 7 additions & 0 deletions convex-core/src/main/java/convex/core/data/AArrayBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ public boolean isChunkPacked() {
public boolean isFullyPacked() {
return (count==Blob.CHUNK_LENGTH);
}

@Override
protected long calcMemorySize() {
// fast path for small Blobs, never have child cells
if (isEmbedded()) return 0;
return super.calcMemorySize();
}

@Override
public final byte byteAt(long i) {
Expand Down
9 changes: 5 additions & 4 deletions convex-core/src/main/java/convex/core/data/ACell.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ public String toString() {
/**
* Returns the CVM String representation of this Cell. Normally, this is as printed, but may be different for some types.
*
* MUST return null in O(1) time if the length of the CVM String would exceed limit.
* SHOULD return null in O(1) time if the length of the CVM String can be proved to exceed the limit.
* MUST complete in O(limit) time and space otherwise
*
* The String representation is intended to be a easy-to-read textual representation of the Cell's data content.
* @param limit Limit of CVM String length in UTF-8 bytes
Expand All @@ -243,7 +244,7 @@ public AString toCVMString(long limit) {
/**
* Gets the cached blob representing this Cell's Encoding in binary format, if it exists.
*
* @return The cached blob for this cell, or null if not available.
* @return The cached blob for this cell, or null if not yet available.
*/
public Blob cachedEncoding() {
return encoding;
Expand Down Expand Up @@ -290,7 +291,7 @@ public int getEncodingLength() {
* Gets the Memory Size of this Cell, computing it if required.
*
* The memory size is the total storage requirement for this cell. Embedded cells do not require storage for
* their own encoding, but may require storage for nested non-embedded Refs.
* their own encoding, but may require storage for nested branches.
*
* @return Memory Size of this Cell
*/
Expand All @@ -306,7 +307,7 @@ public final long getMemorySize() {
* Gets the Memory Size of a Cell, computing it if required.
*
* The memory size is the total storage requirement for this cell. Embedded cells do not require storage for
* their own encoding, but may require storage for nested non-embedded Refs.
* their own encoding, but may require storage for nested branches.
*
* @return Memory Size of this Cell
*/
Expand Down
12 changes: 6 additions & 6 deletions convex-core/src/main/java/convex/core/data/ALongBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,6 @@ public final ByteBuffer getByteBuffer() {
return toFlatBlob().getByteBuffer();
}

@Override
protected final long calcMemorySize() {
// always embedded and no child Refs, so memory size == 0
return 0;
}

@Override
public long hexMatch(ABlobLike<?> b, long start, long length) {
for (int i=0; i<length; i++) {
Expand Down Expand Up @@ -157,6 +151,12 @@ public final boolean isEmbedded() {
// Always embedded
return true;
}

@Override
protected final long calcMemorySize() {
// always embedded and no child Refs, so memory size == 0
return 0;
}

@Override
public boolean isChunkPacked() {
Expand Down
6 changes: 6 additions & 0 deletions convex-core/src/main/java/convex/core/data/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ public CVMLong get(long i) {
public boolean isCanonical() {
return true;
}

@Override
protected long calcMemorySize() {
// always embedded and no child Refs, so memory size == 0
return 0;
}

@Override
public boolean isCVMValue() {
Expand Down
4 changes: 3 additions & 1 deletion convex-core/src/main/java/convex/core/data/Cells.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ public static ABlobLike<CVMLong> getEncoding(ACell a) {
public static long storageSize(ACell a) {
if (a==null) return 1;
long memSize=a.getMemorySize();
if (a.isEmbedded()) memSize+=a.getEncodingLength();
if (a.isEmbedded()) {
memSize+=a.getEncodingLength();
}
return memSize;
}

Expand Down
4 changes: 2 additions & 2 deletions convex-core/src/main/java/convex/core/data/Hash.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ public Blob toCanonical() {

@Override
public int estimatedEncodingSize() {
// tag plus raw data
return 1 + LENGTH;
// tag plus length plis raw data
return 2 + LENGTH;
}

@Override
Expand Down
8 changes: 7 additions & 1 deletion convex-core/src/main/java/convex/core/lang/impl/CoreFn.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,16 @@ public void validateCell() throws InvalidDataException {

@Override
public boolean isEmbedded() {
// embed core functions, since they are the same size as small symbols
// core functions are always small embedded values
return true;
}

@Override
protected final long calcMemorySize() {
// always embedded and no child Refs, so memory size == 0
return 0;
}

@Override
public boolean equals(ACell o) {
// This is OK since these are guaranteed to be singleton instances!
Expand Down

0 comments on commit 08ddaa1

Please sign in to comment.