Skip to content

Commit

Permalink
perf improve
Browse files Browse the repository at this point in the history
  • Loading branch information
StringNick committed Jul 19, 2024
1 parent b68fb92 commit e815aeb
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
9 changes: 5 additions & 4 deletions src/vm/air_input_public.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");
const Felt252 = @import("starknet").fields.Felt252;
const RelocatedTraceEntry = @import("trace_context.zig").RelocatedTraceEntry;
const RelocatedFelt252 = @import("trace_context.zig").RelocatedFelt252;
const HintProcessor = @import("../hint_processor/hint_processor_def.zig").CairoVMHintProcessor;

const Config = @import("config.zig").Config;
Expand Down Expand Up @@ -117,15 +118,15 @@ pub const PublicInput = struct {
// new - creating new PublicInput, all arguments caller is owner
pub fn new(
allocator: std.mem.Allocator,
memory: []const ?Felt252,
memory: []const RelocatedFelt252,
layout: []const u8,
public_memory_addresses: []const std.meta.Tuple(&.{ usize, usize }),
memory_segment_addresses: std.StringHashMap(std.meta.Tuple(&.{ usize, usize })),
trace: []const RelocatedTraceEntry,
rc_limits: std.meta.Tuple(&.{ isize, isize }),
) !Self {
const memory_entry = (struct {
fn func(mem: []const ?Felt252, addresses: std.meta.Tuple(&.{ usize, usize })) !PublicMemoryEntry {
fn func(mem: []const RelocatedFelt252, addresses: std.meta.Tuple(&.{ usize, usize })) !PublicMemoryEntry {
const address, const page = addresses;
return .{
.address = address,
Expand Down Expand Up @@ -181,7 +182,7 @@ pub const PublicMemoryEntry = struct {
address: usize,
value: struct {
/// using struct only for json parse abstraction
value: ?Felt252,
value: RelocatedFelt252,

pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !@This() {
_ = allocator; // autofix
Expand All @@ -200,7 +201,7 @@ pub const PublicMemoryEntry = struct {
}

pub fn jsonStringify(self: @This(), out: anytype) !void {
if (self.value) |v| try out.print("\"0x{x}\"", .{v.toU256()}) else try out.write(null);
if (self.value.getValue()) |v| try out.print("\"0x{x}\"", .{v.toU256()}) else try out.write(null);
}
},
page: usize,
Expand Down
6 changes: 2 additions & 4 deletions src/vm/cairo_run.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ pub fn writeEncodedTrace(relocated_trace: []const RelocatedTraceEntry, dest: any
///
/// - `relocated_memory`: The post-execution memory, relocated.
/// - `dest`: The destination file that the memory is to be written.
pub fn writeEncodedMemory(relocated_memory: []?Felt252, dest: anytype) !void {
pub fn writeEncodedMemory(relocated_memory: []RelocatedFelt252, dest: anytype) !void {
var buf: [8]u8 = undefined;

for (relocated_memory, 0..) |memory_cell, i| {
if (memory_cell) |cell| {
if (memory_cell.getValue()) |cell| {
std.mem.writeInt(u64, &buf, i, .little);
_ = try dest.write(&buf);
_ = try dest.write(&cell.toBytesLe());
Expand Down Expand Up @@ -217,7 +217,6 @@ pub fn runConfig(allocator: Allocator, config: Config) !CairoRunner {
&hint_processor,
);


try runner.vm.verifyAutoDeductions(allocator);

// cairo_runner.read_return_values(allow_missing_builtins)?;
Expand All @@ -228,7 +227,6 @@ pub fn runConfig(allocator: Allocator, config: Config) !CairoRunner {
if (secure_run)
try security.verifySecureRunner(allocator, &runner, true, null);


if (config.print_output) {
var buf = try std.ArrayList(u8).initCapacity(allocator, 100);
defer buf.deinit();
Expand Down
8 changes: 4 additions & 4 deletions src/vm/core.zig
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,10 @@ pub const CairoVM = struct {
try self.opcodeAssertions(instruction, operands_result);

// Constants for offset bit manipulation.
const OFFSET = 1 << 15;
const off_0 = instruction.off_0 + OFFSET;
const off_1 = instruction.off_1 + OFFSET;
const off_2 = instruction.off_2 + OFFSET;
const OFFSET: u16 = 1 << 15;
const off_0 = @as(isize, instruction.off_0) + OFFSET;
const off_1 = @as(isize, instruction.off_1) + OFFSET;
const off_2 = @as(isize, instruction.off_2) + OFFSET;

// Calculate and update relocation limits.
self.rc_limits = if (self.rc_limits) |limits| .{
Expand Down
7 changes: 4 additions & 3 deletions src/vm/instructions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,16 @@ pub const Instruction = struct {
/// Offset 0
///
/// In the range [-2**15, 2*15) = [-2**(OFFSET_BITS-1), 2**(OFFSET_BITS-1)).
off_0: isize = 0,
/// 16 bit signed integer
off_0: i16 = 0,
/// Offset 1
///
/// In the range [-2**15, 2*15) = [-2**(OFFSET_BITS-1), 2**(OFFSET_BITS-1)).
off_1: isize = 0,
off_1: i16 = 0,
/// Offset 2
///
/// In the range [-2**15, 2*15) = [-2**(OFFSET_BITS-1), 2**(OFFSET_BITS-1)).
off_2: isize = 0,
off_2: i16 = 0,
/// Destination register.
dst_reg: Register = .FP,
/// Operand 0 register.
Expand Down
10 changes: 5 additions & 5 deletions src/vm/runners/cairo_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub const CairoRunner = struct {
run_ended: bool = false,
execution_public_memory: ?std.ArrayList(usize) = null,
relocated_trace: ?[]RelocatedTraceEntry = null,
relocated_memory: ArrayList(?Felt252),
relocated_memory: ArrayList(RelocatedFelt252),
execution_scopes: ExecutionScopes = undefined,
segments_finalized: bool,

Expand Down Expand Up @@ -184,7 +184,7 @@ pub const CairoRunner = struct {
.instructions = instructions,
.vm = vm,
.runner_mode = if (proof_mode) .proof_mode_canonical else .execution_mode,
.relocated_memory = ArrayList(?Felt252).init(allocator),
.relocated_memory = ArrayList(RelocatedFelt252).init(allocator),
.execution_scopes = exec_scopes,
.entrypoint = program.shared_program_data.main,
.segments_finalized = false,
Expand Down Expand Up @@ -939,14 +939,14 @@ pub const CairoRunner = struct {

// Resize `relocated_memory` if needed.
if (self.relocated_memory.items.len <= relocated_address) {
self.relocated_memory.appendNTimesAssumeCapacity(null, relocated_address - self.relocated_memory.items.len + 1);
self.relocated_memory.appendNTimesAssumeCapacity(RelocatedFelt252.NONE, relocated_address - self.relocated_memory.items.len + 1);
}

// Update the entry in `relocated_memory` with the relocated value of the memory cell.
self.relocated_memory.items[relocated_address] = try cell.relocateValue(relocation_table);
self.relocated_memory.items[relocated_address] = RelocatedFelt252.init(try cell.relocateValue(relocation_table));
} else {
// If the memory cell is null, append `null` to `relocated_memory`.
self.relocated_memory.appendAssumeCapacity(null);
self.relocated_memory.appendAssumeCapacity(RelocatedFelt252.NONE);
}
}
}
Expand Down

0 comments on commit e815aeb

Please sign in to comment.