Skip to content

Conversation

amomchilov
Copy link
Contributor

@amomchilov amomchilov commented Jun 27, 2025

line_size was growing arithmetically (+ 10) rather than geometrically (* 2). I chose * 2 because it's a popular choice and matches rbs_buffer_t.

Here's some stats on the number of lines per comment

You can see a huge left skew:

Summary

77% of comments are 20 lines or less:

Details
Allocation stats

Before

These tables show how many comments required a particular number of reallocations.

Number of +10 reallocations count
0 197219
1 117334
2 41048
3 21232
4 7536
5 6345
6 2523
7 1624
8 1332
9 1477
10 1618
11 740
12 586
13 738
14 444
15 1464
16 298
17 303
18 4
19 146
20 3
21 2
22 2
23 148
24 292
27 2
30 149
33 2
34 2
35 146
38 2
40 146
42 2
43 146
44 2
48 146
54 146
57 2
59 146
61 146
70 2
80 146
128 146
160 2

After

Number of x2 reallocations count
0 8104
1 6556
2 32848
3 104941
4 130441
5 78311
6 30396
7 9013
8 3846
9 745
10 588
11 148

By some rough numbers, I think this changes reduces needless memory copying from 5.79TB to only 0.89 TB.

Comment on lines -3202 to -3203
com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t);
memcpy(com->tokens, p, sizeof(rbs_token_t) * com->line_count);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just re-implementing realloc

src/parser.c Outdated
@@ -3213,19 +3211,22 @@ static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *c
static rbs_comment_t *alloc_comment(rbs_allocator_t *allocator, rbs_token_t comment_token, rbs_comment_t *last_comment) {
rbs_comment_t *new_comment = rbs_allocator_alloc(allocator, rbs_comment_t);

size_t initial_line_size = 10;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same initial size this code used to use before. I haven't spent any time tuning it.

@amomchilov amomchilov force-pushed the Alex/comment-list-improvements branch from 8c30976 to 1dbb362 Compare July 2, 2025 14:15
@amomchilov amomchilov marked this pull request as ready for review July 2, 2025 14:16
@amomchilov amomchilov force-pushed the Alex/comment-list-improvements branch from 1dbb362 to c0c8021 Compare July 2, 2025 15:23
Comment on lines +30 to +32
size_t line_tokens_capacity;
size_t line_tokens_count;
rbs_token_t *line_tokens;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This naming was confusing, because line_size sounds like "the size of a line".

What is actually means is "the capacity of the line_tokens buffer.

@@ -3160,42 +3160,43 @@ static rbs_comment_t *comment_get_comment(rbs_comment_t *com, int line) {
}

static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *com, rbs_token_t comment_token) {
if (com->line_count == 0) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By changing alloc_comment() to not call comment_insert_new_line(), we never have to deal with the 0 case here 🥳

Copy link
Member

@soutaro soutaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@soutaro
Copy link
Member

soutaro commented Jul 17, 2025

@amomchilov Can you take a look at the clang-format failure? Simply rebasing the branch on the top of master would solve the issue.

@amomchilov amomchilov force-pushed the Alex/comment-list-improvements branch from c0c8021 to be715d4 Compare July 17, 2025 22:42
@amomchilov
Copy link
Contributor Author

@soutaro Done!

@soutaro soutaro added this pull request to the merge queue Jul 24, 2025
Merged via the queue into ruby:master with commit 0f3ceb6 Jul 24, 2025
21 checks passed
@ksss ksss added this to the RBS 4.0 milestone Jul 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants