[Swift-commit] r6334 - branches/faster/src/org/griphyn/vdl/karajan/lib
hategan at ci.uchicago.edu
hategan at ci.uchicago.edu
Wed Mar 6 01:23:15 CST 2013
Author: hategan
Date: 2013-03-06 01:23:14 -0600 (Wed, 06 Mar 2013)
New Revision: 6334
Modified:
branches/faster/src/org/griphyn/vdl/karajan/lib/ExpandArguments.java
Log:
avoid creating arrays when not necessary
Modified: branches/faster/src/org/griphyn/vdl/karajan/lib/ExpandArguments.java
===================================================================
--- branches/faster/src/org/griphyn/vdl/karajan/lib/ExpandArguments.java 2013-03-06 07:22:47 UTC (rev 6333)
+++ branches/faster/src/org/griphyn/vdl/karajan/lib/ExpandArguments.java 2013-03-06 07:23:14 UTC (rev 6334)
@@ -24,6 +24,7 @@
import java.util.TreeMap;
import k.rt.Channel;
+import k.rt.ExecutionException;
import k.rt.Stack;
import org.apache.log4j.Logger;
@@ -44,40 +45,60 @@
}
public Object function(Stack stack) {
- ArrayList<DSHandle> l = new ArrayList<DSHandle>();
- Channel<Object> items = c_vargs.get(stack);
- for (Object item : items) {
- if(!(item instanceof DSHandle)) {
- throw new RuntimeException("Cannot handle argument implemented by " + item.getClass());
- }
-
- if (item instanceof ArrayDataNode) {
- ArrayDataNode array = (ArrayDataNode) item;
- Map<Comparable<?>, DSHandle> m = array.getArrayValue();
- SortedMap<Comparable<?>, DSHandle> sorted = new TreeMap<Comparable<?>, DSHandle>(new PathElementComparator());
- sorted.putAll(m);
- l.addAll(m.values());
- }
- else {
- l.add((DSHandle) item);
- }
- // TODO this does not correctly handle structs or
- // externals - at the moment, probably neither of
- // those should be usable as a string. It also
- // does not handle nested arrays. However, none of
- // those should get here in normal operation due
- // to static type-checking
- }
- return l;
+ Channel<Object> items = c_vargs.get(stack);
+ // prefer lower memory usage over speed here
+ // although the penalty is only there if there are arrays in the arguments
+ if (hasArrays(items)) {
+ return expandArrays(items);
+ }
+ else {
+ return items.getAll();
+ }
}
- class StringsAsIntegersComparator implements Comparator<Object> {
+ private Object expandArrays(Channel<Object> items) {
+ ArrayList<DSHandle> l = new ArrayList<DSHandle>();
+ for (Object item : items) {
+ if (item instanceof ArrayDataNode) {
+ ArrayDataNode array = (ArrayDataNode) item;
+ Map<Comparable<?>, DSHandle> m = array.getArrayValue();
+ SortedMap<Comparable<?>, DSHandle> sorted = new TreeMap<Comparable<?>, DSHandle>(new PathElementComparator());
+ sorted.putAll(m);
+ l.addAll(m.values());
+ }
+ else {
+ l.add((DSHandle) item);
+ }
+ // TODO this does not correctly handle structs or
+ // externals - at the moment, probably neither of
+ // those should be usable as a string. It also
+ // does not handle nested arrays. However, none of
+ // those should get here in normal operation due
+ // to static type-checking
+ }
+ return l;
+ }
+
+ private boolean hasArrays(Channel<Object> items) {
+ boolean arraySeen = false;
+ for (Object item : items) {
+ if(!(item instanceof DSHandle)) {
+ throw new ExecutionException(this, "Cannot handle argument implemented by " + item.getClass());
+ }
+
+ if (item instanceof ArrayDataNode) {
+ arraySeen = true;
+ }
+ }
+ return arraySeen;
+ }
+
+ class StringsAsIntegersComparator implements Comparator<Object> {
public int compare(Object l, Object r) {
Integer lnum = new Integer((String)l);
Integer rnum = new Integer((String)r);
return lnum.compareTo(rnum);
}
}
-
}
More information about the Swift-commit
mailing list