bump cppsnappy, split out benchmark
* benchmark both compression and decompression
* bump C++-snappy: it's gotten better over the years
```
fastStreams, openArrays, nimStreams, cppLib, Samples, Size, Test
0.281 / 0.139, 0.295 / 0.220, 0.528 / 0.238, 0.140 / 0.047, 100, 102400, html
2.697 / 1.457, 3.263 / 1.993, 6.106 / 2.026, 1.655 / 0.530, 100, 702087, urls.10K
0.021 / 0.011, 0.017 / 0.011, 0.031 / 0.011, 0.014 / 0.009, 100, 123093, fireworks.jpeg
0.057 / 0.019, 0.051 / 0.036, 0.092 / 0.042, 0.021 / 0.011, 100, 102400, paper-100k.pdf
1.153 / 0.565, 1.210 / 0.904, 2.172 / 0.985, 0.575 / 0.184, 100, 409600, html_x_4
0.887 / 0.582, 0.976 / 0.743, 1.923 / 0.712, 0.536 / 0.202, 100, 152089, alice29.txt
0.771 / 0.504, 0.850 / 0.639, 1.678 / 0.606, 0.482 / 0.184, 100, 129301, asyoulik.txt
2.349 / 1.518, 2.553 / 1.946, 5.057 / 1.913, 1.391 / 0.522, 100, 426754, lcet10.txt
2.983 / 1.951, 3.241 / 2.408, 6.680 / 2.342, 1.882 / 0.735, 100, 481861, plrabn12.txt
0.293 / 0.120, 0.306 / 0.210, 0.542 / 0.241, 0.131 / 0.042, 100, 118588, geo.protodata
0.743 / 0.501, 0.738 / 0.620, 1.597 / 0.640, 0.414 / 0.191, 100, 184320, kppkn.gtb
0.087 / 0.054, 0.102 / 0.067, 0.193 / 0.061, 0.052 / 0.022, 100, 14564, Mark.Twain-Tom.Sawyer.txt
66.886 / 20.880, 105.393 / 37.907, 210.197 / 38.867, 34.273 / 10.193, 10, 38942424, state-2560000-114a593d-0d5e08e8.ssz
```
In general, we're consistently about 2x slower than C++ right now.
2021-12-30 12:33:27 +00:00
|
|
|
import std/os
|
|
|
|
|
|
|
|
const
|
|
|
|
currentDir = currentSourcePath.parentDir
|
|
|
|
|
|
|
|
{.passl: "-lsnappy -L\"" & currentDir & "\" -lstdc++".}
|
|
|
|
|
|
|
|
proc snappy_compress*(input: cstring, input_length: csize_t, compressed: ptr cchar, compressed_length: var csize_t): cint {.importc, cdecl.}
|
|
|
|
proc snappy_uncompress*(compressed: cstring, compressed_length: csize_t, uncompressed: ptr cchar, uncompressed_length: var csize_t): cint {.importc, cdecl.}
|
|
|
|
proc snappy_max_compressed_length*(source_length: csize_t): csize_t {.importc, cdecl.}
|
|
|
|
proc snappy_uncompressed_length*(compressed: cstring, compressed_length: csize_t, res: var csize_t): cint {.importc, cdecl.}
|
|
|
|
|
|
|
|
proc encode*(input: openArray[byte]): seq[byte] =
|
|
|
|
result = newSeqUninitialized[byte](
|
|
|
|
snappy_max_compressed_length(input.len.csize_t))
|
|
|
|
var bytes = result.len.csize_t
|
|
|
|
let res = if input.len() == 0:
|
|
|
|
snappy_compress(nil, 0, cast[ptr cchar](result[0].addr), bytes)
|
|
|
|
else:
|
|
|
|
snappy_compress(
|
|
|
|
cast[cstring](unsafeAddr input[0]), input.len().csize_t,
|
|
|
|
cast[ptr cchar](result[0].addr), bytes)
|
|
|
|
if res != 0:
|
|
|
|
raise (ref ValueError)(msg: "Cannot compress")
|
|
|
|
|
|
|
|
result.setLen(bytes.int)
|
|
|
|
|
|
|
|
proc decode*(input: openArray[byte]): seq[byte] =
|
|
|
|
if input.len() == 0:
|
|
|
|
raise (ref ValueError)(msg: "empty input")
|
|
|
|
|
|
|
|
var bytes: csize_t
|
|
|
|
if snappy_uncompressed_length(
|
|
|
|
cast[cstring](input[0].unsafeAddr), input.len.csize_t, bytes) != 0:
|
|
|
|
raise (ref ValueError)(msg: "Cannot get length")
|
|
|
|
|
|
|
|
if bytes == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
result = newSeqUninitialized[byte](bytes.int)
|
|
|
|
|
|
|
|
if snappy_uncompress(
|
|
|
|
cast[cstring](unsafeAddr input[0]), input.len().csize_t,
|
|
|
|
cast[ptr cchar](result[0].addr), bytes) != 0:
|
2022-04-14 14:22:41 +00:00
|
|
|
raise (ref ValueError)(msg: "Cannot uncompress")
|