Skip to content

feat(core-backend): Add the ability to change backend context#4531

Open
Lazark0x wants to merge 54 commits intomasterfrom
laz/4449-change-backend-alternative
Open

feat(core-backend): Add the ability to change backend context#4531
Lazark0x wants to merge 54 commits intomasterfrom
laz/4449-change-backend-alternative

Conversation

@Lazark0x
Copy link
Copy Markdown
Member

@Lazark0x Lazark0x commented Mar 10, 2025

Resolves #4449

Sequential Execution Optimization for Ethexe Runtime

Summary

This PR implements sequential execution optimization for the ethexe runtime, enabling WASM environment reuse across consecutive dispatch executions. By caching execution contexts and avoiding redundant lazy pages setup/teardown between dispatches, we achieve significant gas savings.

Problem

Previously, each dispatch execution in ethexe required:

  1. Full lazy pages initialization with memory protection setup
  2. Complete WASM environment instantiation
  3. Tear down of lazy pages protection after execution
  4. Repeat for every single dispatch

This approach incurs substantial overhead when processing multiple dispatches in sequence, as each dispatch paid the full initialization cost (~700M gas) regardless of whether the previous execution could be reused.

Solution

The optimization introduces:

  1. PrechargeContext: Caches code, metadata, and environment state between executions
  2. Sequential execution mode: Reuses WASM environment when processing multiple dispatches for the same program
  3. Deferred lazy pages cleanup: Only performs lazy pages protection setup/teardown once per batch instead of per-dispatch
  4. Memory snapshots: Captures memory state for rollback on execution failure

Benchmark Results

Benchmarks comparing gas consumption between this branch and master when processing multiple dispatches (using demo-fungible-token workload with init + mint operations):

Gas Consumption Comparison

Dispatches Master (Avg Gas) This Branch (Avg Gas) Gas Savings
1 361,256,312 361,256,312 0%
2 1,078,332,174 719,961,176 33.2%
3 1,795,408,036 938,808,118 47.7%
5 3,229,559,760 1,376,502,002 57.4%
10 6,814,939,070 2,470,736,712 63.7%
20 13,985,697,690 4,659,206,132 66.7%
50 35,497,973,550 11,224,614,392 68.4%
100 71,351,766,650 22,166,961,492 68.9%

Gas per Dispatch Analysis

Dispatches Master (Gas/Dispatch) This Branch (Gas/Dispatch) Reduction
1 361M 361M 0%
2 539M 360M 33%
5 646M 275M 57%
10 681M 247M 64%
20 699M 233M 67%
50 710M 224M 68%
100 714M 222M 69%

Execution Time Comparison

Dispatches Master (Time) This Branch (Time) Time Savings
1 264µs 257µs 3%
2 414µs 249µs 40%
5 1.04ms 394µs 62%
10 2.13ms 615µs 71%
20 4.20ms 1.06ms 75%
50 10.6ms 2.41ms 77%
100 21.0ms 4.61ms 78%

Key Observations

  1. Single dispatch: Identical performance (no optimization opportunity)
  2. 2-100 dispatches: 33-69% gas savings, with benefits increasing as dispatch count grows
  3. Gas per dispatch: Converges to ~220M (vs ~710M on master) - a 69% reduction in marginal cost
  4. Execution time: 40-78% faster, with time per dispatch dropping from ~210µs to ~46µs

Why This Matters

Master's gas consumption scales linearly at ~700M gas per dispatch (full init + execution each time).

This branch's gas consumption is sub-linear:

  • First dispatch: ~361M (full init)
  • Subsequent dispatches: ~220M (execution only, environment reused)

At 100 dispatches, this means:

  • Master: 71.4B gas
  • This branch: 22.2B gas
  • Savings: 49.2B gas (69%)

@Lazark0x Lazark0x added the A1-inprogress Issue is in progress or PR draft is not ready to be reviewed label Mar 10, 2025
@Lazark0x Lazark0x self-assigned this Mar 10, 2025
@Lazark0x Lazark0x requested a review from Copilot April 22, 2025 12:06
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds functionality to change the backend context by updating environment state handling, memory reset logic and benchmark configurations. Key changes include:

  • Modifying various Environment::new calls to use a callback function instead of a DispatchKind parameter.
  • Introducing a new memory dump structure and related memory dump/set functions in the core and processor modules.
  • Refining benchmarks, gas counter traits and context types to support new backend context capabilities.

Reviewed Changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated no comments.

Show a summary per file
File Description
utils/wasm-gen/src/tests.rs Updated test setup using the new Environment::new API
pallets/gear/src/benchmarking/mod.rs Replaced DispatchKind with callback-based environment creation
core/src/memory.rs Added PageDump structure and enhanced MemoryError with Default
core/src/gas.rs Added Eq and PartialEq to GasAmount for improved comparisons
core/src/code/mod.rs Introduced the new accessor for stack_end
core/src/buffer.rs Added try_replace method along with a new test case
core-processor/src/lib.rs Added a testing module for processor extensions
core-processor/src/ext.rs Implemented new dump_memory and set_memory functions and updated derives
core-processor/src/executor.rs Updated environment creation and error handling in execute calls
core-processor/src/context.rs Adjusted SystemReservationContext with Eq and PartialEq derives
core-processor/Cargo.toml Updated dependencies/features with optional modules for lazy-pages
core-backend/src/lib.rs Revised Environment::new usage and execute call to support backend context changes

…kend-alternative

# Conflicts:
#	Cargo.lock
#	core/src/memory.rs
…kend-alternative

# Conflicts:
#	core-backend/src/env.rs
#	core-processor/Cargo.toml
#	core-processor/src/executor.rs
#	core-processor/src/ext.rs
#	core/src/memory.rs
#	utils/wasm-gen/src/tests.rs
Lazark0x and others added 28 commits July 31, 2025 17:01
…kend-alternative

# Conflicts:
#	pallets/gear/src/benchmarking/mod.rs
@grishasobol grishasobol added the A6-onhold PR is temporarily paused due to priority or external factors. label Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A1-inprogress Issue is in progress or PR draft is not ready to be reviewed A6-onhold PR is temporarily paused due to priority or external factors.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add the ability to change backend context

4 participants