Skip to content

Commit 9c7ecd0

Browse files
committed
Merge pull request #2667 from ruby/reuse-array
Reuse empty array and hash
1 parent 235b952 commit 9c7ecd0

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

ext/rbs_extension/ast_translation.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#include "rbs_string_bridging.h"
1212
#include "legacy_location.h"
1313

14+
VALUE EMPTY_ARRAY;
15+
VALUE EMPTY_HASH;
16+
1417
#define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1))
1518

1619
rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *constant_pool, VALUE buffer, rb_encoding *ruby_encoding) {
@@ -32,6 +35,10 @@ VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t ctx, rbs_node_list_t
3235
}
3336

3437
VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t ctx, rbs_hash_t *rbs_hash) {
38+
if (!rbs_hash->head) {
39+
return EMPTY_HASH;
40+
}
41+
3542
VALUE ruby_hash = rb_hash_new();
3643

3744
for (rbs_hash_node_t *n = rbs_hash->head; n != NULL; n = n->next) {
@@ -60,12 +67,12 @@ VALUE rbs_loc_to_ruby_location(rbs_translation_context_t ctx, rbs_location_t *so
6067
}
6168

6269
VALUE rbs_location_list_to_ruby_array(rbs_translation_context_t ctx, rbs_location_list_t *list) {
63-
VALUE ruby_array = rb_ary_new();
64-
6570
if (list == NULL) {
66-
return ruby_array;
71+
return EMPTY_ARRAY;
6772
}
6873

74+
VALUE ruby_array = rb_ary_new();
75+
6976
for (rbs_location_list_node_t *n = list->head; n != NULL; n = n->next) {
7077
rb_ary_push(ruby_array, rbs_loc_to_ruby_location(ctx, n->loc));
7178
}

ext/rbs_extension/ast_translation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@ VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t, rbs_node_list_t *li
3131
VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t, rbs_hash_t *hash);
3232
VALUE rbs_struct_to_ruby_value(rbs_translation_context_t, rbs_node_t *instance);
3333

34+
extern VALUE EMPTY_ARRAY;
35+
extern VALUE EMPTY_HASH;
36+
3437
#endif

ext/rbs_extension/main.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,12 @@ static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) {
294294
void rbs__init_parser(void) {
295295
RBS_Parser = rb_define_class_under(RBS, "Parser", rb_cObject);
296296
rb_gc_register_mark_object(RBS_Parser);
297-
VALUE empty_array = rb_obj_freeze(rb_ary_new());
298-
rb_gc_register_mark_object(empty_array);
297+
298+
EMPTY_ARRAY = rb_obj_freeze(rb_ary_new());
299+
rb_gc_register_mark_object(EMPTY_ARRAY);
300+
301+
EMPTY_HASH = rb_obj_freeze(rb_hash_new());
302+
rb_gc_register_mark_object(EMPTY_HASH);
299303

300304
rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 5);
301305
rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5);

templates/ext/rbs_extension/ast_translation.c.erb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include "rbs_string_bridging.h"
55
#include "legacy_location.h"
66

7+
VALUE EMPTY_ARRAY;
8+
VALUE EMPTY_HASH;
9+
710
#define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1))
811

912
rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *constant_pool, VALUE buffer, rb_encoding *ruby_encoding) {
@@ -25,6 +28,10 @@ VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t ctx, rbs_node_list_t
2528
}
2629

2730
VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t ctx, rbs_hash_t *rbs_hash) {
31+
if (!rbs_hash->head) {
32+
return EMPTY_HASH;
33+
}
34+
2835
VALUE ruby_hash = rb_hash_new();
2936

3037
for (rbs_hash_node_t *n = rbs_hash->head; n != NULL; n = n->next) {
@@ -53,12 +60,12 @@ VALUE rbs_loc_to_ruby_location(rbs_translation_context_t ctx, rbs_location_t *so
5360
}
5461

5562
VALUE rbs_location_list_to_ruby_array(rbs_translation_context_t ctx, rbs_location_list_t *list) {
56-
VALUE ruby_array = rb_ary_new();
57-
5863
if (list == NULL) {
59-
return ruby_array;
64+
return EMPTY_ARRAY;
6065
}
6166

67+
VALUE ruby_array = rb_ary_new();
68+
6269
for (rbs_location_list_node_t *n = list->head; n != NULL; n = n->next) {
6370
rb_ary_push(ruby_array, rbs_loc_to_ruby_location(ctx, n->loc));
6471
}

templates/ext/rbs_extension/ast_translation.h.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t, rbs_node_list_t *li
2424
VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t, rbs_hash_t *hash);
2525
VALUE rbs_struct_to_ruby_value(rbs_translation_context_t, rbs_node_t *instance);
2626

27+
extern VALUE EMPTY_ARRAY;
28+
extern VALUE EMPTY_HASH;
29+
2730
#endif

0 commit comments

Comments
 (0)