Skip to content

Commit

Permalink
add Nbuff ctors
Browse files Browse the repository at this point in the history
  • Loading branch information
ikod committed May 2, 2020
1 parent 14ee766 commit 6532d35
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 57 deletions.
26 changes: 25 additions & 1 deletion source/nbuff/buffer.d
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,15 @@ struct Nbuff
_pages._next = new_pages;
}

this(string s) @nogc @safe
{
append(s);
}
this(immutable(ubyte)[] s) @nogc @safe
{
auto c = NbuffChunk(s);
append(c);
}
///
/// copy references to RC-data
///
Expand Down Expand Up @@ -1099,8 +1108,14 @@ struct Nbuff
_endChunkIndex -= ChunksPerPage;
}
}
void pop(int n=1) @safe @nogc
void pop(long n=1) @safe @nogc
{
assert(n <= _length);
if (n == _length)
{
clear();
return;
}
auto toPop = n;
while(toPop > 0)
{
Expand Down Expand Up @@ -1836,6 +1851,15 @@ unittest
auto n = NbuffChunk("abc");
}

@("Nbuff11")
unittest
{
// init from string
auto b = Nbuff("abc");
assert(b.length == 3);
assert(b.data.data == NbuffChunk("abc"));
}

version(Posix)
{
@("iovec")
Expand Down
63 changes: 7 additions & 56 deletions tests/bench.d
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
buildRequirements "allowWarnings"
dependency "automem" version="*"
dependency "ikod-containers" version="0.0.5"
dependency "nbuff" version="0.0.6"
dependency "nbuff" version="0.1.1"
+/

import std.datetime.stopwatch;
Expand All @@ -31,14 +31,7 @@ void main()
chunk = Nbuff.get(2*s);
b.append(chunk, s);
}
void f1()
{
Buffer b;
auto s = uniform(16, 16*1024, rnd);
b.append(new ubyte[](s));
b.append(new ubyte[](2*s));
}
auto r = benchmark!(f0,f1)(1000000);
auto r = benchmark!(f0)(1_000_000);
writefln("append, no data copy:\n%(%s\n%)", r);

auto long_string = "A".repeat().take(2*16*1024).join();
Expand All @@ -54,19 +47,7 @@ void main()
b.append(chunk, s);
}
}
void f3()
{
Buffer b;
auto limit = uniform(8, 32, rnd);
for(int i=0;i<limit;i++)
{
auto s = uniform(16, 16*1024, rnd);
auto chunk = new ubyte[](s);
copy(long_string.representation[0..s], chunk);
b.append(cast(immutable(ubyte)[])chunk);
}
}
r = benchmark!(f2, f3)(100000);
r = benchmark!(f2)(1_000_000);
writefln("append with data copy:\n%(%s\n%)", r);

void f4() @safe
Expand All @@ -82,62 +63,32 @@ void main()
}
}
}
void f5()
{
Buffer b;
auto limit = uniform(8, 64, rnd);
for(int i=0;i<limit;i++)
{
b.append("abcdef");
if ( i % 2 )
{
b.popFront();
}
}
}
r = benchmark!(f4, f5)(1000000);
r = benchmark!(f4)(1_000_000);
writefln("append string:\n%(%s\n%)", r);

Nbuff nbuff;
for(int i=0;i<100;i++)
{
nbuff.append("%d".format(i));
}
Buffer buffer;
for(int i=0;i<100;i++)
{
buffer.append("%d".format(i));
}
void f6()
{
nbuff.countUntil("10".representation);
}
void f7()
{
buffer.countUntil(0, "10".representation);
}
r = benchmark!(f6, f7)(1000000);
r = benchmark!(f6)(1_000_000);
writefln("countUntil(short):\n%(%s\n%)", r);

void f8()
{
nbuff.countUntil("90919".representation);
}
void f9()
{
buffer.countUntil(0, "90919".representation);
}
r = benchmark!(f8, f9)(1000000);
r = benchmark!(f8)(1_000_000);
writefln("countUntil(long) :\n%(%s\n%)", r);
void f10()
{
nbuff.countUntil("90919".representation, 100);
}
void f11()
{
buffer.countUntil(100, "90919".representation);
}
r = benchmark!(f10, f11)(1000000);
r = benchmark!(f10)(1_000_000);
writefln("countUntil(long,skip) :\n%(%s\n%)", r);
}

Expand Down

0 comments on commit 6532d35

Please sign in to comment.