Skip to content

Commit

Permalink
add optimized version of buffer_get_bline.
Browse files Browse the repository at this point in the history
`buffer_get_bline_w_hint` begins the bline hunt at a
parameterized bline instead of always starting at the first
bline in the buffer. when editing huge files with millions
of lines, there is a noticeable perf improvement if the
hint is nearby the desired bline.
  • Loading branch information
adsr committed Nov 15, 2020
1 parent b896a45 commit dd28398
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
40 changes: 35 additions & 5 deletions buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -694,14 +694,44 @@ int buffer_replace_w_bline(buffer_t *self, bline_t *start_line, bint_t start_col

// Return a line given a line_index
int buffer_get_bline(buffer_t *self, bint_t line_index, bline_t **ret_bline) {
bline_t *tmp_line;
return buffer_get_bline_w_hint(self, line_index, self->first_line, ret_bline);
}

// Return a line given a line_index, starting at hint and iterating outward
int buffer_get_bline_w_hint(buffer_t *self, bint_t line_index, bline_t *opt_hint, bline_t **ret_bline) {
bline_t *fwd, *rev, *found;
MLBUF_MAKE_GT_EQ0(line_index);
for (tmp_line = self->first_line; tmp_line; tmp_line = tmp_line->next) {
if (tmp_line->line_index == line_index) {
*ret_bline = tmp_line;
return MLBUF_OK;

if (!opt_hint) {
opt_hint = self->first_line;
}

fwd = opt_hint;
rev = opt_hint->prev;
found = NULL;

while (fwd || rev) {
if (fwd) {
if (fwd->line_index == line_index) {
found = fwd;
break;
}
fwd = fwd->next;
}
if (rev) {
if (rev->line_index == line_index) {
found = rev;
break;
}
rev = rev->prev;
}
}

if (found) {
*ret_bline = found;
return MLBUF_OK;
}

*ret_bline = self->last_line;
return MLBUF_ERR;
}
Expand Down
6 changes: 3 additions & 3 deletions bview.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ int bview_set_viewport_y(bview_t *self, bint_t y, int do_rectify) {
}
self->viewport_y = y;
if (do_rectify) bview_rectify_viewport(self);
buffer_get_bline(self->buffer, self->viewport_y, &self->viewport_bline);
buffer_get_bline_w_hint(self->buffer, self->viewport_y, self->viewport_bline, &self->viewport_bline);
return MLE_OK;
}

Expand Down Expand Up @@ -385,7 +385,7 @@ int bview_rectify_viewport(bview_t *self) {
if (_bview_rectify_viewport_dim(self, mark->bline, mark->bline->line_index, self->viewport_scope_y, self->rect_buffer.h, &self->viewport_y)) {
// TODO viewport_y_vrow (soft-wrapped lines, code folding, etc)
// Refresh viewport_bline
buffer_get_bline(self->buffer, self->viewport_y, &self->viewport_bline);
buffer_get_bline_w_hint(self->buffer, self->viewport_y, self->viewport_bline, &self->viewport_bline);
}

return MLE_OK;
Expand Down Expand Up @@ -519,7 +519,7 @@ static void _bview_buffer_callback(buffer_t *buffer, baction_t *action, void *ud
bview_resize(bview, bview->x, bview->y, bview->w, bview->h);
}
// Adjust viewport_bline
buffer_get_bline(bview->buffer, bview->viewport_y, &bview->viewport_bline);
buffer_get_bline_w_hint(bview->buffer, bview->viewport_y, bview->viewport_bline, &bview->viewport_bline);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion mark.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ int mark_move_to_w_bline(mark_t *self, bline_t *bline, bint_t col) {
// Move mark to line_index:col
int mark_move_to(mark_t *self, bint_t line_index, bint_t col) {
bline_t *bline;
buffer_get_bline(self->bline->buffer, line_index, &bline);
buffer_get_bline_w_hint(self->bline->buffer, line_index, self->bline, &bline);
_mark_mark_move_inner(self, bline, col, 1, 1);
return MLBUF_OK;
}
Expand Down
1 change: 1 addition & 0 deletions mlbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ int buffer_insert_w_bline(buffer_t *self, bline_t *start_line, bint_t start_col,
int buffer_delete_w_bline(buffer_t *self, bline_t *start_line, bint_t start_col, bint_t num_chars);
int buffer_replace_w_bline(buffer_t *self, bline_t *start_line, bint_t start_col, bint_t num_chars, char *data, bint_t data_len);
int buffer_get_bline(buffer_t *self, bint_t line_index, bline_t **ret_bline);
int buffer_get_bline_w_hint(buffer_t *self, bint_t line_index, bline_t *opt_hint, bline_t **ret_bline);
int buffer_get_bline_col(buffer_t *self, bint_t offset, bline_t **ret_bline, bint_t *ret_col);
int buffer_get_offset(buffer_t *self, bline_t *bline, bint_t col, bint_t *ret_offset);
int buffer_undo(buffer_t *self);
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/test_buffer_get_bline.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ void test(buffer_t *buf, mark_t *cur) {
buffer_get_bline(buf, i, &line2);
ASSERT("line", line, line2);
}
for (line = buf->first_line, i = 0; line; line = line->next, i += 1) {
buffer_get_bline_w_hint(buf, i, buf->last_line->prev, &line2);
ASSERT("line", line, line2);
}
}

0 comments on commit dd28398

Please sign in to comment.