Skip to content

Commit

Permalink
Extra Record type optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Oct 2, 2024
1 parent b1749a4 commit 042e500
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class ThreadCoordinationBenchmark {
o = INPUT.take();
OUTPUT.put(o);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/main/java/convex/core/data/AMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public final V get(Object key) {
* @return Value for the specified key, or the notFound value.
*/
@SuppressWarnings("unchecked")
public final V get(ACell key, ACell notFound) {
public V get(ACell key, ACell notFound) {
MapEntry<K, V> me = getEntry((K) key);
if (me == null) {
return (V) notFound;
Expand Down
15 changes: 10 additions & 5 deletions convex-core/src/main/java/convex/core/data/ARecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ protected ARecord toCanonical() {
return true;
}



/**
* Gets a vector of keys for this record
*
Expand Down Expand Up @@ -161,7 +159,6 @@ public final AMap<Keyword, ACell> dissoc(ACell key) {

@Override
public MapEntry<Keyword, ACell> getKeyRefEntry(Ref<ACell> keyRef) {
// TODO: could maybe be more efficient?
return getEntry(keyRef.getValue());
}

Expand Down Expand Up @@ -228,8 +225,16 @@ public boolean print(BlobBuilder sb, long limit) {

@Override
public MapEntry<Keyword, ACell> getEntry(ACell k) {
if (!containsKey(k)) return null;
return MapEntry.create((Keyword)k,get(k));
ACell v=get(k);
if ((v==null)&&!containsKey(k)) return null; //if null, need to check if key exists
return MapEntry.create((Keyword)k,v);
}

@Override
public ACell get(ACell key, ACell notFound) {
ACell v=get(key);
if ((v==null)&&!containsKey(key)) return notFound; //if null, need to check if key exists
return v;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public static AccountStatus read(Blob b, int pos) throws BadFormatException {
};
Address controller=null;
if ((included&HAS_CONTROLLER)!=0) {
controller=Format.read(b, epos); // TODO should be raw Address, save a byte?
controller=Format.read(b, epos);
epos+=controller.getEncodingLength();
}
AHashMap<Symbol, ACell> environment = null;
Expand Down
1 change: 0 additions & 1 deletion convex-core/src/main/java/convex/core/data/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ public final int getBytes(byte[] bs, int pos) {

@Override
public long longValue() {
// TODO Auto-generated method stub
return value;
}

Expand Down
3 changes: 3 additions & 0 deletions convex-core/src/main/java/convex/core/data/Cells.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.function.Consumer;

import convex.core.Result;
import convex.core.data.impl.DummyCell;
import convex.core.exceptions.ParseException;
import convex.core.store.AStore;
import convex.core.store.Stores;
Expand All @@ -27,6 +28,8 @@ public class Cells {
*/
public static final int MAX_BRANCH_COUNT = 68;

public static final ACell DUMMY = new DummyCell();

/**
* Equality method allowing for nulls
*
Expand Down
73 changes: 73 additions & 0 deletions convex-core/src/main/java/convex/core/data/impl/DummyCell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package convex.core.data.impl;

import convex.core.data.ACell;
import convex.core.data.Tag;
import convex.core.data.util.BlobBuilder;
import convex.core.exceptions.InvalidDataException;

/**
* A dummy cell implementation, not a valid CVM value
*/
public class DummyCell extends ACell {

@Override
public int estimatedEncodingSize() {
return 1;
}

@Override
public void validateCell() throws InvalidDataException {
throw new InvalidDataException("This is a dummy value",this);
}

@Override
protected byte getTag() {
return Tag.ILLEGAL;
}

@Override
public boolean equals(ACell a) {
return this==a;
}

@Override
public int encode(byte[] bs, int pos) {
bs[pos++]=Tag.ILLEGAL;
return pos;
}

@Override
protected int encodeRaw(byte[] bs, int pos) {
// Nothing to encode
return pos;
}

@Override
public boolean isCanonical() {
return true;
}

@Override
protected ACell toCanonical() {
return this;
}

@Override
public boolean isCVMValue() {
return false;
}

@Override
public boolean isDataValue() {
return false;
}

@Override
public boolean print(BlobBuilder sb, long limit) {
sb.append("DUMMY");
return sb.check(limit);
}



}

0 comments on commit 042e500

Please sign in to comment.