From 8b892ead3aaf17bb9b144b8ebba8d82d9faee082 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Tue, 2 May 2017 21:01:00 -0400 Subject: [PATCH] resize buffer on file load for efficiency --- lib/buf.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/buf.c b/lib/buf.c index 7164974..e5afc32 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -194,6 +194,12 @@ void buf_init(Buf* buf) { assert(buf->bufstart); } +static size_t next_size(size_t curr) { + int size = 1; + while(size < curr) + size = (size << 1); +} + unsigned buf_load(Buf* buf, char* path) { /* process the file path and address */ if (path && path[0] == '.' && path[1] == '/') @@ -211,6 +217,7 @@ unsigned buf_load(Buf* buf, char* path) { die("Unsupported character set"); /* read the file contents into the buffer */ + buf_resize(buf, next_size(file.len)); for (size_t i = 0; i < file.len;) { Rune r; if (buf->charset == BINARY) { -- 2.52.0