diff options
Diffstat (limited to 'src/zisp/io')
| -rw-r--r-- | src/zisp/io/Parser.zig | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/src/zisp/io/Parser.zig b/src/zisp/io/Parser.zig index 29b8590..6dcfe42 100644 --- a/src/zisp/io/Parser.zig +++ b/src/zisp/io/Parser.zig @@ -48,6 +48,7 @@ const ListPool = gc.ListPool; const IstrSet = gc.IstrSet; const Decoder = io.Decoder; const Value = value.Value; +const IstrPtr = value.istr.IstrPtr; const Parser = @This(); @@ -115,7 +116,7 @@ pub fn init() !Parser { const istr_set = gc.mainIstrSet(); const alloc = gc.mainAlloc(); const decoder = io.mainDecoder(); - return initCustom(list_pool, istr_set, alloc, 32, 2048, 32, decoder); + return initCustom(list_pool, istr_set, alloc, 16, 512, 32, decoder); } pub fn initCustom( @@ -127,13 +128,17 @@ pub fn initCustom( init_list_elts_cap: usize, decoder: ?*Decoder, ) !Parser { + // Force some sane minimum values. + const ctx_cap = @max(1, init_ctx_stack_cap); + const str_cap = @max(8, init_str_chars_cap); + const lst_cap = @max(2, init_list_elts_cap); return .{ .list_pool = list_pool, .istr_set = istr_set, .alloc = alloc, - .ctx_stack = try .initCapacity(alloc, init_ctx_stack_cap), - .str_chars = try .initCapacity(alloc, init_str_chars_cap), - .list_elts = try .initCapacity(alloc, init_list_elts_cap), + .ctx_stack = try .initCapacity(alloc, ctx_cap), + .str_chars = try .initCapacity(alloc, str_cap), + .list_elts = try .initCapacity(alloc, lst_cap), .decoder = decoder, }; } @@ -218,17 +223,17 @@ fn getCharsAsString(p: *Parser) !Value { if (value.sstr.isValidSstr(s)) { return value.sstr.pack(s); } else if (value.istr.isValidIstr(s)) { - return p.getIstr(s); + return value.istr.pack(try p.getIstr(s)); } else { - return value.array.newString(p.alloc, s); + return try value.array.newString(p.alloc, s); } } -fn getIstr(p: *Parser, s: []const u8) !Value { +fn getIstr(p: *Parser, s: []const u8) !IstrPtr { if (p.istr_set) |set| { - return value.istr.getOrNewInSet(set, s); + return try value.istr.getOrNewInSet(set, s); } else { - return value.istr.pack(try value.istr.new(p.alloc, s)); + return try value.istr.new(p.alloc, s); } } @@ -249,7 +254,7 @@ fn addListElt(p: *Parser, elt: Value) !void { fn getList(p: *Parser) !Value { if (p.context.comment) return value.nil; if (p.list_elts.items.len == p.context.list_start) return value.nil; - defer p.list_elts.items.len = p.context.list_start; + defer p.list_elts.shrinkRetainingCapacity(p.context.list_start); const vals = p.list_elts.items[p.context.list_start..]; return value.list.new(p.alloc, p.list_pool, vals); } |
