Skip to content

Commit b783a56

Browse files
committed
Separate yp_node_flags_t from yp_node_type_t
Prior to this commit, we folded the flags into the type. This created extra overhead when calculating the type and setting the flags. This commit separates them.
1 parent 6004173 commit b783a56

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/yarp.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ yp_statements_node_body_append(yp_statements_node_t *node, yp_node_t *statement)
505505
// implement our own arena allocation.
506506
static inline void *
507507
yp_alloc_node(YP_ATTRIBUTE_UNUSED yp_parser_t *parser, size_t size) {
508-
void *memory = malloc(size);
508+
void *memory = calloc(1, size);
509509
if (memory == NULL) {
510510
fprintf(stderr, "Failed to allocate %zu bytes\n", size);
511511
abort();
@@ -2284,7 +2284,8 @@ yp_if_node_create(yp_parser_t *parser,
22842284

22852285
*node = (yp_if_node_t) {
22862286
{
2287-
.type = YP_NODE_IF_NODE | YP_NODE_FLAG_NEWLINE,
2287+
.flags = YP_NODE_FLAG_NEWLINE,
2288+
.type = YP_NODE_IF_NODE,
22882289
.location = {
22892290
.start = if_keyword->start,
22902291
.end = end
@@ -2310,7 +2311,8 @@ yp_if_node_modifier_create(yp_parser_t *parser, yp_node_t *statement, const yp_t
23102311

23112312
*node = (yp_if_node_t) {
23122313
{
2313-
.type = YP_NODE_IF_NODE | YP_NODE_FLAG_NEWLINE,
2314+
.flags = YP_NODE_FLAG_NEWLINE,
2315+
.type = YP_NODE_IF_NODE,
23142316
.location = {
23152317
.start = statement->location.start,
23162318
.end = predicate->location.end
@@ -2342,7 +2344,8 @@ yp_if_node_ternary_create(yp_parser_t *parser, yp_node_t *predicate, yp_node_t *
23422344

23432345
*node = (yp_if_node_t) {
23442346
{
2345-
.type = YP_NODE_IF_NODE | YP_NODE_FLAG_NEWLINE,
2347+
.flags = YP_NODE_FLAG_NEWLINE,
2348+
.type = YP_NODE_IF_NODE,
23462349
.location = {
23472350
.start = predicate->location.start,
23482351
.end = false_expression->location.end,
@@ -3746,7 +3749,7 @@ yp_statements_node_body_append(yp_statements_node_t *node, yp_node_t *statement)
37463749
node->base.location.end = statement->location.end;
37473750

37483751
// Every statement gets marked as a place where a newline can occur.
3749-
statement->type |= YP_NODE_FLAG_NEWLINE;
3752+
statement->flags = YP_NODE_FLAG_NEWLINE;
37503753
}
37513754

37523755
// Allocate a new StringConcatNode node.
@@ -4004,7 +4007,8 @@ yp_unless_node_create(yp_parser_t *parser, const yp_token_t *keyword, yp_node_t
40044007

40054008
*node = (yp_unless_node_t) {
40064009
{
4007-
.type = YP_NODE_UNLESS_NODE | YP_NODE_FLAG_NEWLINE,
4010+
.flags = YP_NODE_FLAG_NEWLINE,
4011+
.type = YP_NODE_UNLESS_NODE,
40084012
.location = {
40094013
.start = keyword->start,
40104014
.end = end
@@ -4030,7 +4034,8 @@ yp_unless_node_modifier_create(yp_parser_t *parser, yp_node_t *statement, const
40304034

40314035
*node = (yp_unless_node_t) {
40324036
{
4033-
.type = YP_NODE_UNLESS_NODE | YP_NODE_FLAG_NEWLINE,
4037+
.flags = YP_NODE_FLAG_NEWLINE,
4038+
.type = YP_NODE_UNLESS_NODE,
40344039
.location = {
40354040
.start = statement->location.start,
40364041
.end = predicate->location.end

templates/include/yarp/ast.h.erb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,31 @@ typedef struct yp_node_list {
4646
size_t capacity;
4747
} yp_node_list_t;
4848

49-
typedef enum {
49+
enum yp_node_type {
5050
<%- nodes.each_with_index do |node, index| -%>
5151
<%= node.type %> = <%= index + 1 %>,
5252
<%- end -%>
53-
} yp_node_type_t;
53+
};
54+
55+
typedef uint16_t yp_node_type_t;
56+
typedef uint16_t yp_node_flags_t;
5457

5558
// We store the node type enum in every node in the tree. We don't have nearly
5659
// as many node types as we do bits in an enum, so we're going to use the top
5760
// half of the enum to store flags about the node.
58-
#define YP_NODE_FLAGS_SHIFT (sizeof(yp_node_type_t) * 8 / 2)
59-
#define YP_NODE_FLAG_NEWLINE (1U << YP_NODE_FLAGS_SHIFT)
61+
static const unsigned int YP_NODE_FLAG_NEWLINE = 0x1;
6062

6163
// For easy access, we define some macros that manipulate only the bottom half
6264
// of the node type enum.
63-
static const unsigned int YP_NODE_TYPE_MASK = ((1 << (sizeof(yp_node_type_t) * 8 / 2)) - 1);
64-
#define YP_NODE_TYPE(node) ((node)->type & YP_NODE_TYPE_MASK)
65+
#define YP_NODE_TYPE(node) ((enum yp_node_type)node->type)
6566
#define YP_NODE_TYPE_P(node, type) (YP_NODE_TYPE(node) == (type))
6667

6768
// This is the overall tagged union representing a node in the syntax tree.
6869
typedef struct yp_node {
70+
// This represents any flags on the node. Currently, this is only a newline
71+
// flag
72+
yp_node_flags_t flags;
73+
6974
// This represents the type of the node. It somewhat maps to the nodes that
7075
// existed in the original grammar and ripper, but it's not a 1:1 mapping.
7176
yp_node_type_t type;

0 commit comments

Comments
 (0)