Skip to content

Commit 08ddaa1

Browse files
committed
Edits and improvements for memory sizing
1 parent d832343 commit 08ddaa1

File tree

7 files changed

+36
-14
lines changed

7 files changed

+36
-14
lines changed

convex-core/src/main/java/convex/core/data/AArrayBlob.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ public boolean isChunkPacked() {
156156
public boolean isFullyPacked() {
157157
return (count==Blob.CHUNK_LENGTH);
158158
}
159+
160+
@Override
161+
protected long calcMemorySize() {
162+
// fast path for small Blobs, never have child cells
163+
if (isEmbedded()) return 0;
164+
return super.calcMemorySize();
165+
}
159166

160167
@Override
161168
public final byte byteAt(long i) {

convex-core/src/main/java/convex/core/data/ACell.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ public String toString() {
227227
/**
228228
* Returns the CVM String representation of this Cell. Normally, this is as printed, but may be different for some types.
229229
*
230-
* MUST return null in O(1) time if the length of the CVM String would exceed limit.
230+
* SHOULD return null in O(1) time if the length of the CVM String can be proved to exceed the limit.
231+
* MUST complete in O(limit) time and space otherwise
231232
*
232233
* The String representation is intended to be a easy-to-read textual representation of the Cell's data content.
233234
* @param limit Limit of CVM String length in UTF-8 bytes
@@ -243,7 +244,7 @@ public AString toCVMString(long limit) {
243244
/**
244245
* Gets the cached blob representing this Cell's Encoding in binary format, if it exists.
245246
*
246-
* @return The cached blob for this cell, or null if not available.
247+
* @return The cached blob for this cell, or null if not yet available.
247248
*/
248249
public Blob cachedEncoding() {
249250
return encoding;
@@ -290,7 +291,7 @@ public int getEncodingLength() {
290291
* Gets the Memory Size of this Cell, computing it if required.
291292
*
292293
* The memory size is the total storage requirement for this cell. Embedded cells do not require storage for
293-
* their own encoding, but may require storage for nested non-embedded Refs.
294+
* their own encoding, but may require storage for nested branches.
294295
*
295296
* @return Memory Size of this Cell
296297
*/
@@ -306,7 +307,7 @@ public final long getMemorySize() {
306307
* Gets the Memory Size of a Cell, computing it if required.
307308
*
308309
* The memory size is the total storage requirement for this cell. Embedded cells do not require storage for
309-
* their own encoding, but may require storage for nested non-embedded Refs.
310+
* their own encoding, but may require storage for nested branches.
310311
*
311312
* @return Memory Size of this Cell
312313
*/

convex-core/src/main/java/convex/core/data/ALongBlob.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,6 @@ public final ByteBuffer getByteBuffer() {
9393
return toFlatBlob().getByteBuffer();
9494
}
9595

96-
@Override
97-
protected final long calcMemorySize() {
98-
// always embedded and no child Refs, so memory size == 0
99-
return 0;
100-
}
101-
10296
@Override
10397
public long hexMatch(ABlobLike<?> b, long start, long length) {
10498
for (int i=0; i<length; i++) {
@@ -157,6 +151,12 @@ public final boolean isEmbedded() {
157151
// Always embedded
158152
return true;
159153
}
154+
155+
@Override
156+
protected final long calcMemorySize() {
157+
// always embedded and no child Refs, so memory size == 0
158+
return 0;
159+
}
160160

161161
@Override
162162
public boolean isChunkPacked() {

convex-core/src/main/java/convex/core/data/Address.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,12 @@ public CVMLong get(long i) {
327327
public boolean isCanonical() {
328328
return true;
329329
}
330+
331+
@Override
332+
protected long calcMemorySize() {
333+
// always embedded and no child Refs, so memory size == 0
334+
return 0;
335+
}
330336

331337
@Override
332338
public boolean isCVMValue() {

convex-core/src/main/java/convex/core/data/Cells.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ public static ABlobLike<CVMLong> getEncoding(ACell a) {
174174
public static long storageSize(ACell a) {
175175
if (a==null) return 1;
176176
long memSize=a.getMemorySize();
177-
if (a.isEmbedded()) memSize+=a.getEncodingLength();
177+
if (a.isEmbedded()) {
178+
memSize+=a.getEncodingLength();
179+
}
178180
return memSize;
179181
}
180182

convex-core/src/main/java/convex/core/data/Hash.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ public Blob toCanonical() {
179179

180180
@Override
181181
public int estimatedEncodingSize() {
182-
// tag plus raw data
183-
return 1 + LENGTH;
182+
// tag plus length plis raw data
183+
return 2 + LENGTH;
184184
}
185185

186186
@Override

convex-core/src/main/java/convex/core/lang/impl/CoreFn.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,16 @@ public void validateCell() throws InvalidDataException {
132132

133133
@Override
134134
public boolean isEmbedded() {
135-
// embed core functions, since they are the same size as small symbols
135+
// core functions are always small embedded values
136136
return true;
137137
}
138138

139+
@Override
140+
protected final long calcMemorySize() {
141+
// always embedded and no child Refs, so memory size == 0
142+
return 0;
143+
}
144+
139145
@Override
140146
public boolean equals(ACell o) {
141147
// This is OK since these are guaranteed to be singleton instances!

0 commit comments

Comments
 (0)