Skip to content

Commit

Permalink
Merge: Perfize FlatBuffer
Browse files Browse the repository at this point in the history
This PR introduces a bit of a performance improvement to `FlatBuffer` by limiting the number of reallocations for common cases.

Pull-Request: #2049
Reviewed-by: Jean Privat <[email protected]>
  • Loading branch information
privat committed May 3, 2016
2 parents 3fe5fcf + d26cff4 commit 6578e32
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/core/text/flat.nit
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -1005,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
Expand Down

0 comments on commit 6578e32

Please sign in to comment.