Skip to content

Commit 6c25fa7

Browse files
authored
perf(vm): optimize cached JUMPDEST analysis (#7067)
* perf(vm): store JUMPDEST positions in a bitmap Replace the HashSet of program counters. Cache key, size, and admission stay the same. * test(vm): cover JUMPDEST bitmap edge cases
1 parent bb7548c commit 6c25fa7

2 files changed

Lines changed: 140 additions & 6 deletions

File tree

‎actuator/src/main/java/org/tron/core/vm/program/ProgramPrecompile.java‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.tron.core.vm.program;
22

3-
import java.util.HashSet;
4-
import java.util.Set;
53
import lombok.extern.slf4j.Slf4j;
64
import org.tron.common.runtime.vm.DataWord;
75
import org.tron.core.vm.Op;
@@ -10,15 +8,22 @@
108
@Slf4j(topic = "VM")
119
public class ProgramPrecompile {
1210

13-
private final Set<Integer> jumpDest = new HashSet<>();
11+
// Bit 1 means compile() would have added this pc to the old HashSet.
12+
private final byte[] jumpDestBits;
13+
private final int codeLength;
14+
15+
private ProgramPrecompile(int codeLength) {
16+
this.codeLength = codeLength;
17+
this.jumpDestBits = new byte[(codeLength + 7) >>> 3];
18+
}
1419

1520
public static ProgramPrecompile compile(byte[] ops) {
16-
ProgramPrecompile ret = new ProgramPrecompile();
21+
ProgramPrecompile ret = new ProgramPrecompile(ops.length);
1722
for (int i = 0; i < ops.length; ++i) {
1823
int op = ops[i] & 0xff;
1924

2025
if (op == Op.JUMPDEST) {
21-
ret.jumpDest.add(i);
26+
ret.setJumpDest(i);
2227
}
2328

2429
if (op >= Op.PUSH1 && op <= Op.PUSH32) {
@@ -28,6 +33,10 @@ public static ProgramPrecompile compile(byte[] ops) {
2833
return ret;
2934
}
3035

36+
private void setJumpDest(int pc) {
37+
jumpDestBits[pc >>> 3] |= (byte) (1 << (pc & 7));
38+
}
39+
3140
public static byte[] getCode(byte[] ops) {
3241
for (int i = 0; i < ops.length; ++i) {
3342

@@ -54,6 +63,9 @@ public static byte[] getCode(byte[] ops) {
5463
}
5564

5665
public boolean hasJumpDest(int pc) {
57-
return jumpDest.contains(pc);
66+
if (pc < 0 || pc >= codeLength) {
67+
return false;
68+
}
69+
return (jumpDestBits[pc >>> 3] & (1 << (pc & 7))) != 0;
5870
}
5971
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package org.tron.core.vm.program;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.util.HashSet;
7+
import java.util.Random;
8+
import java.util.Set;
9+
import org.junit.Test;
10+
import org.tron.core.vm.Op;
11+
12+
public class JumpDestBitmapTest {
13+
14+
@Test
15+
public void bitmapMatchesTheOldJumpDestSet() {
16+
assertSame(new byte[0]);
17+
assertSame(new byte[] {op(Op.JUMPDEST)});
18+
assertSame(new byte[] {op(Op.PUSH1), op(Op.JUMPDEST), op(Op.JUMPDEST)});
19+
assertSame(new byte[] {op(Op.PUSH1), op(Op.JUMPDEST)});
20+
assertSame(truncatedPush32());
21+
assertSame(everyPushWidth());
22+
assertSame(allJumpDests(300));
23+
24+
ProgramPrecompile analysis = ProgramPrecompile.compile(new byte[] {op(Op.JUMPDEST)});
25+
assertEquals(false, analysis.hasJumpDest(-1));
26+
assertEquals(false, analysis.hasJumpDest(1));
27+
assertEquals(false, analysis.hasJumpDest(Integer.MIN_VALUE));
28+
}
29+
30+
@Test
31+
public void singleJumpDestDoesNotSetOtherBits() {
32+
for (int length = 1; length <= 65; length++) {
33+
for (int dest = 0; dest < length; dest++) {
34+
byte[] ops = new byte[length];
35+
ops[dest] = op(Op.JUMPDEST);
36+
ProgramPrecompile analysis = ProgramPrecompile.compile(ops);
37+
for (int pc = -1; pc <= length; pc++) {
38+
assertEquals("length " + length + " dest " + dest + " pc " + pc,
39+
pc == dest, analysis.hasJumpDest(pc));
40+
}
41+
}
42+
}
43+
}
44+
45+
@Test
46+
public void randomBytecodeMatchesTheOldJumpDestSet() {
47+
Random random = new Random(0x5b60617fL);
48+
for (int sample = 0; sample < 10000; sample++) {
49+
byte[] ops = new byte[random.nextInt(8193)];
50+
random.nextBytes(ops);
51+
// Exercise both arbitrary bytecode and bytecode with densely packed destinations.
52+
if ((sample & 1) != 0) {
53+
for (int pc = 0; pc < ops.length; pc++) {
54+
if (random.nextInt(4) == 0) {
55+
ops[pc] = op(Op.JUMPDEST);
56+
}
57+
}
58+
}
59+
assertSame(ops);
60+
}
61+
}
62+
63+
private static void assertSame(byte[] ops) {
64+
ProgramPrecompile analysis = ProgramPrecompile.compile(ops);
65+
Set<Integer> expected = oldJumpDests(ops);
66+
for (int pc = -2; pc <= ops.length + 2; pc++) {
67+
assertEquals(ops.length + " pc " + pc, expected.contains(pc), analysis.hasJumpDest(pc));
68+
}
69+
assertEquals(false, analysis.hasJumpDest(Integer.MIN_VALUE));
70+
assertEquals(false, analysis.hasJumpDest(Integer.MAX_VALUE));
71+
}
72+
73+
private static Set<Integer> oldJumpDests(byte[] ops) {
74+
Set<Integer> jumpDest = new HashSet<>();
75+
for (int i = 0; i < ops.length; ++i) {
76+
int op = ops[i] & 0xff;
77+
if (op == Op.JUMPDEST) {
78+
jumpDest.add(i);
79+
}
80+
if (op >= Op.PUSH1 && op <= Op.PUSH32) {
81+
i += op - Op.PUSH1 + 1;
82+
}
83+
}
84+
return jumpDest;
85+
}
86+
87+
private static byte[] truncatedPush32() {
88+
byte[] ops = new byte[10];
89+
ops[0] = op(Op.PUSH32);
90+
ops[3] = op(Op.JUMPDEST);
91+
return ops;
92+
}
93+
94+
private static byte[] everyPushWidth() {
95+
ByteArrayOutputStream out = new ByteArrayOutputStream();
96+
for (int width = 1; width <= 32; width++) {
97+
for (int align = 0; align < 8; align++) {
98+
for (int pad = 0; pad < align; pad++) {
99+
out.write(0);
100+
}
101+
out.write(op(Op.PUSH1 + width - 1));
102+
for (int n = 0; n < width; n++) {
103+
out.write(n == width - 1 ? op(Op.JUMPDEST) : 0);
104+
}
105+
out.write(op(Op.JUMPDEST));
106+
}
107+
}
108+
return out.toByteArray();
109+
}
110+
111+
private static byte op(int opcode) {
112+
return (byte) opcode;
113+
}
114+
115+
private static byte[] allJumpDests(int length) {
116+
byte[] ops = new byte[length];
117+
for (int i = 0; i < ops.length; i++) {
118+
ops[i] = op(Op.JUMPDEST);
119+
}
120+
return ops;
121+
}
122+
}

0 commit comments

Comments
 (0)