From 115679e67523016057e01c6d4bbf9d8e0a756874 Mon Sep 17 00:00:00 2001 From: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Mon, 28 Aug 2023 14:13:12 +0200 Subject: [PATCH] bench: Add benchmarking for stack memory usage (#2617) --- bench/STACK_MEMORY.md | 307 +++++++++++++++++++++++++++++ tests/bench/bench.json | 267 +++++++++++++++++++++++++ tests/bench/scripts/sync.ts | 1 - tests/bench/scripts/utils.ts | 7 +- tests/bench/tests/binary-size.ts | 2 +- tests/bench/tests/compute-units.ts | 7 +- tests/bench/tests/stack-memory.ts | 64 ++++++ tests/bench/tsconfig.json | 2 +- 8 files changed, 647 insertions(+), 10 deletions(-) create mode 100644 bench/STACK_MEMORY.md create mode 100644 tests/bench/tests/stack-memory.ts diff --git a/bench/STACK_MEMORY.md b/bench/STACK_MEMORY.md new file mode 100644 index 0000000000..756bde7d6b --- /dev/null +++ b/bench/STACK_MEMORY.md @@ -0,0 +1,307 @@ +# Stack Memory + +All notable changes in stack memory usage will be documented in this file. + +The changes are calculated by comparing the current results with the last version's results. Increase in usage is shown with 🔴 and decrease is shown with 🟢. + +The programs and their tests are located in [/tests/bench](https://github.com/coral-xyz/anchor/tree/master/tests/bench). + +> **Note** +> Results documented in this file are autogenerated. Running the tests will update the current results when necessary, manually editing the results should be avoided. + +> **Warning** +> Results may vary depending on Solana version. + +## [Unreleased] + +Solana version: 1.16.0 + +| Instruction | Stack Memory | +/- | +| ------------------------------ | ------------ | --- | +| account_info1 | 328 | - | +| account_info2 | 376 | - | +| account_info4 | 560 | - | +| account_info8 | 728 | - | +| account_empty_init1 | 592 | - | +| account_empty_init2 | 560 | - | +| account_empty_init4 | 632 | - | +| account_empty_init8 | 824 | - | +| account_empty1 | 320 | - | +| account_empty2 | 368 | - | +| account_empty4 | 552 | - | +| account_empty8 | 728 | - | +| account_sized_init1 | 600 | - | +| account_sized_init2 | 552 | - | +| account_sized_init4 | 664 | - | +| account_sized_init8 | 888 | - | +| account_sized1 | 328 | - | +| account_sized2 | 392 | - | +| account_sized4 | 568 | - | +| account_sized8 | 792 | - | +| account_unsized_init1 | 624 | - | +| account_unsized_init2 | 584 | - | +| account_unsized_init4 | 728 | - | +| account_unsized_init8 | 1,016 | - | +| account_unsized1 | 344 | - | +| account_unsized2 | 456 | - | +| account_unsized4 | 632 | - | +| account_unsized8 | 920 | - | +| boxed_account_empty_init1 | 552 | - | +| boxed_account_empty_init2 | 400 | - | +| boxed_account_empty_init4 | 432 | - | +| boxed_account_empty_init8 | 496 | - | +| boxed_account_empty1 | 320 | - | +| boxed_account_empty2 | 320 | - | +| boxed_account_empty4 | 320 | - | +| boxed_account_empty8 | 336 | - | +| boxed_account_sized_init1 | 552 | - | +| boxed_account_sized_init2 | 400 | - | +| boxed_account_sized_init4 | 432 | - | +| boxed_account_sized_init8 | 496 | - | +| boxed_account_sized1 | 320 | - | +| boxed_account_sized2 | 320 | - | +| boxed_account_sized4 | 320 | - | +| boxed_account_sized8 | 336 | - | +| boxed_account_unsized_init1 | 552 | - | +| boxed_account_unsized_init2 | 400 | - | +| boxed_account_unsized_init4 | 432 | - | +| boxed_account_unsized_init8 | 496 | - | +| boxed_account_unsized1 | 320 | - | +| boxed_account_unsized2 | 320 | - | +| boxed_account_unsized4 | 320 | - | +| boxed_account_unsized8 | 336 | - | +| boxed_interface_account_mint1 | 320 | - | +| boxed_interface_account_mint2 | 320 | - | +| boxed_interface_account_mint4 | 320 | - | +| boxed_interface_account_mint8 | 336 | - | +| boxed_interface_account_token1 | 320 | - | +| boxed_interface_account_token2 | 320 | - | +| boxed_interface_account_token4 | 320 | - | +| boxed_interface_account_token8 | 336 | - | +| interface_account_mint1 | 504 | - | +| interface_account_mint2 | 680 | - | +| interface_account_mint4 | 1,016 | - | +| interface_account_mint8 | 1,688 | - | +| interface_account_token1 | 680 | - | +| interface_account_token2 | 856 | - | +| interface_account_token4 | 1,368 | - | +| interface1 | 320 | - | +| interface2 | 368 | - | +| interface4 | 552 | - | +| interface8 | 728 | - | +| program1 | 320 | - | +| program2 | 368 | - | +| program4 | 552 | - | +| program8 | 728 | - | +| signer1 | 328 | - | +| signer2 | 376 | - | +| signer4 | 560 | - | +| signer8 | 728 | - | +| system_account1 | 328 | - | +| system_account2 | 376 | - | +| system_account4 | 560 | - | +| system_account8 | 728 | - | +| unchecked_account1 | 328 | - | +| unchecked_account2 | 376 | - | +| unchecked_account4 | 560 | - | +| unchecked_account8 | 728 | - | + +### Notable changes + +--- + +## [0.28.0] + +Solana version: 1.16.0 + +| Instruction | Stack Memory | +/- | +| ------------------------------ | ------------ | ---------------------- | +| account_info1 | 328 | 🔴 **+80 (32.26%)** | +| account_info2 | 376 | 🟢 **-16 (4.08%)** | +| account_info4 | 560 | 🔴 **+48 (9.38%)** | +| account_info8 | 728 | 🟢 **-168 (18.75%)** | +| account_empty_init1 | 592 | 🔴 **+8 (1.37%)** | +| account_empty_init2 | 560 | 🔴 **+48 (9.38%)** | +| account_empty_init4 | 632 | 🟢 **-72 (10.23%)** | +| account_empty_init8 | 824 | 🟢 **-264 (24.26%)** | +| account_empty1 | 320 | 🔴 **+120 (60.00%)** | +| account_empty2 | 368 | 🔴 **+24 (6.98%)** | +| account_empty4 | 552 | 🔴 **+88 (18.97%)** | +| account_empty8 | 728 | 🟢 **-120 (14.15%)** | +| account_sized_init1 | 600 | 🔴 **+8 (1.35%)** | +| account_sized_init2 | 552 | 🔴 **+8 (1.47%)** | +| account_sized_init4 | 664 | 🟢 **-104 (13.54%)** | +| account_sized_init8 | 888 | 🟢 **-328 (26.97%)** | +| account_sized1 | 328 | 🔴 **+128 (64.00%)** | +| account_sized2 | 392 | 🔴 **+32 (8.89%)** | +| account_sized4 | 568 | 🔴 **+40 (7.58%)** | +| account_sized8 | 792 | 🟢 **-184 (18.85%)** | +| account_unsized_init1 | 624 | 🔴 **+16 (2.63%)** | +| account_unsized_init2 | 584 | 🟢 **-24 (3.95%)** | +| account_unsized_init4 | 728 | 🟢 **-168 (18.75%)** | +| account_unsized_init8 | 1,016 | 🟢 **-456 (30.98%)** | +| account_unsized1 | 344 | 🔴 **+168 (95.45%)** | +| account_unsized2 | 456 | 🔴 **+64 (16.33%)** | +| account_unsized4 | 632 | 🟢 **-24 (3.66%)** | +| account_unsized8 | 920 | 🟢 **-312 (25.32%)** | +| boxed_account_empty_init1 | 552 | 🔴 **+8 (1.47%)** | +| boxed_account_empty_init2 | 400 | 🟢 **-8 (1.96%)** | +| boxed_account_empty_init4 | 432 | 🔴 **+8 (1.89%)** | +| boxed_account_empty_init8 | 496 | 🔴 **+40 (8.77%)** | +| boxed_account_empty1 | 320 | 🔴 **+80 (33.33%)** | +| boxed_account_empty2 | 320 | 🔴 **+72 (29.03%)** | +| boxed_account_empty4 | 320 | 🔴 **+40 (14.29%)** | +| boxed_account_empty8 | 336 | 🔴 **+24 (7.69%)** | +| boxed_account_sized_init1 | 552 | 🔴 **+8 (1.47%)** | +| boxed_account_sized_init2 | 400 | 🟢 **-8 (1.96%)** | +| boxed_account_sized_init4 | 432 | 🔴 **+8 (1.89%)** | +| boxed_account_sized_init8 | 496 | 🔴 **+40 (8.77%)** | +| boxed_account_sized1 | 320 | 🔴 **+80 (33.33%)** | +| boxed_account_sized2 | 320 | 🔴 **+72 (29.03%)** | +| boxed_account_sized4 | 320 | 🔴 **+40 (14.29%)** | +| boxed_account_sized8 | 336 | 🔴 **+24 (7.69%)** | +| boxed_account_unsized_init1 | 552 | 🔴 **+8 (1.47%)** | +| boxed_account_unsized_init2 | 400 | 🟢 **-8 (1.96%)** | +| boxed_account_unsized_init4 | 432 | 🔴 **+8 (1.89%)** | +| boxed_account_unsized_init8 | 496 | 🔴 **+40 (8.77%)** | +| boxed_account_unsized1 | 320 | 🔴 **+72 (29.03%)** | +| boxed_account_unsized2 | 320 | 🔴 **+72 (29.03%)** | +| boxed_account_unsized4 | 320 | 🔴 **+40 (14.29%)** | +| boxed_account_unsized8 | 336 | 🔴 **+24 (7.69%)** | +| boxed_interface_account_mint1 | 320 | 🔴 **+80 (33.33%)** | +| boxed_interface_account_mint2 | 320 | 🔴 **+72 (29.03%)** | +| boxed_interface_account_mint4 | 320 | 🔴 **+40 (14.29%)** | +| boxed_interface_account_mint8 | 336 | 🔴 **+24 (7.69%)** | +| boxed_interface_account_token1 | 320 | 🔴 **+80 (33.33%)** | +| boxed_interface_account_token2 | 320 | 🔴 **+72 (29.03%)** | +| boxed_interface_account_token4 | 320 | 🔴 **+40 (14.29%)** | +| boxed_interface_account_token8 | 336 | 🔴 **+24 (7.69%)** | +| interface_account_mint1 | 504 | 🔴 **+296 (142.31%)** | +| interface_account_mint2 | 680 | 🟢 **-72 (9.57%)** | +| interface_account_mint4 | 1,016 | 🟢 **-408 (28.65%)** | +| interface_account_mint8 | 1,688 | 🟢 **-1,080 (39.02%)** | +| interface_account_token1 | 680 | 🔴 **+416 (157.58%)** | +| interface_account_token2 | 856 | 🟢 **-248 (22.46%)** | +| interface_account_token4 | 1,368 | 🟢 **-760 (35.71%)** | +| interface1 | 320 | 🔴 **+120 (60.00%)** | +| interface2 | 368 | 🔴 **+24 (6.98%)** | +| interface4 | 552 | 🔴 **+88 (18.97%)** | +| interface8 | 728 | 🟢 **-120 (14.15%)** | +| program1 | 320 | 🔴 **+120 (60.00%)** | +| program2 | 368 | 🔴 **+24 (6.98%)** | +| program4 | 552 | 🔴 **+88 (18.97%)** | +| program8 | 728 | 🟢 **-120 (14.15%)** | +| signer1 | 328 | 🔴 **+80 (32.26%)** | +| signer2 | 376 | 🟢 **-16 (4.08%)** | +| signer4 | 560 | 🔴 **+48 (9.38%)** | +| signer8 | 728 | 🟢 **-168 (18.75%)** | +| system_account1 | 328 | 🔴 **+80 (32.26%)** | +| system_account2 | 376 | 🟢 **-16 (4.08%)** | +| system_account4 | 560 | 🔴 **+48 (9.38%)** | +| system_account8 | 728 | 🟢 **-168 (18.75%)** | +| unchecked_account1 | 328 | 🔴 **+80 (32.26%)** | +| unchecked_account2 | 376 | 🟢 **-16 (4.08%)** | +| unchecked_account4 | 560 | 🔴 **+48 (9.38%)** | +| unchecked_account8 | 728 | 🟢 **-168 (18.75%)** | + +### Notable changes + +- Upgrading Solana to `1.16`. The difference in stack memory usage between `0.27.0` and `0.28.0` is the direct result of upgrading Solana version(both build tools and crates) ([#2512](https://github.com/coral-xyz/anchor/pull/2512)). + +--- + +## [0.27.0] + +Solana version: 1.14.16 + +| Instruction | Stack Memory | +/- | +| ------------------------------ | ------------ | --- | +| account_info1 | 248 | N/A | +| account_info2 | 392 | N/A | +| account_info4 | 512 | N/A | +| account_info8 | 896 | N/A | +| account_empty_init1 | 584 | N/A | +| account_empty_init2 | 512 | N/A | +| account_empty_init4 | 704 | N/A | +| account_empty_init8 | 1,088 | N/A | +| account_empty1 | 200 | N/A | +| account_empty2 | 344 | N/A | +| account_empty4 | 464 | N/A | +| account_empty8 | 848 | N/A | +| account_sized_init1 | 592 | N/A | +| account_sized_init2 | 544 | N/A | +| account_sized_init4 | 768 | N/A | +| account_sized_init8 | 1,216 | N/A | +| account_sized1 | 200 | N/A | +| account_sized2 | 360 | N/A | +| account_sized4 | 528 | N/A | +| account_sized8 | 976 | N/A | +| account_unsized_init1 | 608 | N/A | +| account_unsized_init2 | 608 | N/A | +| account_unsized_init4 | 896 | N/A | +| account_unsized_init8 | 1,472 | N/A | +| account_unsized1 | 176 | N/A | +| account_unsized2 | 392 | N/A | +| account_unsized4 | 656 | N/A | +| account_unsized8 | 1,232 | N/A | +| boxed_account_empty_init1 | 544 | N/A | +| boxed_account_empty_init2 | 408 | N/A | +| boxed_account_empty_init4 | 424 | N/A | +| boxed_account_empty_init8 | 456 | N/A | +| boxed_account_empty1 | 240 | N/A | +| boxed_account_empty2 | 248 | N/A | +| boxed_account_empty4 | 280 | N/A | +| boxed_account_empty8 | 312 | N/A | +| boxed_account_sized_init1 | 544 | N/A | +| boxed_account_sized_init2 | 408 | N/A | +| boxed_account_sized_init4 | 424 | N/A | +| boxed_account_sized_init8 | 456 | N/A | +| boxed_account_sized1 | 240 | N/A | +| boxed_account_sized2 | 248 | N/A | +| boxed_account_sized4 | 280 | N/A | +| boxed_account_sized8 | 312 | N/A | +| boxed_account_unsized_init1 | 544 | N/A | +| boxed_account_unsized_init2 | 408 | N/A | +| boxed_account_unsized_init4 | 424 | N/A | +| boxed_account_unsized_init8 | 456 | N/A | +| boxed_account_unsized1 | 248 | N/A | +| boxed_account_unsized2 | 248 | N/A | +| boxed_account_unsized4 | 280 | N/A | +| boxed_account_unsized8 | 312 | N/A | +| boxed_interface_account_mint1 | 240 | N/A | +| boxed_interface_account_mint2 | 248 | N/A | +| boxed_interface_account_mint4 | 280 | N/A | +| boxed_interface_account_mint8 | 312 | N/A | +| boxed_interface_account_token1 | 240 | N/A | +| boxed_interface_account_token2 | 248 | N/A | +| boxed_interface_account_token4 | 280 | N/A | +| boxed_interface_account_token8 | 312 | N/A | +| interface_account_mint1 | 208 | N/A | +| interface_account_mint2 | 752 | N/A | +| interface_account_mint4 | 1,424 | N/A | +| interface_account_mint8 | 2,768 | N/A | +| interface_account_token1 | 264 | N/A | +| interface_account_token2 | 1,104 | N/A | +| interface_account_token4 | 2,128 | N/A | +| interface1 | 200 | N/A | +| interface2 | 344 | N/A | +| interface4 | 464 | N/A | +| interface8 | 848 | N/A | +| program1 | 200 | N/A | +| program2 | 344 | N/A | +| program4 | 464 | N/A | +| program8 | 848 | N/A | +| signer1 | 248 | N/A | +| signer2 | 392 | N/A | +| signer4 | 512 | N/A | +| signer8 | 896 | N/A | +| system_account1 | 248 | N/A | +| system_account2 | 392 | N/A | +| system_account4 | 512 | N/A | +| system_account8 | 896 | N/A | +| unchecked_account1 | 248 | N/A | +| unchecked_account2 | 392 | N/A | +| unchecked_account4 | 512 | N/A | +| unchecked_account8 | 896 | N/A | + +--- diff --git a/tests/bench/bench.json b/tests/bench/bench.json index a1b3f72a69..d5f4477b60 100644 --- a/tests/bench/bench.json +++ b/tests/bench/bench.json @@ -93,6 +93,95 @@ "uncheckedAccount2": 1567, "uncheckedAccount4": 2060, "uncheckedAccount8": 3855 + }, + "stackMemory": { + "account_info1": 248, + "account_info2": 392, + "account_info4": 512, + "account_info8": 896, + "account_empty_init1": 584, + "account_empty_init2": 512, + "account_empty_init4": 704, + "account_empty_init8": 1088, + "account_empty1": 200, + "account_empty2": 344, + "account_empty4": 464, + "account_empty8": 848, + "account_sized_init1": 592, + "account_sized_init2": 544, + "account_sized_init4": 768, + "account_sized_init8": 1216, + "account_sized1": 200, + "account_sized2": 360, + "account_sized4": 528, + "account_sized8": 976, + "account_unsized_init1": 608, + "account_unsized_init2": 608, + "account_unsized_init4": 896, + "account_unsized_init8": 1472, + "account_unsized1": 176, + "account_unsized2": 392, + "account_unsized4": 656, + "account_unsized8": 1232, + "boxed_account_empty_init1": 544, + "boxed_account_empty_init2": 408, + "boxed_account_empty_init4": 424, + "boxed_account_empty_init8": 456, + "boxed_account_empty1": 240, + "boxed_account_empty2": 248, + "boxed_account_empty4": 280, + "boxed_account_empty8": 312, + "boxed_account_sized_init1": 544, + "boxed_account_sized_init2": 408, + "boxed_account_sized_init4": 424, + "boxed_account_sized_init8": 456, + "boxed_account_sized1": 240, + "boxed_account_sized2": 248, + "boxed_account_sized4": 280, + "boxed_account_sized8": 312, + "boxed_account_unsized_init1": 544, + "boxed_account_unsized_init2": 408, + "boxed_account_unsized_init4": 424, + "boxed_account_unsized_init8": 456, + "boxed_account_unsized1": 248, + "boxed_account_unsized2": 248, + "boxed_account_unsized4": 280, + "boxed_account_unsized8": 312, + "boxed_interface_account_mint1": 240, + "boxed_interface_account_mint2": 248, + "boxed_interface_account_mint4": 280, + "boxed_interface_account_mint8": 312, + "boxed_interface_account_token1": 240, + "boxed_interface_account_token2": 248, + "boxed_interface_account_token4": 280, + "boxed_interface_account_token8": 312, + "interface_account_mint1": 208, + "interface_account_mint2": 752, + "interface_account_mint4": 1424, + "interface_account_mint8": 2768, + "interface_account_token1": 264, + "interface_account_token2": 1104, + "interface_account_token4": 2128, + "interface1": 200, + "interface2": 344, + "interface4": 464, + "interface8": 848, + "program1": 200, + "program2": 344, + "program4": 464, + "program8": 848, + "signer1": 248, + "signer2": 392, + "signer4": 512, + "signer8": 896, + "system_account1": 248, + "system_account2": 392, + "system_account4": 512, + "system_account8": 896, + "unchecked_account1": 248, + "unchecked_account2": 392, + "unchecked_account4": 512, + "unchecked_account8": 896 } } }, @@ -190,6 +279,95 @@ "uncheckedAccount2": 1475, "uncheckedAccount4": 1965, "uncheckedAccount8": 3841 + }, + "stackMemory": { + "account_info1": 328, + "account_info2": 376, + "account_info4": 560, + "account_info8": 728, + "account_empty_init1": 592, + "account_empty_init2": 560, + "account_empty_init4": 632, + "account_empty_init8": 824, + "account_empty1": 320, + "account_empty2": 368, + "account_empty4": 552, + "account_empty8": 728, + "account_sized_init1": 600, + "account_sized_init2": 552, + "account_sized_init4": 664, + "account_sized_init8": 888, + "account_sized1": 328, + "account_sized2": 392, + "account_sized4": 568, + "account_sized8": 792, + "account_unsized_init1": 624, + "account_unsized_init2": 584, + "account_unsized_init4": 728, + "account_unsized_init8": 1016, + "account_unsized1": 344, + "account_unsized2": 456, + "account_unsized4": 632, + "account_unsized8": 920, + "boxed_account_empty_init1": 552, + "boxed_account_empty_init2": 400, + "boxed_account_empty_init4": 432, + "boxed_account_empty_init8": 496, + "boxed_account_empty1": 320, + "boxed_account_empty2": 320, + "boxed_account_empty4": 320, + "boxed_account_empty8": 336, + "boxed_account_sized_init1": 552, + "boxed_account_sized_init2": 400, + "boxed_account_sized_init4": 432, + "boxed_account_sized_init8": 496, + "boxed_account_sized1": 320, + "boxed_account_sized2": 320, + "boxed_account_sized4": 320, + "boxed_account_sized8": 336, + "boxed_account_unsized_init1": 552, + "boxed_account_unsized_init2": 400, + "boxed_account_unsized_init4": 432, + "boxed_account_unsized_init8": 496, + "boxed_account_unsized1": 320, + "boxed_account_unsized2": 320, + "boxed_account_unsized4": 320, + "boxed_account_unsized8": 336, + "boxed_interface_account_mint1": 320, + "boxed_interface_account_mint2": 320, + "boxed_interface_account_mint4": 320, + "boxed_interface_account_mint8": 336, + "boxed_interface_account_token1": 320, + "boxed_interface_account_token2": 320, + "boxed_interface_account_token4": 320, + "boxed_interface_account_token8": 336, + "interface_account_mint1": 504, + "interface_account_mint2": 680, + "interface_account_mint4": 1016, + "interface_account_mint8": 1688, + "interface_account_token1": 680, + "interface_account_token2": 856, + "interface_account_token4": 1368, + "interface1": 320, + "interface2": 368, + "interface4": 552, + "interface8": 728, + "program1": 320, + "program2": 368, + "program4": 552, + "program8": 728, + "signer1": 328, + "signer2": 376, + "signer4": 560, + "signer8": 728, + "system_account1": 328, + "system_account2": 376, + "system_account4": 560, + "system_account8": 728, + "unchecked_account1": 328, + "unchecked_account2": 376, + "unchecked_account4": 560, + "unchecked_account8": 728 } } }, @@ -287,6 +465,95 @@ "uncheckedAccount2": 1475, "uncheckedAccount4": 1965, "uncheckedAccount8": 3841 + }, + "stackMemory": { + "account_info1": 328, + "account_info2": 376, + "account_info4": 560, + "account_info8": 728, + "account_empty_init1": 592, + "account_empty_init2": 560, + "account_empty_init4": 632, + "account_empty_init8": 824, + "account_empty1": 320, + "account_empty2": 368, + "account_empty4": 552, + "account_empty8": 728, + "account_sized_init1": 600, + "account_sized_init2": 552, + "account_sized_init4": 664, + "account_sized_init8": 888, + "account_sized1": 328, + "account_sized2": 392, + "account_sized4": 568, + "account_sized8": 792, + "account_unsized_init1": 624, + "account_unsized_init2": 584, + "account_unsized_init4": 728, + "account_unsized_init8": 1016, + "account_unsized1": 344, + "account_unsized2": 456, + "account_unsized4": 632, + "account_unsized8": 920, + "boxed_account_empty_init1": 552, + "boxed_account_empty_init2": 400, + "boxed_account_empty_init4": 432, + "boxed_account_empty_init8": 496, + "boxed_account_empty1": 320, + "boxed_account_empty2": 320, + "boxed_account_empty4": 320, + "boxed_account_empty8": 336, + "boxed_account_sized_init1": 552, + "boxed_account_sized_init2": 400, + "boxed_account_sized_init4": 432, + "boxed_account_sized_init8": 496, + "boxed_account_sized1": 320, + "boxed_account_sized2": 320, + "boxed_account_sized4": 320, + "boxed_account_sized8": 336, + "boxed_account_unsized_init1": 552, + "boxed_account_unsized_init2": 400, + "boxed_account_unsized_init4": 432, + "boxed_account_unsized_init8": 496, + "boxed_account_unsized1": 320, + "boxed_account_unsized2": 320, + "boxed_account_unsized4": 320, + "boxed_account_unsized8": 336, + "boxed_interface_account_mint1": 320, + "boxed_interface_account_mint2": 320, + "boxed_interface_account_mint4": 320, + "boxed_interface_account_mint8": 336, + "boxed_interface_account_token1": 320, + "boxed_interface_account_token2": 320, + "boxed_interface_account_token4": 320, + "boxed_interface_account_token8": 336, + "interface_account_mint1": 504, + "interface_account_mint2": 680, + "interface_account_mint4": 1016, + "interface_account_mint8": 1688, + "interface_account_token1": 680, + "interface_account_token2": 856, + "interface_account_token4": 1368, + "interface1": 320, + "interface2": 368, + "interface4": 552, + "interface8": 728, + "program1": 320, + "program2": 368, + "program4": 552, + "program8": 728, + "signer1": 328, + "signer2": 376, + "signer4": 560, + "signer8": 728, + "system_account1": 328, + "system_account2": 376, + "system_account4": 560, + "system_account8": 728, + "unchecked_account1": 328, + "unchecked_account2": 376, + "unchecked_account4": 560, + "unchecked_account8": 728 } } } diff --git a/tests/bench/scripts/sync.ts b/tests/bench/scripts/sync.ts index 44103c1046..970e6dc341 100644 --- a/tests/bench/scripts/sync.ts +++ b/tests/bench/scripts/sync.ts @@ -67,7 +67,6 @@ import { // Run the command to update the current version's results const result = runAnchorTest(); - console.log(result.output.toString()); // Check failure if (result.status !== 0) { diff --git a/tests/bench/scripts/utils.ts b/tests/bench/scripts/utils.ts index 5c50dfbcd2..7b1b1e7a69 100644 --- a/tests/bench/scripts/utils.ts +++ b/tests/bench/scripts/utils.ts @@ -25,6 +25,8 @@ export type BenchResult = { binarySize: BinarySize; /** Benchmark result for compute units consumed */ computeUnits: ComputeUnits; + /** Benchmark result for stack memory usage */ + stackMemory: StackMemory; }; /** `program name -> binary size` */ @@ -33,6 +35,9 @@ export type BinarySize = { [programName: string]: number }; /** `instruction name -> compute units consumed` */ export type ComputeUnits = { [ixName: string]: number }; +/** `instruction name -> stack memory used` */ +export type StackMemory = { [ixName: string]: number }; + /** * How much of a percentage difference between the current and the previous data * should be significant. Any difference above this number should be noted in @@ -124,7 +129,7 @@ export class BenchData { ) => { for (const name in compareFrom) { if (compareTo[name] === undefined) { - cb(name, compareTo[name]); + cb(name, compareFrom[name]); } } }; diff --git a/tests/bench/tests/binary-size.ts b/tests/bench/tests/binary-size.ts index 8a141b5e9a..0e60a56697 100644 --- a/tests/bench/tests/binary-size.ts +++ b/tests/bench/tests/binary-size.ts @@ -7,7 +7,7 @@ import { BenchData, BinarySize } from "../scripts/utils"; describe("Binary size", () => { const binarySize: BinarySize = {}; - it(`Measure binary size of program '${IDL.name}'`, async () => { + it("Measure binary size", async () => { const stat = await fs.stat(path.join("target", "deploy", `${IDL.name}.so`)); binarySize[IDL.name] = stat.size; }); diff --git a/tests/bench/tests/compute-units.ts b/tests/bench/tests/compute-units.ts index 108c2630e5..3465bcf1b8 100644 --- a/tests/bench/tests/compute-units.ts +++ b/tests/bench/tests/compute-units.ts @@ -2,12 +2,7 @@ import * as anchor from "@coral-xyz/anchor"; import * as token from "@coral-xyz/spl-token"; import { Bench, IDL } from "../target/types/bench"; -import { - BenchData, - ComputeUnits, - getVersionFromArgs, - spawn, -} from "../scripts/utils"; +import { BenchData, ComputeUnits } from "../scripts/utils"; describe("Compute units", () => { // Configure the client to use the local cluster diff --git a/tests/bench/tests/stack-memory.ts b/tests/bench/tests/stack-memory.ts new file mode 100644 index 0000000000..caad67b643 --- /dev/null +++ b/tests/bench/tests/stack-memory.ts @@ -0,0 +1,64 @@ +import path from "path"; +import fs from "fs/promises"; + +import { BenchData, StackMemory, spawn } from "../scripts/utils"; +import { IDL } from "../target/types/bench"; + +describe("Stack memory", () => { + const stackMemory: StackMemory = {}; + + const STACK_CONTENT = [ + "", + `let stack_limit: [u16; 2048] = [1; 2048];`, + `msg!("{}", stack_limit.len());`, + "", + ].join("\n\t\t"); + + it("Measure stack memory usage", async () => { + const libPath = path.join("programs", IDL.name, "src", "lib.rs"); + const lib = await fs.readFile(libPath, "utf8"); + const indices = [...lib.matchAll(/fn\s[\w\d]+\(/g)] + .map((match) => match.index) + .filter(Boolean) as number[]; + + let modifiedLib = lib; + let cumulativeIndex = 0; + + for (const index of indices) { + const curlyIndex = index + lib.slice(index).indexOf("{"); + const nextLineIndex = + curlyIndex + lib.slice(curlyIndex).indexOf("\n") + cumulativeIndex; + modifiedLib = + modifiedLib.slice(0, nextLineIndex) + + STACK_CONTENT + + modifiedLib.slice(nextLineIndex); + + cumulativeIndex += STACK_CONTENT.length; + } + + // Write the modified file + await fs.writeFile(libPath, modifiedLib); + + // Expected error: + // Error: Function _ZN5bench9__private8__global13account_info117h88e5c10f03de9fddE + // Stack offset of 4424 exceeded max offset of 4096 by 328 bytes + const buildResult = spawn("anchor", ["build", "--skip-lint"]); + const output = buildResult.output.toString(); + const matches = output.matchAll( + /global[\d]+([\w\d]+?)17.*by\s(\d+)\sbytes/g + ); + for (const match of matches) { + const ixName = match[1]; + const stackUsage = match[2]; + stackMemory[ixName] = +stackUsage; + } + + // Restore to the original file + await fs.writeFile(libPath, lib); + }); + + after(async () => { + const bench = await BenchData.open(); + await bench.update({ stackMemory }); + }); +}); diff --git a/tests/bench/tsconfig.json b/tests/bench/tsconfig.json index a53432cd4f..d5015c2bb7 100644 --- a/tests/bench/tsconfig.json +++ b/tests/bench/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "types": ["mocha", "node"], - "lib": ["ES6"], + "lib": ["es2020"], "module": "commonjs", "target": "es6", "esModuleInterop": true,