From c98f97c33f8595118079a7e6a1de24b3048b3f31 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Fri, 28 Oct 2016 12:36:41 -0400 Subject: [PATCH] Implemented log coalescing for inserts --- buf.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/buf.c b/buf.c index 72020e1..f145b60 100644 --- a/buf.c +++ b/buf.c @@ -94,12 +94,24 @@ void buf_init(Buf* buf) { } static void log_insert(Log** list, unsigned beg, unsigned end) { - Log* newlog = (Log*)calloc(sizeof(Log), 1); - newlog->insert = true; - newlog->data.ins.beg = beg; - newlog->data.ins.end = end; - newlog->next = *list; - *list = newlog; + Log* log = *list; + if (!log || log->locked || !log->insert || (log->data.ins.beg && beg < log->data.ins.beg-1) || end > log->data.ins.end+1) { + //if (log) printf("%d %d %d %d\n", log->locked, !log->insert, beg < log->data.ins.beg-1, end > log->data.ins.end+1); + //if (log) printf("%u < %u\n", beg, log->data.ins.beg-1); + //printf("new %u-%u\n", beg, end); + Log* newlog = (Log*)calloc(sizeof(Log), 1); + newlog->insert = true; + newlog->data.ins.beg = beg; + newlog->data.ins.end = end; + newlog->next = *list; + *list = newlog; + } else if (beg <= log->data.ins.beg) { + //puts("coalesce 1"); + log->data.ins.beg--; + } else { + //puts("coalesce 2"); + log->data.ins.end++; + } } static void log_delete(Log** list, unsigned off, Rune* r, size_t len) { -- 2.49.0