authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-20 23:47:04-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-20 23:47:04-04:00
logd57d9448aa322c1818de110adeb3cc69ac5dbcd9
tree9bef690df27fc49b002aef664725fb01a892351d
parent3c5d581ce35b137a7b80ac1431c1d9132e281fef

stage2 parsing: rework block statements AST memory layout

block statements are now directly following the Block AST node rather than a singly linked list. This had negligible impact on performance: throughput: 72.3 MiB/s => 72.7 MiB/s however it greatly improves the API since the statements are laid out in a flat array in memory.

3 files changed, 48 insertions(+), 18 deletions(-)

lib/std/zig/ast.zig+36-7
......@@ -804,6 +804,7 @@ pub const Node = struct {
804804 }
805805 };
806806
807 /// The fields and decls Node pointers directly follow this struct in memory.
807808 pub const ContainerDecl = struct {
808809 base: Node = Node{ .id = .ContainerDecl },
809810 kind_token: TokenIndex,
......@@ -1188,23 +1189,37 @@ pub const Node = struct {
11881189 }
11891190 };
11901191
1192 /// The statements of the block follow Block directly in memory.
11911193 pub const Block = struct {
11921194 base: Node = Node{ .id = .Block },
1193 label: ?TokenIndex,
1195 statements_len: NodeIndex,
11941196 lbrace: TokenIndex,
1195 statements: StatementList,
11961197 rbrace: TokenIndex,
1198 label: ?TokenIndex,
11971199
1198 pub const StatementList = LinkedList(*Node);
1200 /// After this the caller must initialize the statements list.
1201 pub fn alloc(allocator: *mem.Allocator, statements_len: NodeIndex) !*Block {
1202 const bytes = try allocator.alignedAlloc(u8, @alignOf(Block), sizeInBytes(statements_len));
1203 return @ptrCast(*Block, bytes.ptr);
1204 }
1205
1206 pub fn free(self: *Block, allocator: *mem.Allocator) void {
1207 const bytes = @ptrCast([*]u8, self)[0..sizeInBytes(self.statements_len)];
1208 allocator.free(bytes);
1209 }
11991210
12001211 pub fn iterate(self: *const Block) Node.Iterator {
1201 return .{ .parent_node = &self.base, .index = 0, .node = self.statements.first };
1212 return .{ .parent_node = &self.base, .index = 0, .node = null };
12021213 }
12031214
12041215 pub fn iterateNext(self: *const Block, it: *Node.Iterator) ?*Node {
1205 const child = it.node orelse return null;
1206 it.node = child.next;
1207 return child.data;
1216 var i = it.index;
1217 it.index += 1;
1218
1219 if (i < self.statements_len) return self.statementsConst()[i];
1220 i -= self.statements_len;
1221
1222 return null;
12081223 }
12091224
12101225 pub fn firstToken(self: *const Block) TokenIndex {
......@@ -1218,6 +1233,20 @@ pub const Node = struct {
12181233 pub fn lastToken(self: *const Block) TokenIndex {
12191234 return self.rbrace;
12201235 }
1236
1237 pub fn statements(self: *Block) []*Node {
1238 const decls_start = @ptrCast([*]u8, self) + @sizeOf(Block);
1239 return @ptrCast([*]*Node, decls_start)[0..self.statements_len];
1240 }
1241
1242 pub fn statementsConst(self: *const Block) []const *Node {
1243 const decls_start = @ptrCast([*]const u8, self) + @sizeOf(Block);
1244 return @ptrCast([*]const *Node, decls_start)[0..self.statements_len];
1245 }
1246
1247 fn sizeInBytes(statements_len: NodeIndex) usize {
1248 return @sizeOf(Block) + @sizeOf(*Node) * @as(usize, statements_len);
1249 }
12211250 };
12221251
12231252 pub const Defer = struct {
lib/std/zig/parse.zig+7-5
......@@ -1178,8 +1178,9 @@ const Parser = struct {
11781178 fn parseBlock(p: *Parser) !?*Node {
11791179 const lbrace = p.eatToken(.LBrace) orelse return null;
11801180
1181 var statements = Node.Block.StatementList{};
1182 var statements_it = &statements.first;
1181 var statements = std.ArrayList(*Node).init(p.gpa);
1182 defer statements.deinit();
1183
11831184 while (true) {
11841185 const statement = (p.parseStatement() catch |err| switch (err) {
11851186 error.OutOfMemory => return error.OutOfMemory,
......@@ -1189,18 +1190,19 @@ const Parser = struct {
11891190 continue;
11901191 },
11911192 }) orelse break;
1192 statements_it = try p.llpush(*Node, statements_it, statement);
1193 try statements.append(statement);
11931194 }
11941195
11951196 const rbrace = try p.expectToken(.RBrace);
11961197
1197 const block_node = try p.arena.allocator.create(Node.Block);
1198 const block_node = try Node.Block.alloc(&p.arena.allocator, statements.items.len);
11981199 block_node.* = .{
11991200 .label = null,
12001201 .lbrace = lbrace,
1201 .statements = statements,
1202 .statements_len = statements.items.len,
12021203 .rbrace = rbrace,
12031204 };
1205 std.mem.copy(*Node, block_node.statements(), statements.items);
12041206
12051207 return &block_node.base;
12061208 }
lib/std/zig/render.zig+5-6
......@@ -358,21 +358,20 @@ fn renderExpression(
358358 try renderToken(tree, stream, tree.nextToken(label), indent, start_col, Space.Space);
359359 }
360360
361 if (block.statements.first == null) {
361 if (block.statements_len == 0) {
362362 try renderToken(tree, stream, block.lbrace, indent + indent_delta, start_col, Space.None);
363363 return renderToken(tree, stream, block.rbrace, indent, start_col, space);
364364 } else {
365365 const block_indent = indent + indent_delta;
366366 try renderToken(tree, stream, block.lbrace, block_indent, start_col, Space.Newline);
367367
368 var it = block.statements.first;
369 while (it) |statement_node| : (it = statement_node.next) {
370 const statement = statement_node.data;
368 const block_statements = block.statements();
369 for (block_statements) |statement, i| {
371370 try stream.writeByteNTimes(' ', block_indent);
372371 try renderStatement(allocator, stream, tree, block_indent, start_col, statement);
373372
374 if (statement_node.next) |next_statement| {
375 try renderExtraNewline(tree, stream, start_col, next_statement.data);
373 if (i + 1 < block_statements.len) {
374 try renderExtraNewline(tree, stream, start_col, block_statements[i + 1]);
376375 }
377376 }
378377