Skip to content

Commit 0984608

Browse files
author
Nicolas Laurent
committed
use a simple PE-friendly stack implementation
1 parent af4f553 commit 0984608

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. This
3+
* code is released under a tri EPL/GPL/LGPL license. You can use it,
4+
* redistribute it and/or modify it under the terms of the:
5+
*
6+
* Eclipse Public License version 2.0, or
7+
* GNU General Public License version 2, or
8+
* GNU Lesser General Public License version 2.1.
9+
*/
10+
package org.truffleruby.collections;
11+
12+
import org.truffleruby.core.array.ArrayUtils;
13+
14+
/** Simplistic array stack implementation that will partial-evaluate nicely, unlike {@link java.util.ArrayDeque}. */
15+
@SuppressWarnings("unchecked")
16+
public class SimpleStack<T> {
17+
18+
Object[] storage;
19+
int index = -1;
20+
21+
public SimpleStack() {
22+
this(16);
23+
}
24+
25+
public SimpleStack(int length) {
26+
this.storage = new Object[length];
27+
}
28+
29+
public boolean isEmpty() {
30+
return index == -1;
31+
}
32+
33+
public void push(T value) {
34+
if (++index == storage.length) {
35+
storage = ArrayUtils.grow(storage, storage.length * 2);
36+
}
37+
storage[index] = value;
38+
}
39+
40+
public T peek() {
41+
return (T) storage[index];
42+
}
43+
44+
public T pop() {
45+
T out = (T) storage[index];
46+
storage[index] = null;
47+
--index;
48+
return out;
49+
}
50+
51+
public int size() {
52+
return index + 1;
53+
}
54+
}

src/main/java/org/truffleruby/core/array/ArrayNodes.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import static org.truffleruby.core.array.ArrayHelpers.setStoreAndSize;
1414
import static org.truffleruby.language.dispatch.DispatchNode.PUBLIC;
1515

16-
import java.util.ArrayDeque;
1716
import java.util.Arrays;
1817

1918
import com.oracle.truffle.api.dsl.CachedLanguage;
@@ -29,6 +28,7 @@
2928
import org.truffleruby.builtins.Primitive;
3029
import org.truffleruby.builtins.PrimitiveArrayArgumentsNode;
3130
import org.truffleruby.builtins.YieldingCoreMethodNode;
31+
import org.truffleruby.collections.SimpleStack;
3232
import org.truffleruby.core.CoreLibrary;
3333
import org.truffleruby.core.Hashing;
3434
import org.truffleruby.core.array.ArrayBuilderNode.BuilderState;
@@ -2364,7 +2364,6 @@ protected boolean flattenHelper(RubyArray array, RubyArray out, int maxLevels,
23642364

23652365
boolean modified = false;
23662366
final EconomicSet<RubyArray> visited = EconomicSet.create(Equivalence.IDENTITY);
2367-
// final HashSet<RubyArray> visited = new HashSet<>();
23682367
class Entry {
23692368
final RubyArray array;
23702369
final int index;
@@ -2374,7 +2373,7 @@ class Entry {
23742373
this.index = index;
23752374
}
23762375
}
2377-
final ArrayDeque<Entry> workStack = new ArrayDeque<>();
2376+
final SimpleStack<Entry> workStack = new SimpleStack<>();
23782377
workStack.push(new Entry(array, 0));
23792378

23802379
while (!workStack.isEmpty()) {

0 commit comments

Comments
 (0)