From 9de2985c2ddd2d98f8299d5ff8a5921ccea5769d Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Sun, 14 Apr 2024 02:53:11 +0200 Subject: src: piece-table: optimize when adding to a piece in the end of the buffer --- src/piece-table.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src') diff --git a/src/piece-table.c b/src/piece-table.c index a41cf00..d77ffa0 100644 --- a/src/piece-table.c +++ b/src/piece-table.c @@ -153,6 +153,8 @@ void insert_piece_table(piece_table *pt, size_t pos, char *in, size_t in_len) { start = pt->sentinel; } else if ( pos == pt->length ) { start = pt->sentinel->prev; + pt->cached_offset = pt->length - start->length; + pt->cached = start; } else { find_piece_by_pos(pt, pos); start = split_piece(pt->cached, pos - pt->cached_offset); @@ -168,6 +170,18 @@ void insert_piece_table(piece_table *pt, size_t pos, char *in, size_t in_len) { pt->cached_offset -= pt->cached->length; } + /* Optimization! + * Add in the end of the buffer, and the end of the piece: enlarge + */ + if ( pt->add.used != 0 && + pt->cached_offset + start->length == pos && + pt->add.content + pt->add.used == + start->start + start->length + in_len ) { + pt->length += in_len; + start->length += in_len; + return; + } + /* Make a new piece and insert it */ new = make_piece(); new->start = pt->add.content + pt->add.used - in_len; new->length = in_len; -- cgit v1.2.3