authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-02-28 16:36:09-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-15 10:48:13-07:00
loga42888e145ced152e6437c3241b799fd39c0da6a
treea647574327fd71553ef50fbed8ea2c3d4ea61428
parent6d88c3e935357e73493a0d10cd85dd2676f6b85b

std.zig.ErrorBundle: add special representation for empty

This allows the common case of no compilation errors to be represented without any allocations.

1 files changed, 22 insertions(+), 0 deletions(-)

lib/std/zig/ErrorBundle.zig+22
......@@ -2,11 +2,21 @@
22//! so that they can be created and destroyed appropriately. This structure
33//! is used to collect all the errors from the various places into one
44//! convenient place for API users to consume.
5//!
6//! There is one special encoding for this data structure. If both arrays are
7//! empty, it means there are no errors. This special encoding exists so that
8//! heap allocation is not needed in the common case of no errors.
59
610string_bytes: []const u8,
711/// The first thing in this array is an `ErrorMessageList`.
812extra: []const u32,
913
14/// Special encoding when there are no errors.
15pub const empty: ErrorBundle = .{
16 .string_bytes = &.{},
17 .extra = &.{},
18};
19
1020// An index into `extra` pointing at an `ErrorMessage`.
1121pub const MessageIndex = enum(u32) {
1222 _,
......@@ -72,6 +82,7 @@ pub fn deinit(eb: *ErrorBundle, gpa: Allocator) void {
7282}
7383
7484pub fn errorMessageCount(eb: ErrorBundle) u32 {
85 if (eb.extra.len == 0) return 0;
7586 return eb.getErrorMessageList().len;
7687}
7788
......@@ -313,6 +324,17 @@ pub const Wip = struct {
313324
314325 pub fn toOwnedBundle(wip: *Wip) !ErrorBundle {
315326 const gpa = wip.gpa;
327 if (wip.root_list.items.len == 0) {
328 // Special encoding when there are no errors.
329 wip.deinit();
330 wip.* = .{
331 .gpa = gpa,
332 .string_bytes = .{},
333 .extra = .{},
334 .root_list = .{},
335 };
336 return empty;
337 }
316338 wip.setExtra(0, ErrorMessageList{
317339 .len = @intCast(u32, wip.root_list.items.len),
318340 .start = @intCast(u32, wip.extra.items.len),