Skip to content

Commit

Permalink
feat: Don't add same constant more than once in function
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Feb 3, 2025
1 parent 3931040 commit 2046f37
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
21 changes: 20 additions & 1 deletion src/Codegen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,17 @@ pub const Frame = struct {
function: ?*obj.ObjFunction = null,
return_counts: bool = false,
return_emitted: bool = false,
// Keep track of constants to avoid adding the same more than once to the chunk
constants: std.AutoHashMapUnmanaged(Value, u24),

try_should_handle: ?std.AutoHashMap(*obj.ObjTypeDef, Ast.TokenIndex) = null,

pub fn deinit(self: *Frame, allocator: std.mem.Allocator) void {
self.constants.deinit(allocator);
if (self.try_should_handle) |*handle| {
handle.deinit();
}
}
};

const NodeGen = *const fn (
Expand Down Expand Up @@ -289,7 +298,12 @@ pub fn emitConstant(self: *Self, location: Ast.TokenIndex, value: Value) !void {
}

pub fn makeConstant(self: *Self, value: Value) !u24 {
if (self.current.?.constants.get(value)) |idx| {
return idx;
}

const constant = try self.current.?.function.?.chunk.addConstant(null, value);
try self.current.?.constants.put(self.gc.allocator, value, constant);
if (constant > Chunk.max_constants) {
self.reportError("Too many constants in one chunk.");
return 0;
Expand Down Expand Up @@ -2398,6 +2412,7 @@ fn generateFunction(self: *Self, node: Ast.Node.Index, breaks: ?*Breaks) Error!?
self.current.?.* = .{
.enclosing = enclosing,
.function_node = node,
.constants = .{},
};

var function = try obj.ObjFunction.init(
Expand Down Expand Up @@ -2569,7 +2584,7 @@ fn generateFunction(self: *Self, node: Ast.Node.Index, breaks: ?*Breaks) Error!?
}
}

const frame = self.current.?;
var frame = self.current.?;
const current_function = frame.function.?;
current_function.upvalue_count = @intCast(components.upvalue_binding.count());

Expand All @@ -2580,6 +2595,10 @@ fn generateFunction(self: *Self, node: Ast.Node.Index, breaks: ?*Breaks) Error!?

self.current = frame.enclosing;

// We don't need this frame anymore
frame.deinit(self.gc.allocator);
self.gc.allocator.destroy(frame);

if (function_type != .ScriptEntryPoint and function_type != .Repl) {
// `extern` functions don't have upvalues
if (function_type == .Extern) {
Expand Down
3 changes: 0 additions & 3 deletions src/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,12 @@ pub const Frame = struct {
function_node: Ast.Node.Index,
function: ?*obj.ObjFunction = null,
generics: ?*std.AutoArrayHashMap(*obj.ObjString, *obj.ObjTypeDef) = null,
constants: std.ArrayList(Value),

in_try: bool = false,
in_block_expression: ?u32 = null,

pub fn deinit(self: *Frame) void {
self.scopes.deinit();
self.constants.deinit();
// self.generics ends up in AST node so we don't deinit it
}

Expand Down Expand Up @@ -1070,7 +1068,6 @@ fn beginFrame(self: *Self, function_type: obj.ObjFunction.FunctionType, function
.upvalues = [_]UpValue{undefined} ** 255,
.enclosing = enclosing,
.function_node = function_node,
.constants = .init(self.gc.allocator),
.scopes = .init(self.gc.allocator),
};

Expand Down

0 comments on commit 2046f37

Please sign in to comment.