Skip to content

Commit 0f3ceb6

Browse files
authored
Merge pull request #2578 from Shopify/Alex/comment-list-improvements
Fix bad scaling in `rbs_comment_t` tokens
2 parents 262302d + be715d4 commit 0f3ceb6

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

include/rbs/parser.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ typedef struct rbs_comment_t {
2727
rbs_position_t start;
2828
rbs_position_t end;
2929

30-
size_t line_size;
31-
size_t line_count;
32-
rbs_token_t *tokens;
30+
size_t line_tokens_capacity;
31+
size_t line_tokens_count;
32+
rbs_token_t *line_tokens;
3333

3434
struct rbs_comment_t *next_comment;
3535
} rbs_comment_t;

src/parser.c

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3182,8 +3182,8 @@ static rbs_ast_comment_t *parse_comment_lines(rbs_parser_t *parser, rbs_comment_
31823182
rbs_buffer_t rbs_buffer;
31833183
rbs_buffer_init(ALLOCATOR(), &rbs_buffer);
31843184

3185-
for (size_t i = 0; i < com->line_count; i++) {
3186-
rbs_token_t tok = com->tokens[i];
3185+
for (size_t i = 0; i < com->line_tokens_count; i++) {
3186+
rbs_token_t tok = com->line_tokens[i];
31873187

31883188
const char *comment_start = parser->rbs_lexer_t->string.start + tok.range.start.byte_pos + hash_bytes;
31893189
size_t comment_bytes = RBS_RANGE_BYTES(tok.range) - hash_bytes;
@@ -3227,42 +3227,43 @@ static rbs_comment_t *comment_get_comment(rbs_comment_t *com, int line) {
32273227
}
32283228

32293229
static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *com, rbs_token_t comment_token) {
3230-
if (com->line_count == 0) {
3231-
com->start = comment_token.range.start;
3232-
}
3233-
3234-
if (com->line_count == com->line_size) {
3235-
com->line_size += 10;
3236-
3237-
if (com->tokens) {
3238-
rbs_token_t *p = com->tokens;
3239-
com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t);
3240-
memcpy(com->tokens, p, sizeof(rbs_token_t) * com->line_count);
3241-
} else {
3242-
com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t);
3243-
}
3230+
if (com->line_tokens_count == com->line_tokens_capacity) {
3231+
size_t old_size = com->line_tokens_capacity;
3232+
size_t new_size = old_size * 2;
3233+
com->line_tokens_capacity = new_size;
3234+
3235+
com->line_tokens = rbs_allocator_realloc(
3236+
allocator,
3237+
com->line_tokens,
3238+
sizeof(rbs_token_t) * old_size,
3239+
sizeof(rbs_token_t) * new_size,
3240+
rbs_token_t
3241+
);
32443242
}
32453243

3246-
com->tokens[com->line_count++] = comment_token;
3244+
com->line_tokens[com->line_tokens_count++] = comment_token;
32473245
com->end = comment_token.range.end;
32483246
}
32493247

32503248
static rbs_comment_t *alloc_comment(rbs_allocator_t *allocator, rbs_token_t comment_token, rbs_comment_t *last_comment) {
32513249
rbs_comment_t *new_comment = rbs_allocator_alloc(allocator, rbs_comment_t);
32523250

3251+
size_t initial_line_capacity = 10;
3252+
3253+
rbs_token_t *tokens = rbs_allocator_calloc(allocator, initial_line_capacity, rbs_token_t);
3254+
tokens[0] = comment_token;
3255+
32533256
*new_comment = (rbs_comment_t) {
32543257
.start = comment_token.range.start,
32553258
.end = comment_token.range.end,
32563259

3257-
.line_size = 0,
3258-
.line_count = 0,
3259-
.tokens = NULL,
3260+
.line_tokens_capacity = initial_line_capacity,
3261+
.line_tokens_count = 1,
3262+
.line_tokens = tokens,
32603263

32613264
.next_comment = last_comment,
32623265
};
32633266

3264-
comment_insert_new_line(allocator, new_comment, comment_token);
3265-
32663267
return new_comment;
32673268
}
32683269

0 commit comments

Comments
 (0)