Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[experimental] Optimize memory consumption (take 2) #718

Merged
merged 52 commits into from May 23, 2023

Conversation

Robbepop
Copy link
Collaborator

This PR is based on #716.

It tries to achieve a net win by taking a compromise approach between the current state in master and the aforementioned PR.
The compromise reduces the size_of of Instruction to 8 bytes instead of 4. This allows to avoid intx types in most if not all places which is important since it seems that usage of those custom types caused a lot of performance regressions.

This optimization makes both, drop and keep, values use only 12 bits each, restricting their range of valid numbers to just 0-4095. While low, these numbers should suffice for practical use cases. If practical use cases arise that exceed these limitations we might think about new or better encodings.
This makes more sense with the planned introduction of InstructionWord as a base unit of instruction encoding.
We are going to need this for the new encoding of memory load and store as well as constant value instructions.
These are more (space-)efficient alternatives to more general constant value instructions for small integer values.
This new ConstRef instruction replaces the old Const instruction. The advantage of ConstRef is that it is possible to encode it in 3 bytes. This is a trade off since it is probably slower at execution time. To circumvent unnecessary slowdowns in common cases we introduced I32Const24 and I64Const24 instructions earlier.
@paritytech-cicd-pr
Copy link

paritytech-cicd-pr commented May 23, 2023

BENCHMARKS

NATIVEWASMTIME
BENCHMARKMASTERPRDIFFMASTERPRDIFFWASMTIME OVERHEAD
execute/
bare_call_0
1.51ms 1.57ms 🔴 4.71% 1.23ms 1.05ms 🟢 -14.14% 🟢 -33%
execute/
bare_call_0/typed
1.15ms 1.25ms 🔴 9.59% 912.27µs 687.88µs 🟢 -24.42% 🟢 -45%
execute/
bare_call_1
1.58ms 1.61ms 🔴 2.07% 1.35ms 1.23ms 🟢 -8.79% 🟢 -24%
execute/
bare_call_16
2.47ms 2.54ms 🔴 2.37% 3.53ms 3.52ms 🟢 -0.07% 🟢 39%
execute/
bare_call_16/typed
1.50ms 1.55ms 🔴 3.06% 2.03ms 1.88ms 🟢 -7.48% 🟢 21%
execute/
bare_call_1/typed
1.21ms 1.30ms 🔴 6.89% 1.21ms 1.06ms 🟢 -12.65% 🟢 -18%
execute/
bare_call_4
1.75ms 1.75ms 🔴 0.65% 1.86ms 1.60ms 🟢 -14.26% 🟢 -9%
execute/
bare_call_4/typed
1.18ms 1.27ms 🔴 7.88% 1.23ms 987.44µs 🟢 -19.75% 🟢 -22%
execute/
br_table
1.39ms 1.35ms 🟢 -3.51% 1.38ms 1.22ms 🟢 -11.95% 🟢 -9%
execute/
count_until
635.71µs 621.36µs 🟢 -2.33% 1.16ms 1.33ms 🔴 14.78% 🔴 115%
execute/
factorial_iterative
334.12µs 317.87µs 🟢 -4.97% 525.25µs 557.10µs 🔴 6.26% 🟡 75%
execute/
factorial_recursive
591.62µs 589.63µs ⚪ -0.15% 871.21µs 860.15µs ⚪ -0.55% 🟢 46%
execute/
fibonacci_iter
1.47ms 1.40ms 🟢 -5.05% 2.80ms 3.05ms 🔴 9.26% 🔴 119%
execute/
fibonacci_rec
4.81ms 5.18ms 🔴 7.71% 7.87ms 7.81ms ⚪ -1.59% 🟡 51%
execute/
fibonacci_tail
1.17ms 1.11ms 🟢 -4.73% 1.75ms 1.92ms 🔴 9.47% 🟡 73%
execute/
global_bump
840.40µs 741.09µs 🟢 -11.45% 1.45ms 1.86ms 🔴 28.01% 🔴 151%
execute/
global_const
749.33µs 659.15µs 🟢 -13.13% 1.24ms 1.36ms 🔴 9.36% 🔴 107%
execute/
host_calls
35.33µs 39.62µs 🔴 6.92% 48.01µs 40.41µs 🟢 -15.75% 🟢 2%
execute/
memory_fill
1.20ms 1.14ms 🟢 -4.73% 2.42ms 2.47ms 🔴 2.43% 🔴 116%
execute/
memory_sum
1.18ms 1.13ms 🟢 -4.31% 2.35ms 2.45ms 🔴 4.00% 🔴 116%
execute/
memory_vec_add
2.32ms 2.23ms 🟢 -3.91% 4.44ms 4.80ms 🔴 8.23% 🔴 115%
execute/
recursive_is_even
1.04ms 1.01ms 🟢 -2.62% 1.62ms 1.62ms ⚪ -0.36% 🟡 59%
execute/
recursive_ok
122.14µs 120.09µs 🟢 -1.68% 191.60µs 194.76µs 🔴 1.72% 🟡 62%
execute/
recursive_scan
157.87µs 157.26µs ⚪ -0.34% 245.00µs 242.60µs ⚪ -0.76% 🟡 54%
execute/
recursive_trap
12.54µs 12.27µs 🟢 -2.14% 20.00µs 20.49µs 🔴 2.27% 🟡 67%
execute/
regex_redux
497.77µs 499.81µs ⚪ 0.42% 852.32µs 902.81µs 🔴 5.97% 🟡 81%
execute/
rev_complement
451.09µs 439.48µs 🟢 -2.55% 830.96µs 883.65µs 🔴 6.47% 🔴 101%
execute/
tiny_keccak
339.92µs 338.93µs ⚪ -0.36% 653.13µs 665.78µs ⚪ 1.70% 🟡 96%
execute/
trunc_f2i
772.88µs 740.25µs 🟢 -4.29% 1.60ms 1.65ms 🔴 3.42% 🔴 123%
instantiate/
wasm_kernel
53.09µs 55.17µs 🔴 4.80% 54.80µs 58.28µs 🔴 7.24% 🟢 6%
translate/
erc1155
235.98µs 196.84µs 🟢 -16.88% 457.45µs 433.40µs 🟢 -4.99% 🔴 120%
translate/
erc20
114.16µs 96.75µs 🟢 -15.07% 216.02µs 207.10µs 🟢 -4.55% 🔴 114%
translate/
erc721
163.18µs 135.30µs 🟢 -17.15% 316.56µs 303.01µs 🟢 -4.18% 🔴 124%
translate/
spidermonkey
0.00ns 60.56ms 🟢 -25.50% 0.00ns 0.00ns 🟢 -4.94% 🟢 -100%
translate/
wasm_kernel
4.10ms 3.43ms 🟢 -16.84% 8.16ms 7.75ms 🟢 -5.08% 🔴 126%

Link to pipeline

@codecov-commenter
Copy link

codecov-commenter commented May 23, 2023

Codecov Report

Merging #718 (3b83ca1) into master (23d8d8c) will decrease coverage by 0.86%.
The diff coverage is 81.08%.

@@            Coverage Diff             @@
##           master     #718      +/-   ##
==========================================
- Coverage   79.87%   79.02%   -0.86%     
==========================================
  Files         101      102       +1     
  Lines        8479     8682     +203     
==========================================
+ Hits         6773     6861      +88     
- Misses       1706     1821     +115     
Impacted Files Coverage Δ
crates/wasmi/src/engine/func_builder/error.rs 6.89% <0.00%> (-5.61%) ⬇️
crates/wasmi/src/func/func_type.rs 88.40% <0.00%> (-5.45%) ⬇️
crates/wasmi/src/engine/mod.rs 77.29% <46.15%> (-3.74%) ⬇️
crates/wasmi/src/engine/executor.rs 78.50% <62.61%> (-2.49%) ⬇️
crates/wasmi/src/engine/bytecode/utils.rs 67.02% <65.71%> (-9.04%) ⬇️
...ates/wasmi/src/engine/func_builder/inst_builder.rs 80.64% <73.91%> (-3.36%) ⬇️
crates/wasmi/src/engine/const_pool.rs 81.81% <81.81%> (ø)
crates/wasmi/src/engine/func_builder/translator.rs 86.63% <82.84%> (-2.06%) ⬇️
crates/wasmi/src/engine/bytecode/mod.rs 94.11% <100.00%> (+7.45%) ⬆️
crates/wasmi/src/engine/bytecode/tests.rs 100.00% <100.00%> (ø)
... and 7 more

... and 15 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@Robbepop Robbepop marked this pull request as ready for review May 23, 2023 13:25
@Robbepop Robbepop merged commit fd0bc97 into master May 23, 2023
17 checks passed
@Robbepop Robbepop deleted the rf-opt-mem-usage-v2 branch May 23, 2023 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants