Commit 16e868e
authored
perf: speed up direct long rendering (#1087)
## Motivation
Direct long rendering in sjsonnet was already fairly optimized: it used
scratch buffers and digit-pair lookup tables instead of repeatedly
allocating `String`s on the hot renderer paths. The remaining cost was
the `/100` loop for every two digits, plus TOML exact-long doubles still
went through an intermediate string path and used a `math.round` guard
that saturates near `Long.MaxValue`.
This uses the jeaiii integer-formatting idea and jsoniter-scala v2.38.17
as references, but applies them conservatively for sjsonnet: the
blog-style `1441151881` `/100` reciprocal is only safe in a bounded
range, so the full `Long` path uses jsoniter-style `Math.multiplyHigh`
chunking by `1e8`/`1e16`, then digit-pair extraction for int-sized
chunks.
References:
- https://jk-jeon.github.io/posts/2022/02/jeaiii-algorithm/
- https://github.com/plokhotnyuk/jsoniter-scala/releases/tag/v2.38.17
which leverages this
## Modification
- Add a shared `FastLongRenderer` for ASCII `Long` rendering.
- Route both `BaseByteRenderer` and `BaseCharRenderer` through that
shared helper.
- Keep TOML exact-Long doubles on `StringBuilder.append(Long)` and fall
back to `RenderUtils.renderDouble` for non-exact Long values, avoiding
the previous `math.round(...).toLong` saturation case.
- Add renderer boundary/random regression coverage and a committed JMH
benchmark so the long-rendering comparison can be reproduced from the
PR.
## Result
The PR is squashed to one commit on top of `databricks/master`. The
shared code lives under `sjsonnet/src`, so the optimization is used by
JVM, Scala.js, Scala Native, and WASM builds. Local checks covered
formatting, JVM tests, compile coverage across JVM/JS/WASM/Native, and
renderer tests on JS/WASM/Native.
JMH command:
```bash
./mill bench.runJmh ".*LongRenderingBenchmark.*" -f 1 -wi 5 -i 8 -r 1s -w 1s
```
Local JMH on JDK 21, Apple Silicon, lower is better:
| Benchmark | upstream/master | this PR | Change |
| --- | ---: | ---: | ---: |
| `baseByteRendererLongs` | 40.280 ± 1.054 us/op | 32.865 ± 0.277 us/op
| 1.23x faster |
| `baseCharRendererLongs` | 48.081 ± 1.233 us/op | 45.992 ± 0.229 us/op
| 1.05x faster |
| `tomlRendererExactLongDoubles` | 81.820 ± 0.939 us/op | 43.046 ± 1.273
us/op | 1.90x faster |
The smaller char-renderer win is expected: the existing char path was
already optimized with a scratch buffer and digit-pair tables, so most
of the new gain comes from removing repeated division and sharing the
full-Long chunking logic.1 parent c5da414 commit 16e868e
7 files changed
Lines changed: 365 additions & 118 deletions
File tree
- bench/src/sjsonnet/bench
- sjsonnet
- src/sjsonnet
- test/src/sjsonnet
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| |||
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
169 | | - | |
| 169 | + | |
| 170 | + | |
170 | 171 | | |
171 | 172 | | |
172 | 173 | | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | 174 | | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
| 175 | + | |
| 176 | + | |
208 | 177 | | |
209 | 178 | | |
210 | | - | |
211 | | - | |
| 179 | + | |
| 180 | + | |
212 | 181 | | |
213 | 182 | | |
214 | 183 | | |
| |||
545 | 514 | | |
546 | 515 | | |
547 | 516 | | |
548 | | - | |
549 | | - | |
550 | | - | |
551 | | - | |
552 | | - | |
553 | | - | |
554 | | - | |
555 | | - | |
556 | | - | |
557 | | - | |
558 | | - | |
559 | | - | |
560 | | - | |
561 | | - | |
562 | | - | |
563 | | - | |
564 | | - | |
565 | 517 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | 18 | | |
33 | 19 | | |
34 | 20 | | |
| |||
54 | 40 | | |
55 | 41 | | |
56 | 42 | | |
57 | | - | |
| 43 | + | |
58 | 44 | | |
59 | 45 | | |
60 | 46 | | |
| |||
218 | 204 | | |
219 | 205 | | |
220 | 206 | | |
221 | | - | |
| 207 | + | |
| 208 | + | |
222 | 209 | | |
223 | 210 | | |
224 | 211 | | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | 212 | | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
259 | | - | |
| 213 | + | |
| 214 | + | |
260 | 215 | | |
261 | | - | |
262 | | - | |
263 | | - | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
264 | 225 | | |
265 | 226 | | |
266 | 227 | | |
| |||
0 commit comments