From dd632cfe55ad300e8b8c23463791a1433ccb4e27 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Fri, 29 Apr 2016 16:15:24 -0400 Subject: [PATCH 1/3] lib/core: Improve FlatBuffer re-allocation rate by pre-allocating at least 16 bytes Signed-off-by: Lucas Bajolet --- lib/core/text/flat.nit | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/core/text/flat.nit b/lib/core/text/flat.nit index 0fc56637f1..25333233ee 100644 --- a/lib/core/text/flat.nit +++ b/lib/core/text/flat.nit @@ -933,12 +933,13 @@ class FlatBuffer do var c = capacity if cap <= c then return - while c <= cap do c = c * 2 + 2 + if c <= 16 then c = 16 + while c <= cap do c = c * 2 # The COW flag can be set at false here, since # it does a copy of the current `Buffer` written = false var bln = _bytelen - var a = new NativeString(c+1) + var a = new NativeString(c) if bln > 0 then var it = _items if bln > 0 then it.copy_to(a, bln, 0, 0) From 12c1403570074a02e5258053048ab4f0d366fd41 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Mon, 2 May 2016 15:32:26 -0400 Subject: [PATCH 2/3] lib/core: Improved running time for reset Signed-off-by: Lucas Bajolet --- lib/core/text/flat.nit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core/text/flat.nit b/lib/core/text/flat.nit index 25333233ee..c3a2fed16e 100644 --- a/lib/core/text/flat.nit +++ b/lib/core/text/flat.nit @@ -922,9 +922,9 @@ class FlatBuffer redef fun clear do is_dirty = true - if written then reset _bytelen = 0 _length = 0 + if written then reset end redef fun empty do return new Buffer From d26cff45434a5e2f5d1b184487e974aa750d103b Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Mon, 2 May 2016 23:27:28 -0400 Subject: [PATCH 3/3] lib/core: Stop allocating 1 byte more when initializing a `FlatBuffer` with capacity Signed-off-by: Lucas Bajolet --- lib/core/text/flat.nit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core/text/flat.nit b/lib/core/text/flat.nit index c3a2fed16e..45ad144d4a 100644 --- a/lib/core/text/flat.nit +++ b/lib/core/text/flat.nit @@ -1006,7 +1006,7 @@ class FlatBuffer init with_capacity(cap: Int) do assert cap >= 0 - _items = new NativeString(cap + 1) + _items = new NativeString(cap) capacity = cap _bytelen = 0 end