Skip to content

🟑 [Medium] Performance EVM: Heap allocation for PUSH9-32 values adds pressure #858

@roninjin10

Description

@roninjin10

Summary

The dispatch preprocessor allocates individual heap objects for PUSH values larger than 8 bytes, which adds allocation pressure during bytecode preprocessing.

Location

src/preprocessor/dispatch.zig:42-62

Current Code

fn processPushOpcode(...) !void {
    if (data.size <= 8 and data.value <= std.math.maxInt(u64)) {
        const inline_value: u64 = @intCast(data.value);
        try schedule_items.append(allocator, .{ .push_inline = .{ .value = inline_value } });
    } else {
        // Allocate individual u256 value on heap
        const value_ptr = try allocator.create(FrameType.WordType);
        errdefer allocator.destroy(value_ptr);
        value_ptr.* = data.value;
        try schedule_items.append(allocator, .{ .push_pointer = .{ .value_ptr = value_ptr } });
    }
}

Problem

  • Each PUSH9-32 instruction requires a separate heap allocation
  • Contracts with many large pushes (addresses, hashes) create many allocations
  • Individual deallocations required during cleanup
  • Could fragment memory over time

Impact

  • Increased preprocessing time for contracts with large constants
  • Memory fragmentation
  • Allocator pressure

Recommended Fix

Consider using an arena or bump allocator for push values:

// Use a dedicated arena for push values
const push_arena = try allocator.create(std.heap.ArenaAllocator);
push_arena.* = std.heap.ArenaAllocator.init(allocator);
// All push values allocated from arena, single deinit at end

Or pre-scan bytecode to allocate a single array of all needed values.


Note: This issue was created by Claude AI assistant during code review, not @roninjin10 or @fucory

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions