diff options
author | Ekaitz Zarraga <ekaitz@elenq.tech> | 2024-04-14 02:53:11 +0200 |
---|---|---|
committer | Ekaitz Zarraga <ekaitz@elenq.tech> | 2024-04-14 02:53:11 +0200 |
commit | 9de2985c2ddd2d98f8299d5ff8a5921ccea5769d (patch) | |
tree | a51ed6c423aad05d8d156995e536eb0a5431431a | |
parent | 231e57a611a3c4a0cef51fb818855bcf6bacc4a6 (diff) |
-rw-r--r-- | src/piece-table.c | 14 |
1 files changed, 14 insertions, 0 deletions
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; |