| ... | @@ -14,13 +14,16 @@ pub const Error = error{ParseError} || Allocator.Error; | ... | @@ -14,13 +14,16 @@ pub const Error = error{ParseError} || Allocator.Error; |
| 14 | /// Result should be freed with tree.deinit() when there are | 14 | /// Result should be freed with tree.deinit() when there are |
| 15 | /// no more references to any of the tokens or nodes. | 15 | /// no more references to any of the tokens or nodes. |
| 16 | pub fn parse(gpa: *Allocator, source: []const u8) Allocator.Error!*Tree { | 16 | pub fn parse(gpa: *Allocator, source: []const u8) Allocator.Error!*Tree { |
| 17 | // TODO optimization idea: ensureCapacity on the tokens list and | | |
| 18 | // then appendAssumeCapacity inside the loop. | | |
| 19 | var token_ids = std.ArrayList(Token.Id).init(gpa); | 17 | var token_ids = std.ArrayList(Token.Id).init(gpa); |
| 20 | defer token_ids.deinit(); | 18 | defer token_ids.deinit(); |
| 21 | var token_locs = std.ArrayList(Token.Loc).init(gpa); | 19 | var token_locs = std.ArrayList(Token.Loc).init(gpa); |
| 22 | defer token_locs.deinit(); | 20 | defer token_locs.deinit(); |
| 23 | | 21 | |
| | 22 | // Empirically, the zig std lib has an 8:1 ratio of source bytes to token count. |
| | 23 | const estimated_token_count = source.len / 8; |
| | 24 | try token_ids.ensureCapacity(estimated_token_count); |
| | 25 | try token_locs.ensureCapacity(estimated_token_count); |
| | 26 | |
| 24 | var tokenizer = std.zig.Tokenizer.init(source); | 27 | var tokenizer = std.zig.Tokenizer.init(source); |
| 25 | while (true) { | 28 | while (true) { |
| 26 | const token = tokenizer.next(); | 29 | const token = tokenizer.next(); |