From df7515f8a0860029dc83c54f8017bf33ac7eab64 Mon Sep 17 00:00:00 2001 From: Nikita Date: Wed, 8 May 2024 15:28:04 +0200 Subject: [PATCH] perf opt --- build.zig | 1 + .../benchmarks/compare_arrays_200000.json | 6 ++--- scripts/benchmarks.sh | 6 ++--- src/cmd/cmd.zig | 2 +- src/vm/cairo_run.zig | 2 +- src/vm/core.zig | 22 ++++++------------- src/vm/memory/memory.zig | 11 +++++----- src/vm/memory/relocatable.zig | 2 +- src/vm/runners/cairo_runner.zig | 4 ---- 9 files changed, 22 insertions(+), 34 deletions(-) diff --git a/build.zig b/build.zig index 8dff82cc..b5ef6683 100644 --- a/build.zig +++ b/build.zig @@ -83,6 +83,7 @@ pub fn build(b: *std.Build) void { .optimize = optimize, .single_threaded = true, }); + exe.linkLibC(); // Add dependency modules to the executable. for (deps) |mod| exe.root_module.addImport( mod.name, diff --git a/cairo_programs/benchmarks/compare_arrays_200000.json b/cairo_programs/benchmarks/compare_arrays_200000.json index 406fd618..bfa082e7 100644 --- a/cairo_programs/benchmarks/compare_arrays_200000.json +++ b/cairo_programs/benchmarks/compare_arrays_200000.json @@ -186,7 +186,7 @@ "end_col": 38, "end_line": 3, "input_file": { - "filename": "/Users/nikita/Projects/hobby/ziggy-starkdust/cairo-vm-env/lib/python3.12/site-packages/starkware/cairo/common/alloc.cairo" + "filename": "/home/nikita/Projects/starknet/ziggy-starkdust/cairo-vm-env/lib/python3.12/site-packages/starkware/cairo/common/alloc.cairo" }, "start_col": 5, "start_line": 3 @@ -198,7 +198,7 @@ "end_col": 12, "end_line": 4, "input_file": { - "filename": "/Users/nikita/Projects/hobby/ziggy-starkdust/cairo-vm-env/lib/python3.12/site-packages/starkware/cairo/common/alloc.cairo" + "filename": "/home/nikita/Projects/starknet/ziggy-starkdust/cairo-vm-env/lib/python3.12/site-packages/starkware/cairo/common/alloc.cairo" }, "start_col": 5, "start_line": 4 @@ -221,7 +221,7 @@ "end_col": 40, "end_line": 5, "input_file": { - "filename": "/Users/nikita/Projects/hobby/ziggy-starkdust/cairo-vm-env/lib/python3.12/site-packages/starkware/cairo/common/alloc.cairo" + "filename": "/home/nikita/Projects/starknet/ziggy-starkdust/cairo-vm-env/lib/python3.12/site-packages/starkware/cairo/common/alloc.cairo" }, "start_col": 5, "start_line": 5 diff --git a/scripts/benchmarks.sh b/scripts/benchmarks.sh index d7dc277a..6f18ad43 100644 --- a/scripts/benchmarks.sh +++ b/scripts/benchmarks.sh @@ -1,7 +1,7 @@ #!/bin/bash set -e -source ../cairo-vm-env/bin/activate +. ../cairo-vm-env/bin/activate BENCH_DIR=../cairo_programs/benchmarks CAIRO_DIR=../cairo_programs/ @@ -13,6 +13,6 @@ for file in $(ls ${BENCH_DIR} | grep .cairo | sed -E 's/\.cairo//'); do cairo-compile --cairo_path="${CAIRO_DIR}" ${BENCH_DIR}/${file}.cairo --output ${BENCH_DIR}/${file}.json --proof_mode echo "Running ${file} benchmark" hyperfine --show-output --warmup 2 \ - -n "cairo-vm (Zig)" "${ZIG_CLI} execute --filename ${BENCH_DIR}/${file}.json --enable-trace=true --output-memory=/dev/null --output-trace=/dev/null --layout all_cairo" \ - -n "cairo-vm (Rust)" "${CAIRO_VM_CLI} ${BENCH_DIR}/${file}.json --proof_mode --memory_file /dev/null --trace_file /dev/null --layout all_cairo" + -n "cairo-vm (Zig)" "${ZIG_CLI} execute --filename ${BENCH_DIR}/${file}.json --layout all_cairo" \ + -n "cairo-vm (Rust)" "${CAIRO_VM_CLI} ${BENCH_DIR}/${file}.json --layout all_cairo" done diff --git a/src/cmd/cmd.zig b/src/cmd/cmd.zig index 1d138c57..5bca9c25 100644 --- a/src/cmd/cmd.zig +++ b/src/cmd/cmd.zig @@ -15,7 +15,7 @@ const CairoRunner = cairo_runner.CairoRunner; const ProgramJson = @import("../vm/types/programjson.zig").ProgramJson; const cairo_run = @import("../vm/cairo_run.zig"); -var gpa = std.heap.GeneralPurposeAllocator(.{}){}; +// var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const gpa_allocator = std.heap.c_allocator; // Configuration settings for the CLI. diff --git a/src/vm/cairo_run.zig b/src/vm/cairo_run.zig index 60b6c6c9..e411e419 100644 --- a/src/vm/cairo_run.zig +++ b/src/vm/cairo_run.zig @@ -86,7 +86,7 @@ pub fn runConfig(allocator: Allocator, config: Config) !void { try runner.runUntilPC(end, false, &hint_processor); try runner.endRun( allocator, - true, + false, false, &hint_processor, false, diff --git a/src/vm/core.zig b/src/vm/core.zig index fe826d18..14a83b35 100644 --- a/src/vm/core.zig +++ b/src/vm/core.zig @@ -263,7 +263,6 @@ pub const CairoVM = struct { pub fn stepHintExtensive( self: *Self, - allocator: Allocator, hint_processor: HintProcessor, exec_scopes: *ExecutionScopes, hint_datas: *std.ArrayList(HintData), @@ -275,7 +274,7 @@ pub const CairoVM = struct { for (hint_range.start..hint_range.start + hint_range.length) |idx| { const hint_data = if (idx < hint_datas.items.len) &hint_datas.items[idx] else return CairoVMError.Unexpected; - var hint_extension = try hint_processor.executeHintExtensive(allocator, self, hint_data, constants, exec_scopes); + var hint_extension = try hint_processor.executeHintExtensive(self.allocator, self, hint_data, constants, exec_scopes); defer hint_extension.deinit(); var it = hint_extension.iterator(); @@ -291,14 +290,13 @@ pub const CairoVM = struct { pub fn stepHintNotExtensive( self: *Self, - allocator: Allocator, hint_processor: HintProcessor, exec_scopes: *ExecutionScopes, hint_datas: []HintData, constants: *std.StringHashMap(Felt252), ) !void { for (hint_datas) |*hint_data| { - try hint_processor.executeHint(allocator, self, hint_data, constants, exec_scopes); + try hint_processor.executeHint(self.allocator, self, hint_data, constants, exec_scopes); } } @@ -315,7 +313,7 @@ pub const CairoVM = struct { /// - `allocator`: The allocator used for memory operations. /// # Errors /// - If accessing an unknown memory cell occurs. - pub fn stepInstruction(self: *Self, allocator: Allocator) !void { + pub fn stepInstruction(self: *Self) !void { const inst = if (self.run_context.pc.segment_index == 0) value: { // Run instructions from the program segment, using the instruction cache. const pc = self.run_context.pc.offset; @@ -344,7 +342,6 @@ pub const CairoVM = struct { // Execute the instruction if skip_instruction_execution is false. if (!self.skip_instruction_execution) { try self.runInstruction( - allocator, inst, ); } else { @@ -358,27 +355,24 @@ pub const CairoVM = struct { /// Process an instruction cycle using the typical fetch-decode-execute cycle. pub fn stepNotExtensive( self: *Self, - allocator: Allocator, hint_processor: HintProcessor, exec_scopes: *ExecutionScopes, hint_datas: []HintData, constants: *std.StringHashMap(Felt252), ) !void { try self.stepHintNotExtensive( - allocator, hint_processor, exec_scopes, hint_datas, constants, ); - try self.stepInstruction(allocator); + try self.stepInstruction(); } /// Do a single step of the VM with extensive hints. /// Process an instruction cycle using the typical fetch-decode-execute cycle. pub fn stepExtensive( self: *Self, - allocator: Allocator, hint_processor: HintProcessor, exec_scopes: *ExecutionScopes, hint_datas: *std.ArrayList(HintData), @@ -386,14 +380,13 @@ pub const CairoVM = struct { constants: *std.StringHashMap(Felt252), ) !void { try self.stepHintExtensive( - allocator, hint_processor, exec_scopes, hint_datas, hint_ranges, constants, ); - try self.stepInstruction(allocator); + try self.stepInstruction(); } /// Insert Operands only after checking if they were deduced. @@ -463,7 +456,6 @@ pub const CairoVM = struct { /// a controlled environment to ensure the correct execution of instructions and memory operations. pub fn runInstruction( self: *Self, - allocator: Allocator, instruction: Instruction, ) !void { // Check if tracing is disabled and log the current state if not. @@ -478,10 +470,10 @@ pub const CairoVM = struct { } // Compute operands for the instruction. - const operands_result = try self.computeOperands(allocator, instruction); + const operands_result = try self.computeOperands(self.allocator, instruction); // Insert deduced operands into memory. - try self.insertDeducedOperands(allocator, operands_result); + try self.insertDeducedOperands(self.allocator, operands_result); // Perform opcode-specific assertions on operands using the `opcodeAssertions` function. try self.opcodeAssertions(instruction, operands_result); diff --git a/src/vm/memory/memory.zig b/src/vm/memory/memory.zig index 609022da..35cb46de 100644 --- a/src/vm/memory/memory.zig +++ b/src/vm/memory/memory.zig @@ -326,7 +326,7 @@ pub const Memory = struct { /// # Returns /// /// Returns a reference to the data in the form of `std.ArrayList(std.ArrayListUnmanaged(?MemoryCell))`. - pub inline fn getDataFromSegmentIndex( + pub fn getDataFromSegmentIndex( self: *const Self, segment_index: i64, ) []std.ArrayListUnmanaged(?MemoryCell) { @@ -408,16 +408,15 @@ pub const Memory = struct { const segment_index = address.getAdjustedSegmentIndex(); // Check if the segment index is valid within the data structure. - const isSegmentIndexValid = address.segment_index < data.len; + if (segment_index >= data.len) return null; + const segment_data = data[segment_index].items; // Check if the offset is valid within the specified segment. - const isOffsetValid = isSegmentIndexValid and (address.offset < data[segment_index].items.len); + if (address.offset >= segment_data.len) return null; // Return null if either the segment index or offset is not valid. // Otherwise, return the maybe_relocatable value at the specified address. - return if (!isSegmentIndexValid or !isOffsetValid) - null - else if (data[segment_index].items[address.offset]) |val| + return if (segment_data[address.offset]) |val| switch (val.maybe_relocatable) { .relocatable => |addr| Self.relocateAddress(addr, self.relocation_rules) catch unreachable, else => |_| val.maybe_relocatable, diff --git a/src/vm/memory/relocatable.zig b/src/vm/memory/relocatable.zig index dcdd96e6..8350448f 100644 --- a/src/vm/memory/relocatable.zig +++ b/src/vm/memory/relocatable.zig @@ -270,7 +270,7 @@ pub const Relocatable = struct { /// /// # Returns /// The adjusted `usize` value representing the segment index. - pub inline fn getAdjustedSegmentIndex(self: *const Self) usize { + pub inline fn getAdjustedSegmentIndex(self: Self) usize { return @intCast(if (self.segment_index < 0) -(self.segment_index + 1) else diff --git a/src/vm/runners/cairo_runner.zig b/src/vm/runners/cairo_runner.zig index 497545c7..e9303000 100644 --- a/src/vm/runners/cairo_runner.zig +++ b/src/vm/runners/cairo_runner.zig @@ -567,7 +567,6 @@ pub const CairoRunner = struct { while (!end.eq(self.vm.run_context.pc) and !hint_processor.run_resources.consumed()) { if (extensive_hints) { try self.vm.stepExtensive( - self.allocator, hint_processor.*, &self.execution_scopes, &hint_datas, @@ -576,7 +575,6 @@ pub const CairoRunner = struct { ); } else { try self.vm.stepNotExtensive( - self.allocator, hint_processor.*, &self.execution_scopes, if (self.program @@ -713,7 +711,6 @@ pub const CairoRunner = struct { // Execute a single step of the program, considering extensive or non-extensive hints if (extensive_hints) { try self.vm.stepExtensive( - self.allocator, hint_processor.*, &self.execution_scopes, &hint_datas, @@ -722,7 +719,6 @@ pub const CairoRunner = struct { ); } else { try self.vm.stepNotExtensive( - self.allocator, hint_processor.*, &self.execution_scopes, hint_data_final,