Skip to content

Commit f4756cd

Browse files
committed
Less const_get
Right now whenever you go to create a Ruby object from a C struct we dynamically look up the constants. This is not great for performance. Now instead we template out a constant for each VALUE that holds the classes, and then directly reference it.
1 parent 960350b commit f4756cd

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

ext/yarp/extension.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// All non-trivial logic should be in librubyparser so it can be shared its the various callers.
55

66
VALUE rb_cYARP;
7+
VALUE rb_cYARPNode;
78
VALUE rb_cYARPSource;
89
VALUE rb_cYARPToken;
910
VALUE rb_cYARPLocation;
@@ -490,6 +491,7 @@ Init_yarp(void) {
490491
// Grab up references to all of the constants that we're going to need to
491492
// reference throughout this extension.
492493
rb_cYARP = rb_define_module("YARP");
494+
rb_cYARPNode = rb_define_class_under(rb_cYARP, "Node", rb_cObject);
493495
rb_cYARPSource = rb_define_class_under(rb_cYARP, "Source", rb_cObject);
494496
rb_cYARPToken = rb_define_class_under(rb_cYARP, "Token", rb_cObject);
495497
rb_cYARPLocation = rb_define_class_under(rb_cYARP, "Location", rb_cObject);
@@ -520,6 +522,7 @@ Init_yarp(void) {
520522
rb_define_singleton_method(rb_cYARPDebug, "memsize", memsize, 1);
521523
rb_define_singleton_method(rb_cYARPDebug, "profile_file", profile_file, 1);
522524

523-
// Next, initialize the pack API.
525+
// Next, initialize the other APIs.
526+
Init_yarp_api_node();
524527
Init_yarp_pack();
525528
}

ext/yarp/extension.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ VALUE yp_source_new(yp_parser_t *parser);
1111
VALUE yp_token_new(yp_parser_t *parser, yp_token_t *token, rb_encoding *encoding, VALUE source);
1212
VALUE yp_ast_new(yp_parser_t *parser, yp_node_t *node, rb_encoding *encoding);
1313

14+
void Init_yarp_api_node(void);
1415
void Init_yarp_pack(void);
1516
YP_EXPORTED_FUNCTION void Init_yarp(void);
1617

templates/ext/yarp/api_node.c.erb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
#include "yarp/extension.h"
33

44
extern VALUE rb_cYARP;
5+
extern VALUE rb_cYARPNode;
56
extern VALUE rb_cYARPSource;
67
extern VALUE rb_cYARPToken;
78
extern VALUE rb_cYARPLocation;
89

10+
<%- nodes.each do |node| -%>
11+
static VALUE rb_cYARP<%= node.name %>;
12+
<%- end -%>
13+
914
static VALUE
1015
yp_location_new(yp_parser_t *parser, const char *start, const char *end, VALUE source) {
1116
VALUE argv[] = { source, LONG2FIX(start - parser->start), LONG2FIX(end - start) };
@@ -174,7 +179,7 @@ yp_ast_new(yp_parser_t *parser, yp_node_t *node, rb_encoding *encoding) {
174179
// location
175180
argv[<%= node.params.length %>] = yp_location_new(parser, node->location.start, node->location.end, source);
176181

177-
rb_ary_push(value_stack, rb_class_new_instance(<%= node.params.length + 1 %>, argv, rb_const_get_at(rb_cYARP, rb_intern("<%= node.name %>"))));
182+
rb_ary_push(value_stack, rb_class_new_instance(<%= node.params.length + 1 %>, argv, rb_cYARP<%= node.name %>));
178183
break;
179184
}
180185
<%- end -%>
@@ -188,3 +193,10 @@ yp_ast_new(yp_parser_t *parser, yp_node_t *node, rb_encoding *encoding) {
188193
free(constants);
189194
return result;
190195
}
196+
197+
void
198+
Init_yarp_api_node(void) {
199+
<%- nodes.each do |node| -%>
200+
rb_cYARP<%= node.name %> = rb_define_class_under(rb_cYARP, "<%= node.name %>", rb_cYARPNode);
201+
<%- end -%>
202+
}

0 commit comments

Comments
 (0)