Skip to content

Commit

Permalink
Added optimizations back
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Garn committed Sep 19, 2022
1 parent 430c3cd commit 651b36a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Expand Up @@ -100,7 +100,7 @@ public abstract static class JSIteratorFromNode extends JSBuiltinNode {
@Child private GetIteratorDirectNode getIteratorDirectNode;
@Child private OrdinaryHasInstanceNode ordinaryHasInstanceNode;

private ConditionProfile usingIteratorProfile = ConditionProfile.createBinaryProfile();
private final ConditionProfile usingIteratorProfile = ConditionProfile.createBinaryProfile();

public JSIteratorFromNode(JSContext context, JSBuiltin builtin) {
super(context, builtin);
Expand Down
Expand Up @@ -42,6 +42,7 @@

import java.util.Set;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Executed;
import com.oracle.truffle.api.dsl.Specialization;
Expand All @@ -62,6 +63,10 @@ public abstract class IteratorToArrayNode extends JavaScriptNode {
@Child @Executed JavaScriptNode iteratorNode;
@Child private IteratorGetNextValueNode iteratorStepNode;

@CompilerDirectives.CompilationFinal private int capacity = 0;
@CompilerDirectives.CompilationFinal private boolean first = true;


protected IteratorToArrayNode(JSContext context, JavaScriptNode iteratorNode, IteratorGetNextValueNode iteratorStepNode) {
this.context = context;
this.iteratorNode = iteratorNode;
Expand All @@ -75,12 +80,24 @@ public static IteratorToArrayNode create(JSContext context, JavaScriptNode itera

@Specialization(guards = "!iteratorRecord.isDone()")
protected Object doIterator(VirtualFrame frame, IteratorRecord iteratorRecord,
@Cached BranchProfile firstGrowProfile,
@Cached BranchProfile growProfile) {
SimpleArrayList<Object> elements = new SimpleArrayList<>();
SimpleArrayList<Object> elements = new SimpleArrayList<>(capacity);
Object value;
while ((value = iteratorStepNode.execute(frame, iteratorRecord)) != null) {
elements.add(value, growProfile);
elements.add(value, first ? firstGrowProfile : growProfile);
}

if (CompilerDirectives.inInterpreter()) {
if (first) {
capacity = elements.size();
first = false;
} else if (capacity != elements.size()) {
//Capacity is changing even though we are still in interpreter. Assume fluctuating capacity values.
capacity = 0;
}
}

return JSArray.createZeroBasedObjectArray(context, getRealm(), elements.toArray());
}

Expand Down

0 comments on commit 651b36a

Please sign in to comment.