Skip to content

Commit

Permalink
Misc code enhancements / optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Oct 2, 2024
1 parent 1018f3e commit 80fd5f7
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 35 deletions.
10 changes: 5 additions & 5 deletions convex-core/src/main/java/convex/core/data/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public static ByteBuffer writeLength(ByteBuffer bb, int i) {
* @param <T> Type of referenced value
* @param b Blob containing a ref to read
* @param pos Position to read Ref from (should point to tag)
* @return Ref as read from ByteBuffer
* @return Ref as read from Blob at the specified position
* @throws BadFormatException If the data is badly formatted, or a non-embedded
* value is found where it should be a Ref.
*/
Expand All @@ -476,10 +476,10 @@ public static <T extends ACell> Ref<T> readRef(Blob b,int pos) throws BadFormatE

if (tag==Tag.NULL) return Ref.nil();

// Looks like an embedded cell, so try to read this
// We now expect a non-null embedded cell
T cell= Format.read(tag,b,pos);
if (!Format.isEmbedded(cell)) throw new BadFormatException("Non-embedded Cell found instead of ref: type = " +RT.getType(cell));
return Ref.get(cell);
if (!cell.isEmbedded()) throw new BadFormatException("Non-embedded cell found instead of ref: type = " +RT.getType(cell));
return cell.getRef();
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -529,7 +529,7 @@ private static ACell readCode(byte tag, Blob b, int pos) throws BadFormatExcepti
public static <T extends ACell> T read(Blob blob) throws BadFormatException {
long n=blob.count();
if (n<1) throw new BadFormatException("Attempt to decode from empty Blob");
byte tag = blob.byteAt(0);
byte tag = blob.byteAtUnchecked(0);
T result= read(tag,blob,0);
if (result==null) {
if (n!=1) throw new BadFormatException("Decode of nil value but blob size = "+n);
Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/main/java/convex/core/data/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ public static <K extends ABlobLike<?>, V extends ACell> Index<K, V> read(Blob b,
epos+=kr.getEncodingLength();
Ref<V> vr=Format.readRef(b,epos);
epos+=vr.getEncodingLength();
me=MapEntry.createRef(kr, vr);
me=MapEntry.fromRefs(kr, vr);

if (count == 1) {
// single entry map, doesn't need separate depth encoding
Expand Down
6 changes: 3 additions & 3 deletions convex-core/src/main/java/convex/core/data/MapEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public AType getType() {
}

@SuppressWarnings("unchecked")
public static <K extends ACell, V extends ACell> MapEntry<K, V> createRef(Ref<? extends K> keyRef, Ref<? extends V> valueRef) {
public static <K extends ACell, V extends ACell> MapEntry<K, V> fromRefs(Ref<? extends K> keyRef, Ref<? extends V> valueRef) {
// ensure we have a hash at least
return new MapEntry<K, V>((Ref<K>) keyRef, (Ref<V>) valueRef);
}
Expand All @@ -57,7 +57,7 @@ public static <K extends ACell, V extends ACell> MapEntry<K, V> createRef(Ref<?
* @return New MapEntry instance
*/
public static <K extends ACell, V extends ACell> MapEntry<K, V> create(K key, V value) {
return createRef(Ref.get(key), Ref.get(value));
return fromRefs(Ref.get(key), Ref.get(value));
}

/**
Expand All @@ -75,7 +75,7 @@ public static <K extends ACell, V extends ACell> MapEntry<K, V> of(Object key, O
@SuppressWarnings({ "rawtypes", "unchecked" })
public static MapEntry convertOrNull(AVector v) {
if (v.count()!=2) return null;
return createRef(v.getElementRef(0),v.getElementRef(1));
return fromRefs(v.getElementRef(0),v.getElementRef(1));
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/main/java/convex/core/data/MapLeaf.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public static <K extends ACell, V extends ACell> MapLeaf<K, V> read(Blob b, int
epos+=kr.getEncodingLength();
Ref<V> vr=Format.readRef(b,epos);
epos+=vr.getEncodingLength();
items[i] = MapEntry.createRef(kr, vr);
items[i] = MapEntry.fromRefs(kr, vr);
}

if (!isValidOrder(items)) {
Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/main/java/convex/core/data/MapTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ protected MapTree<K, V> assocRef(Ref<K> keyRef, V value, int shift) {
int i = Bits.indexForDigit(digit, mask);
if (i < 0) {
// location not present, need to insert new child
AHashMap<K, V> newChild = MapLeaf.create(MapEntry.createRef(keyRef, Ref.get(value)));
AHashMap<K, V> newChild = MapLeaf.create(MapEntry.fromRefs(keyRef, Ref.get(value)));
return insertChild(digit, newChild.getRef());
} else {
// child exists, so assoc in new ref at lower shift level
Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/main/java/convex/core/lang/RT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,7 @@ public static <K extends ACell, V extends ACell> MapEntry<K, V> ensureMapEntry(A
AVector<?> v = (AVector<?>) x;
if (v.count() != 2)
return null;
me = MapEntry.createRef(v.getRef(0), v.getRef(1));
me = MapEntry.fromRefs(v.getRef(0), v.getRef(1));
} else {
return null;
}
Expand Down
31 changes: 8 additions & 23 deletions convex-core/src/main/java/convex/core/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ public static String toHexString(short val) {
* @return Lowercase hex string
*/
public static String toHexString(byte value) {
StringBuilder sb = new StringBuilder(2);
sb.append(toHexChar((((int) value) & 0xF0) >>> 4));
sb.append(toHexChar(((int) value) & 0xF));
return sb.toString();
char[] chars=new char[2];
chars[0]=(toHexChar((((int) value) & 0xF0) >>> 4));
chars[1]=(toHexChar(((int) value) & 0xF));
return new String(chars);
}


Expand All @@ -104,26 +104,11 @@ public static String toHexString(byte value) {
* @return Hex string for the given long
*/
public static String toHexString(long x) {
StringBuffer sb = new StringBuffer(16);
char[] chars=new char[16];
for (int i = 15; i >= 0; i--) {
sb.append(toHexChar(((int) (x >> (4 * i))) & 0xF));
chars[i]=(toHexChar(((int) (x >> (4 * i))) & 0xF));
}
return sb.toString();
}

/**
* Converts a hex string to a friendly version ( first x chars).
* SECURITY; do not use this output for any comparison.
*
* @param hexString String to show in friendly format.
* @param size Number of hex chars to output.
* @return Hex String
*/
public static String toFriendlyHexString(String hexString, int size) {
String cleanHexString = hexString.replaceAll("^0[Xx]", "");
String result = cleanHexString.substring(0, size);
// + ".." + cleanHexString.substring(cleanHexString.length() - size);
return result;
return new String(chars);
}

/**
Expand All @@ -144,7 +129,7 @@ public static int readInt(byte[] data, int offset) {

/**
* Reads an int from a specified location in a byte array. Assumes 4-byte
* big-endian representation. Assumes zeros beyong end of array
* big-endian representation. Assumes zeros beyond end of array
*
* @param data Byte array from which to read the 4-byte int representation
* @param offset Offset into byte array to read
Expand Down

0 comments on commit 80fd5f7

Please sign in to comment.