Avoid one heap allocation when instantiating a TSTreeCursor

This commit is contained in:
Max Brunsfeld 2018-05-16 16:05:08 -07:00
parent 6fc8d9871c
commit e3670be42f
8 changed files with 96 additions and 86 deletions

View file

@ -25,13 +25,13 @@ static void range_array_add(RangeArray *results, TSPoint start, TSPoint end) {
}
typedef struct {
TSTreeCursor cursor;
TreeCursor cursor;
const TSLanguage *language;
unsigned visible_depth;
bool in_padding;
} Iterator;
static Iterator iterator_new(TSTreeCursor *cursor, const Subtree *tree, const TSLanguage *language) {
static Iterator iterator_new(TreeCursor *cursor, const Subtree *tree, const TSLanguage *language) {
array_clear(&cursor->stack);
array_push(&cursor->stack, ((TreeCursorEntry){
.subtree = tree,
@ -262,7 +262,7 @@ static inline void iterator_print_state(Iterator *self) {
#endif
unsigned ts_subtree_get_changed_ranges(const Subtree *old_tree, const Subtree *new_tree,
TSTreeCursor *cursor1, TSTreeCursor *cursor2,
TreeCursor *cursor1, TreeCursor *cursor2,
const TSLanguage *language, TSRange **ranges) {
RangeArray results = array_new();

View file

@ -1,11 +1,12 @@
#ifndef RUNTIME_GET_CHANGED_RANGES_H_
#define RUNTIME_GET_CHANGED_RANGES_H_
#include "runtime/tree_cursor.h"
#include "runtime/subtree.h"
unsigned ts_subtree_get_changed_ranges(
const Subtree *old_tree, const Subtree *new_tree,
TSTreeCursor *cursor1, TSTreeCursor *cursor2,
TreeCursor *cursor1, TreeCursor *cursor2,
const TSLanguage *language, TSRange **ranges
);

View file

@ -16,13 +16,13 @@ typedef struct {
TSNode ts_node_new(const TSTree *tree, const Subtree *subtree, Length position, TSSymbol alias) {
return (TSNode) {
{position.bytes, position.extent.row, position.extent.column, alias},
{tree, subtree},
{position.bytes, position.extent.row, position.extent.column, alias}
};
}
static inline TSNode ts_node__null() {
return (TSNode) {{NULL, NULL}, {0, 0, 0, 0}};
return ts_node_new(NULL, NULL, length_zero(), 0);
}
// TSNode - accessors

View file

@ -36,7 +36,7 @@ void ts_tree_edit(TSTree *self, const TSInputEdit *edit) {
TSRange *ts_tree_get_changed_ranges(const TSTree *self, const TSTree *other, uint32_t *count) {
TSRange *result;
TSTreeCursor cursor1, cursor2;
TreeCursor cursor1, cursor2;
ts_tree_cursor_init(&cursor1, self);
ts_tree_cursor_init(&cursor2, self);
*count = ts_subtree_get_changed_ranges(

View file

@ -4,13 +4,13 @@
#include "runtime/language.h"
#include "runtime/tree.h"
TSTreeCursor *ts_tree_cursor_new(const TSTree *tree) {
TSTreeCursor *self = ts_malloc(sizeof(TSTreeCursor));
ts_tree_cursor_init(self, tree);
TSTreeCursor ts_tree_cursor_new(const TSTree *tree) {
TSTreeCursor self;
ts_tree_cursor_init((TreeCursor *)&self, tree);
return self;
}
void ts_tree_cursor_init(TSTreeCursor *self, const TSTree *tree) {
void ts_tree_cursor_init(TreeCursor *self, const TSTree *tree) {
self->tree = tree;
array_init(&self->stack);
array_push(&self->stack, ((TreeCursorEntry) {
@ -21,12 +21,13 @@ void ts_tree_cursor_init(TSTreeCursor *self, const TSTree *tree) {
}));
}
void ts_tree_cursor_delete(TSTreeCursor *self) {
void ts_tree_cursor_delete(TSTreeCursor *_self) {
TreeCursor *self = (TreeCursor *)_self;
array_delete(&self->stack);
ts_free(self);
}
bool ts_tree_cursor_goto_first_child(TSTreeCursor *self) {
bool ts_tree_cursor_goto_first_child(TSTreeCursor *_self) {
TreeCursor *self = (TreeCursor *)_self;
TreeCursorEntry *last_entry = array_back(&self->stack);
const Subtree *tree = last_entry->subtree;
Length position = last_entry->position;
@ -62,7 +63,8 @@ bool ts_tree_cursor_goto_first_child(TSTreeCursor *self) {
return false;
}
int64_t ts_tree_cursor_goto_first_child_for_byte(TSTreeCursor *self, uint32_t goal_byte) {
int64_t ts_tree_cursor_goto_first_child_for_byte(TSTreeCursor *_self, uint32_t goal_byte) {
TreeCursor *self = (TreeCursor *)_self;
uint32_t initial_size = self->stack.size;
TreeCursorEntry *last_entry = array_back(&self->stack);
const Subtree *tree = last_entry->subtree;
@ -113,7 +115,8 @@ int64_t ts_tree_cursor_goto_first_child_for_byte(TSTreeCursor *self, uint32_t go
return -1;
}
bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *self) {
bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *_self) {
TreeCursor *self = (TreeCursor *)_self;
TreeCursorEntry *child_entry = array_back(&self->stack);
for (unsigned i = self->stack.size - 2; i + 1 > 0; i--) {
@ -142,7 +145,7 @@ bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *self) {
if (child->visible) {
return true;
} else {
ts_tree_cursor_goto_first_child(self);
ts_tree_cursor_goto_first_child(_self);
return true;
}
}
@ -155,7 +158,8 @@ bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *self) {
return false;
}
bool ts_tree_cursor_goto_parent(TSTreeCursor *self) {
bool ts_tree_cursor_goto_parent(TSTreeCursor *_self) {
TreeCursor *self = (TreeCursor *)_self;
for (unsigned i = self->stack.size - 2; i + 1 > 0; i--) {
TreeCursorEntry *entry = &self->stack.contents[i];
if (entry->subtree->visible) {
@ -166,7 +170,8 @@ bool ts_tree_cursor_goto_parent(TSTreeCursor *self) {
return false;
}
TSNode ts_tree_cursor_current_node(TSTreeCursor *self) {
TSNode ts_tree_cursor_current_node(TSTreeCursor *_self) {
TreeCursor *self = (TreeCursor *)_self;
TreeCursorEntry *last_entry = array_back(&self->stack);
TSSymbol alias_symbol = 0;
if (self->stack.size > 1) {

View file

@ -10,11 +10,11 @@ typedef struct {
uint32_t structural_child_index;
} TreeCursorEntry;
struct TSTreeCursor {
const TSTree *tree;
typedef struct {
Array(TreeCursorEntry) stack;
};
const TSTree *tree;
} TreeCursor;
void ts_tree_cursor_init(TSTreeCursor *, const TSTree *);
void ts_tree_cursor_init(TreeCursor *, const TSTree *);
#endif // RUNTIME_TREE_CURSOR_H_