Skip to content

Commit 734b382

Browse files
committed
Optimizing Stack (merge all var stack arrays into single one)
1 parent f76f7a7 commit 734b382

File tree

4 files changed

+33
-49
lines changed

4 files changed

+33
-49
lines changed

net.tascalate.javaflow.api/src/main/java/org/apache/commons/javaflow/core/Stack.java

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,23 @@ public class Stack implements Serializable {
3636
private static final Log log = LogFactory.getLog(Stack.class);
3737
private static final long serialVersionUID = 3L;
3838

39+
private int[] istack;
3940
private long[] pstack;
4041
private Object[] ostack;
4142
private Object[] rstack;
4243
private int iTop, fTop, dTop, lTop, oTop, rTop;
43-
private int ret;
4444
protected Runnable runnable;
4545

4646
Stack(Runnable pRunnable) {
47-
ret = -1;
48-
pstack = new long[16];
47+
istack = new int[8];
48+
pstack = new long[8];
4949
ostack = new Object[8];
5050
rstack = new Object[4];
5151
runnable = pRunnable;
5252
}
5353

5454
Stack(final Stack pParent) {
55+
istack = new int[pParent.istack.length];
5556
pstack = new long[pParent.pstack.length];
5657
ostack = new Object[pParent.ostack.length];
5758
rstack = new Object[pParent.rstack.length];
@@ -61,10 +62,10 @@ public class Stack implements Serializable {
6162
lTop = pParent.lTop;
6263
oTop = pParent.oTop;
6364
rTop = pParent.rTop;
65+
System.arraycopy(pParent.istack, 0, istack, 0, iTop);
6466
System.arraycopy(pParent.pstack, 0, pstack, 0, dTop + fTop + lTop);
6567
System.arraycopy(pParent.ostack, 0, ostack, 0, oTop);
6668
System.arraycopy(pParent.rstack, 0, rstack, 0, rTop);
67-
ret = pParent.ret;
6869
runnable = pParent.runnable;
6970
}
7071

@@ -84,30 +85,6 @@ public final double popDouble() {
8485
}
8586
return d;
8687
}
87-
88-
public final int popRet() {
89-
if (ret < 0) {
90-
throw new EmptyStackException("pop ret");
91-
}
92-
if (log.isDebugEnabled()) {
93-
log.debug("pop ret " + ret + " " + getStats());
94-
}
95-
final int result = ret;
96-
ret = -1;
97-
return result;
98-
}
99-
100-
public final void pushRet(int idx) {
101-
if (log.isDebugEnabled()) {
102-
log.debug("push ret " + idx + " " + getStats());
103-
}
104-
105-
if (ret != -1) {
106-
throw new IllegalStateException("ret is already set");
107-
}
108-
109-
ret = idx;
110-
}
11188

11289
public final boolean hasFloat() {
11390
return fTop > 0;
@@ -152,8 +129,7 @@ public final int popInt() {
152129
throw new EmptyStackException("pop int");
153130
}
154131

155-
final int i = (int)popPrimitive();
156-
--iTop;
132+
final int i = istack[--iTop];
157133
if (log.isDebugEnabled()) {
158134
log.debug("pop int " + i + " " + getStats());
159135
}
@@ -232,9 +208,12 @@ public final void pushInt(int i) {
232208
if (log.isDebugEnabled()) {
233209
log.debug("push int " + i + " " + getStats());
234210
}
235-
ensurePrimitivesStackSize();
236-
pushPrimitive(i);
237-
iTop++;
211+
if (iTop == istack.length) {
212+
int[] hlp = new int[Math.max(8, istack.length * 2)];
213+
System.arraycopy(istack, 0, hlp, 0, istack.length);
214+
istack = hlp;
215+
}
216+
istack[iTop++] = i;
238217
}
239218

240219
public final void pushObject(Object o) {
@@ -327,29 +306,31 @@ public String toString() {
327306
}
328307

329308
private final void pushPrimitive(long value) {
330-
pstack[dTop + fTop + lTop + iTop] = value;
309+
pstack[dTop + fTop + lTop] = value;
331310
}
332311

333312
private final long popPrimitive() {
334-
return pstack[dTop + fTop + lTop + iTop];
313+
return pstack[dTop + fTop + lTop];
335314
}
336315

337316
private final void ensurePrimitivesStackSize() {
338-
if (dTop + fTop + lTop + iTop == pstack.length) {
317+
if (dTop + fTop + lTop == pstack.length) {
339318
long[] hlp = new long[Math.max(8, pstack.length * 2)];
340319
System.arraycopy(pstack, 0, hlp, 0, pstack.length);
341320
pstack = hlp;
342321
}
343322
}
344323

345324
private void writeObject(ObjectOutputStream s) throws IOException {
346-
s.writeInt(ret);
325+
s.writeInt(iTop);
326+
for (int i = 0; i < iTop; i++) {
327+
s.writeInt(istack[i]);
328+
}
347329

348330
s.writeInt(dTop);
349331
s.writeInt(fTop);
350332
s.writeInt(lTop);
351-
s.writeInt(iTop);
352-
int pTop = dTop + fTop + lTop + iTop;
333+
int pTop = dTop + fTop + lTop;
353334
for (int i = 0; i < pTop; i++) {
354335
s.writeLong(pstack[i]);
355336
}
@@ -368,13 +349,16 @@ private void writeObject(ObjectOutputStream s) throws IOException {
368349
}
369350

370351
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
371-
ret = s.readInt();
352+
iTop = s.readInt();
353+
istack = new int[iTop];
354+
for (int i = 0; i < iTop; i++) {
355+
istack[i] = s.readInt();
356+
}
372357

373358
dTop = s.readInt();
374-
dTop = s.readInt();
359+
fTop = s.readInt();
375360
lTop = s.readInt();
376-
iTop = s.readInt();
377-
int pTop = dTop + fTop + lTop + iTop;
361+
int pTop = dTop + fTop + lTop;
378362
pstack = new long[pTop];
379363
for (int i = 0; i < pTop; i++) {
380364
pstack[i] = s.readLong();

net.tascalate.javaflow.providers.asm3/src/main/java/org/apache/commons/javaflow/providers/asm3/ContinuableMethodVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void visitCode() {
100100

101101
mv.visitVarInsn(ALOAD, stackRecorderVar);
102102
// PC: stackRecorder.popInt();
103-
mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, "popRet", "()I");
103+
mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, "popInt", "()I");
104104
mv.visitTableSwitchInsn(0, fsize - 1, l0, restoreLabels);
105105

106106
// switch cases
@@ -300,7 +300,7 @@ public void visitMethodInsn(int opcode, String owner, String name, String desc)
300300
mv.visitInsn(ICONST_0 + currentIndex);
301301
else
302302
mv.visitIntInsn(SIPUSH, currentIndex);
303-
mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, "pushRet", "(I)V");
303+
mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, "pushInt", "(I)V");
304304

305305
if (currentFrame instanceof MonitoringFrame) {
306306
int[] monitoredLocals = ((MonitoringFrame) currentFrame).getMonitored();

net.tascalate.javaflow.providers.asm4/src/main/java/org/apache/commons/javaflow/providers/asm4/ContinuableMethodVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void visitCode() {
105105

106106
mv.visitVarInsn(ALOAD, stackRecorderVar);
107107
// PC: stackRecorder.popInt();
108-
mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, "popRet", "()I");
108+
mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, "popInt", "()I");
109109
mv.visitTableSwitchInsn(0, fsize - 1, l0, restoreLabels);
110110

111111
// switch cases
@@ -313,7 +313,7 @@ public void visitCall(int opcode, String desc) {
313313
mv.visitInsn(ICONST_0 + currentIndex);
314314
else
315315
mv.visitIntInsn(SIPUSH, currentIndex);
316-
mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, "pushRet", "(I)V");
316+
mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, "pushInt", "(I)V");
317317

318318
if (currentFrame instanceof MonitoringFrame) {
319319
int[] monitoredLocals = ((MonitoringFrame) currentFrame).getMonitored();

net.tascalate.javaflow.providers.asm5/src/main/java/org/apache/commons/javaflow/providers/asm5/ContinuableMethodVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void visitCode() {
105105

106106
mv.visitVarInsn(ALOAD, stackRecorderVar);
107107
// PC: stackRecorder.popInt();
108-
mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, "popRet", "()I", false);
108+
mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, "popInt", "()I", false);
109109
mv.visitTableSwitchInsn(0, fsize - 1, l0, restoreLabels);
110110

111111
// switch cases
@@ -319,7 +319,7 @@ public void visitCall(int opcode, String desc) {
319319
mv.visitInsn(ICONST_0 + currentIndex);
320320
else
321321
mv.visitIntInsn(SIPUSH, currentIndex);
322-
mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, "pushRet", "(I)V", false);
322+
mv.visitMethodInsn(INVOKEVIRTUAL, STACK_RECORDER, "pushInt", "(I)V", false);
323323

324324
if (currentFrame instanceof MonitoringFrame) {
325325
int[] monitoredLocals = ((MonitoringFrame) currentFrame).getMonitored();

0 commit comments

Comments
 (0)