Commit 30a7f1f
authored
Web optimizations (#559)
As discussed in #541. This PR adds the following improvements:
- Caching of the global `Crypto` object.
- Detecting if our Wasm memory is based on a `SharedArrayBuffer`. If
not, we can copy bytes directly into our memory instead of having to go
through JS. This saves allocating the buffer in JS and copying the bytes
into Wasm memory. This is also the most common path. `SharedArrayBuffer`
requires `target_feature = "atomics"`, which is unstable and requires
Rust nightly. See
#559 (comment)
for full context.
- The atomic path only creates a sub-array when necessary, potentially
saving another FFI call.
- The atomic path will now allocate an `Uint8Array` with the minimum
amount of bytes necessary instead of a fixed size.
- The maximum chunk size for the non-atomic path and the maximum
`Uint8Array` size for the atomic paths have been increased to 65536
bytes: the maximum allowed buffer size for `Crypto.getRandomValues()`.
All in all this should give a performance improvement of ~5% to ~500%
depending on the amount of requested bytes and which path is taken. See
#559 (comment)
for some benchmark results.
This spawned a bunch of improvements and fixes in `wasm-bindgen` that
are being used here:
- wasm-bindgen/wasm-bindgen#4315
- wasm-bindgen/wasm-bindgen#4316
- wasm-bindgen/wasm-bindgen#4318
- wasm-bindgen/wasm-bindgen#4319
- wasm-bindgen/wasm-bindgen#43401 parent ae0c807 commit 30a7f1f
File tree
5 files changed
+75
-45
lines changed- .github/workflows
- src
- backends
5 files changed
+75
-45
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
229 | 229 | | |
230 | 230 | | |
231 | 231 | | |
232 | | - | |
| 232 | + | |
233 | 233 | | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
234 | 251 | | |
235 | 252 | | |
236 | | - | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
237 | 257 | | |
238 | 258 | | |
239 | 259 | | |
| |||
244 | 264 | | |
245 | 265 | | |
246 | 266 | | |
247 | | - | |
248 | | - | |
| 267 | + | |
| 268 | + | |
249 | 269 | | |
250 | 270 | | |
251 | 271 | | |
252 | | - | |
253 | | - | |
| 272 | + | |
| 273 | + | |
254 | 274 | | |
255 | 275 | | |
256 | 276 | | |
257 | | - | |
258 | | - | |
| 277 | + | |
| 278 | + | |
259 | 279 | | |
260 | 280 | | |
261 | 281 | | |
262 | | - | |
263 | | - | |
| 282 | + | |
| 283 | + | |
264 | 284 | | |
265 | 285 | | |
266 | 286 | | |
267 | | - | |
268 | | - | |
| 287 | + | |
| 288 | + | |
269 | 289 | | |
270 | 290 | | |
271 | 291 | | |
272 | | - | |
| 292 | + | |
273 | 293 | | |
274 | | - | |
| 294 | + | |
275 | 295 | | |
276 | 296 | | |
277 | 297 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
52 | 56 | | |
53 | 57 | | |
54 | 58 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
68 | | - | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | | - | |
| 10 | + | |
12 | 11 | | |
13 | | - | |
14 | | - | |
15 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
16 | 15 | | |
| 16 | + | |
17 | 17 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
23 | 22 | | |
| 23 | + | |
| 24 | + | |
24 | 25 | | |
| 26 | + | |
| 27 | + | |
25 | 28 | | |
26 | 29 | | |
27 | | - | |
28 | | - | |
29 | | - | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
30 | 37 | | |
31 | 38 | | |
32 | | - | |
| 39 | + | |
33 | 40 | | |
34 | 41 | | |
35 | | - | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
36 | 47 | | |
37 | | - | |
38 | | - | |
| 48 | + | |
| 49 | + | |
39 | 50 | | |
40 | 51 | | |
41 | 52 | | |
| |||
46 | 57 | | |
47 | 58 | | |
48 | 59 | | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | 60 | | |
57 | | - | |
58 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
59 | 67 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
43 | | - | |
44 | 42 | | |
45 | 43 | | |
46 | 44 | | |
| |||
155 | 153 | | |
156 | 154 | | |
157 | 155 | | |
158 | | - | |
159 | 156 | | |
160 | 157 | | |
161 | 158 | | |
| |||
0 commit comments