Skip to content

Commit 576362a

Browse files
committed
Merge pull request #2578 from Shopify/Alex/comment-list-improvements
Fix bad scaling in `rbs_comment_t` tokens
1 parent e7d10ca commit 576362a

2 files changed

Lines changed: 26 additions & 25 deletions

File tree

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
@@ -3098,8 +3098,8 @@ static rbs_ast_comment_t *parse_comment_lines(rbs_parser_t *parser, rbs_comment_
30983098
rbs_buffer_t rbs_buffer;
30993099
rbs_buffer_init(ALLOCATOR(), &rbs_buffer);
31003100

3101-
for (size_t i = 0; i < com->line_count; i++) {
3102-
rbs_token_t tok = com->tokens[i];
3101+
for (size_t i = 0; i < com->line_tokens_count; i++) {
3102+
rbs_token_t tok = com->line_tokens[i];
31033103

31043104
const char *comment_start = parser->rbs_lexer_t->string.start + tok.range.start.byte_pos + hash_bytes;
31053105
size_t comment_bytes = RBS_RANGE_BYTES(tok.range) - hash_bytes;
@@ -3143,42 +3143,43 @@ static rbs_comment_t *comment_get_comment(rbs_comment_t *com, int line) {
31433143
}
31443144

31453145
static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *com, rbs_token_t comment_token) {
3146-
if (com->line_count == 0) {
3147-
com->start = comment_token.range.start;
3148-
}
3149-
3150-
if (com->line_count == com->line_size) {
3151-
com->line_size += 10;
3152-
3153-
if (com->tokens) {
3154-
rbs_token_t *p = com->tokens;
3155-
com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t);
3156-
memcpy(com->tokens, p, sizeof(rbs_token_t) * com->line_count);
3157-
} else {
3158-
com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t);
3159-
}
3146+
if (com->line_tokens_count == com->line_tokens_capacity) {
3147+
size_t old_size = com->line_tokens_capacity;
3148+
size_t new_size = old_size * 2;
3149+
com->line_tokens_capacity = new_size;
3150+
3151+
com->line_tokens = rbs_allocator_realloc(
3152+
allocator,
3153+
com->line_tokens,
3154+
sizeof(rbs_token_t) * old_size,
3155+
sizeof(rbs_token_t) * new_size,
3156+
rbs_token_t
3157+
);
31603158
}
31613159

3162-
com->tokens[com->line_count++] = comment_token;
3160+
com->line_tokens[com->line_tokens_count++] = comment_token;
31633161
com->end = comment_token.range.end;
31643162
}
31653163

31663164
static rbs_comment_t *alloc_comment(rbs_allocator_t *allocator, rbs_token_t comment_token, rbs_comment_t *last_comment) {
31673165
rbs_comment_t *new_comment = rbs_allocator_alloc(allocator, rbs_comment_t);
31683166

3167+
size_t initial_line_capacity = 10;
3168+
3169+
rbs_token_t *tokens = rbs_allocator_calloc(allocator, initial_line_capacity, rbs_token_t);
3170+
tokens[0] = comment_token;
3171+
31693172
*new_comment = (rbs_comment_t) {
31703173
.start = comment_token.range.start,
31713174
.end = comment_token.range.end,
31723175

3173-
.line_size = 0,
3174-
.line_count = 0,
3175-
.tokens = NULL,
3176+
.line_tokens_capacity = initial_line_capacity,
3177+
.line_tokens_count = 1,
3178+
.line_tokens = tokens,
31763179

31773180
.next_comment = last_comment,
31783181
};
31793182

3180-
comment_insert_new_line(allocator, new_comment, comment_token);
3181-
31823183
return new_comment;
31833184
}
31843185

0 commit comments

Comments
 (0)