Skip to content

Commit 4c2e0d3

Browse files
committed
improve perf of mark_(replace|delete)_between
previously we were using `buffer_get_offset` to get both mark offsets (slow) and then `buffer_(replace|delete)` which in turn converted one of the offsets back into a bline and col before finally invoking `buffer_(replace|delete)_w_bline`. now we're calculating nchars via `mark_get_nchars_between` and calling `buffer_(replace|delete)_w_bline` more directly via `bline_(replace|delete)`). this could be made even more efficient if we had buffer functions that accepted two marks instead of a mark and an offset.
1 parent 9e94716 commit 4c2e0d3

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

mark.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,11 @@ int mark_replace(mark_t *self, bint_t num_chars, char *data, bint_t data_len) {
7272

7373
// Replace data between marks
7474
int mark_replace_between(mark_t *self, mark_t *other, char *data, bint_t data_len) {
75-
bint_t offset_a;
76-
bint_t offset_b;
77-
// TODO More efficient buffer_replace_w_bline_and_end
78-
buffer_get_offset(self->bline->buffer, self->bline, self->col, &offset_a);
79-
buffer_get_offset(other->bline->buffer, other->bline, other->col, &offset_b);
80-
if (offset_a < offset_b) {
81-
return buffer_replace(self->bline->buffer, offset_a, offset_b - offset_a, data, data_len);
82-
}
83-
return buffer_replace(self->bline->buffer, offset_b, offset_a - offset_b, data, data_len);
75+
bint_t nchars;
76+
mark_t *a, *b;
77+
mark_cmp(self, other, &a, &b);
78+
mark_get_nchars_between(a, b, &nchars);
79+
return bline_replace(a->bline, a->col, nchars, data, data_len);
8480
}
8581

8682
// Move mark to bline:col
@@ -364,17 +360,11 @@ int mark_find_bracket_pair(mark_t *self, bint_t max_chars, bline_t **ret_line, b
364360

365361
// Delete data between self and other
366362
int mark_delete_between(mark_t *self, mark_t *other) {
367-
bint_t offset_a;
368-
bint_t offset_b;
369-
// TODO More efficient buffer_replace_w_bline_and_end
370-
buffer_get_offset(self->bline->buffer, self->bline, self->col, &offset_a);
371-
buffer_get_offset(other->bline->buffer, other->bline, other->col, &offset_b);
372-
if (offset_a == offset_b) {
373-
return MLBUF_OK;
374-
} else if (offset_a > offset_b) {
375-
return buffer_delete(self->bline->buffer, offset_b, offset_a - offset_b);
376-
}
377-
return buffer_delete(self->bline->buffer, offset_a, offset_b - offset_a);
363+
bint_t nchars;
364+
mark_t *a, *b;
365+
mark_cmp(self, other, &a, &b);
366+
mark_get_nchars_between(a, b, &nchars);
367+
return bline_delete(a->bline, a->col, nchars);
378368
}
379369

380370
// Return data between self and other

0 commit comments

Comments
 (0)