From 4c2e0d3bc42eff9c1842e1b5da062c0f3007b11a Mon Sep 17 00:00:00 2001 From: Adam Saponara Date: Sun, 15 Dec 2024 14:36:44 -0500 Subject: [PATCH] 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. --- mark.c | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/mark.c b/mark.c index 115feac..ce40674 100644 --- a/mark.c +++ b/mark.c @@ -72,15 +72,11 @@ int mark_replace(mark_t *self, bint_t num_chars, char *data, bint_t data_len) { // Replace data between marks int mark_replace_between(mark_t *self, mark_t *other, char *data, bint_t data_len) { - bint_t offset_a; - bint_t offset_b; - // TODO More efficient buffer_replace_w_bline_and_end - buffer_get_offset(self->bline->buffer, self->bline, self->col, &offset_a); - buffer_get_offset(other->bline->buffer, other->bline, other->col, &offset_b); - if (offset_a < offset_b) { - return buffer_replace(self->bline->buffer, offset_a, offset_b - offset_a, data, data_len); - } - return buffer_replace(self->bline->buffer, offset_b, offset_a - offset_b, data, data_len); + bint_t nchars; + mark_t *a, *b; + mark_cmp(self, other, &a, &b); + mark_get_nchars_between(a, b, &nchars); + return bline_replace(a->bline, a->col, nchars, data, data_len); } // 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 // Delete data between self and other int mark_delete_between(mark_t *self, mark_t *other) { - bint_t offset_a; - bint_t offset_b; - // TODO More efficient buffer_replace_w_bline_and_end - buffer_get_offset(self->bline->buffer, self->bline, self->col, &offset_a); - buffer_get_offset(other->bline->buffer, other->bline, other->col, &offset_b); - if (offset_a == offset_b) { - return MLBUF_OK; - } else if (offset_a > offset_b) { - return buffer_delete(self->bline->buffer, offset_b, offset_a - offset_b); - } - return buffer_delete(self->bline->buffer, offset_a, offset_b - offset_a); + bint_t nchars; + mark_t *a, *b; + mark_cmp(self, other, &a, &b); + mark_get_nchars_between(a, b, &nchars); + return bline_delete(a->bline, a->col, nchars); } // Return data between self and other