Repository navigation
Expand file tree
/
Copy pathFastLayoutBenchmark.java
More file actions
119 lines (100 loc) 路 3.82 KB
/
Copy pathFastLayoutBenchmark.java
File metadata and controls
119 lines (100 loc) 路 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package fastlayout.benchmark;
import fastlayout.*;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Thread)
@Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(0)
public class FastLayoutBenchmark {
@Param({"10", "100", "1000"})
private int elementCount;
private LinearLayout horizontalLayout;
private LinearLayout verticalLayout;
private List<Cell> cells;
private LayoutArena arena;
private Rect[] outRects;
private float[] outFloatBuffer;
private LayoutMeasure reusableMeasure;
@Setup(Level.Trial)
public void setup() {
horizontalLayout = LinearLayout.horizontal(Alignment.CENTER);
verticalLayout = LinearLayout.vertical(Alignment.STRETCH);
cells = new ArrayList<>(elementCount);
arena = new LayoutArena(elementCount);
outRects = new Rect[elementCount];
outFloatBuffer = new float[elementCount * 4];
reusableMeasure = new LayoutMeasure();
for (int i = 0; i < elementCount; i++) {
Cell c = new Cell(32, 32);
c.margin(2);
if (i % 5 == 0) {
c.weight = 1.0f; // some flex items
}
cells.add(c);
outRects[i] = new Rect();
}
// Preload LayoutArena for pure SoA benchmark
arena.load(cells);
}
@Benchmark
public void benchmarkMeasureZeroAlloc(Blackhole bh) {
horizontalLayout.measure(cells, 0, 0, 4, 8, 8, 8, 8, reusableMeasure);
bh.consume(reusableMeasure.width);
bh.consume(reusableMeasure.height);
}
@Benchmark
public void benchmarkMeasureArenaSoA(Blackhole bh) {
horizontalLayout.measure(arena, 0, elementCount, 0, 0, 4, 8, 8, 8, 8, reusableMeasure);
bh.consume(reusableMeasure.width);
bh.consume(reusableMeasure.height);
}
@Benchmark
public void benchmarkArrangeAoS_RectArray(Blackhole bh) {
horizontalLayout.arrange(cells, 1000, 50, 4, 8, 8, 8, 8, outRects);
bh.consume(outRects);
}
@Benchmark
public void benchmarkArrangeBuffer_FlatFloats(Blackhole bh) {
horizontalLayout.arrange(cells, 1000, 50, 4, 8, 8, 8, 8, outFloatBuffer, 0);
bh.consume(outFloatBuffer);
}
@Benchmark
public void benchmarkArrangeArena_PureSoA(Blackhole bh) {
horizontalLayout.arrange(arena, 0, elementCount, 1000, 50, 4, 8, 8, 8, 8, outFloatBuffer, 0);
bh.consume(outFloatBuffer);
}
@Benchmark
public void benchmarkVerticalArrangeAoS_RectArray(Blackhole bh) {
verticalLayout.arrange(cells, 200, 5000, 4, 8, 8, 8, 8, outRects);
bh.consume(outRects);
}
@Benchmark
public void benchmarkVerticalArrangeBuffer_FlatFloats(Blackhole bh) {
verticalLayout.arrange(cells, 200, 5000, 4, 8, 8, 8, 8, outFloatBuffer, 0);
bh.consume(outFloatBuffer);
}
@Benchmark
public void benchmarkVerticalArrangeArena_PureSoA(Blackhole bh) {
verticalLayout.arrange(arena, 0, elementCount, 200, 5000, 4, 8, 8, 8, 8, outFloatBuffer, 0);
bh.consume(outFloatBuffer);
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(FastLayoutBenchmark.class.getSimpleName())
.forks(0)
.warmupIterations(1)
.measurementIterations(2)
.build();
new Runner(opt).run();
}
}