summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/piece-table.c14
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;