Skip to content

Commit

Permalink
Slight performance improvements for function invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Jan 1, 2025
1 parent c96ee6e commit 2dcd50f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
5 changes: 3 additions & 2 deletions convex-core/src/main/java/convex/core/cvm/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,8 @@ public Context updateBindings(ACell bindingForm, Object args) {
if (bindingForm instanceof Symbol) {
Symbol sym=(Symbol)bindingForm;
if (sym.equals(Symbols.UNDERSCORE)) return ctx;
// TODO: confirm must be an ACell at this point?
// args must be an ACell at this point, since we must have descended at least one level of binding from a
// function invocation on ACell[]
return withLocalBindings(localBindings.conj((ACell)args));
} else if (bindingForm instanceof AVector) {
AVector<ACell> v=(AVector<ACell>)bindingForm;
Expand All @@ -1083,7 +1084,7 @@ public Context updateBindings(ACell bindingForm, Object args) {
// bind variadic form at position i+1 to all args except nLeft
long consumeCount=(argCount-i)-nLeft;
if (consumeCount<0) return ctx.withArityError("Insufficient arguments to allow variadic binding");
AVector<ACell> rest=RT.vec(args).slice(i,i+consumeCount); // TODO: cost of this?
AVector<ACell> rest=RT.vec(args,i,i+consumeCount);
ctx= ctx.updateBindings(v.get(i+1), rest);
if(ctx.isExceptional()) return ctx;

Expand Down
8 changes: 8 additions & 0 deletions convex-core/src/main/java/convex/core/data/VectorArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ private VectorArray(ACell[] data, long start, long count) {
public static <T extends ACell> VectorArray<T> wrap(ACell[] arr) {
return new VectorArray<T>(arr,0,arr.length);
}

public static <T extends ACell> VectorArray<T> wrap(ACell[] elements, long start, long end) {
long n=end-start;
if (n<0) throw new IllegalArgumentException("end before start index");
return new VectorArray<T>(elements,start,n);
}

public static <T extends ACell> VectorArray<T> of(Object ... os) {
int n=os.length;
Expand Down Expand Up @@ -308,4 +314,6 @@ public AVector<T> dissocAt(long i) {

return wrap(cells);
}


}
5 changes: 3 additions & 2 deletions convex-core/src/main/java/convex/core/data/Vectors.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ public static <T extends ACell> AVector<T> create(ACell... elements) {
* @return New vector wrapping the specified elements
*/
public static <T extends ACell> AVector<T> wrap(ACell[] elements) {
if (elements.length==0) return empty();
return VectorArray.wrap(elements);
int n=elements.length;
if (n==0) return empty();
return VectorArray.wrap(elements,0,n);
}

/**
Expand Down
26 changes: 24 additions & 2 deletions convex-core/src/main/java/convex/core/lang/RT.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import convex.core.data.Sets;
import convex.core.data.Strings;
import convex.core.data.Symbol;
import convex.core.data.VectorArray;
import convex.core.data.Vectors;
import convex.core.data.prim.AInteger;
import convex.core.data.prim.ANumeric;
Expand Down Expand Up @@ -688,9 +689,10 @@ private static double doubleValue(ACell a) {
}

/**
* Converts any data structure to a vector
* Converts any data structure to a vector. May wrap a Java array in VectorArray, so
* the object must be treated as immutable in order for this to be safe.
*
* @param o Object to attempt to convert to a Vector. May wrap a Java array in VectorArray
* @param o Object to attempt to convert to a Vector.
* @return AVector instance, or null if not convertible
*/
@SuppressWarnings("unchecked")
Expand All @@ -712,6 +714,25 @@ public static <T extends ACell> AVector<T> vec(Object o) {

return null;
}

/**
* Converts any data structure to a vector. May wrap a Java array in VectorArray, so
* the object must be treated as immutable in order for this to be safe.
*
* @param o Object to attempt to convert to a Vector.
* @param start start index to slice
* @end end index to slice
* @return AVector instance, or null if not convertible
*/
@SuppressWarnings("unchecked")
public static <T extends ACell> AVector<T> vec(Object o, long start, long end) {
if (o instanceof ACell[]) {
ACell[] arr = (ACell[])o;
return VectorArray.wrap(arr,start,end);
}

return (AVector<T>) vec(o).slice(start, end);
}

/**
* Converts any countable data structure to a vector. Might be O(n)
Expand Down Expand Up @@ -1902,4 +1923,5 @@ public static long[] toLongArray(AVector<?> v) {




}

0 comments on commit 2dcd50f

Please sign in to comment.