Fast, SIMD hex string encoder and decoder C++ lib and Node.js module
A fast, SIMD (vectorized) hex string encoder/decoder, available as a stand-alone C++ module and as a Node.js module.
I think these implementations are close to optimal, but PRs are welcome for optimizations.
Pull in src/hex.h
and src/hex.cc
, and adjust your build appropriately
(GCC/Clang/ICC: -march=haswell
for example; MSVC: set
EnableAdvancedInstructionSet
to "AVX2" or /arch:AVX2
).
See hex.h
for the exported functions. There are three decoder implementations
and two encoder implementations, with the same signature:
// Decodes src hex string into dest bytes.
// len is number of dest bytes (1/2 the size of src).
void decodeHex___(uint8_t* __restrict__ dest, const uint8_t* __restrict__ src, size_t len);
// Encodes src bytes into dest hex string.
// len is number of src bytes (dest must be twice the size of src).
void encodeHex___(uint8_t* __restrict__ dest, const uint8_t* __restrict__ src, size_t len);
Benchmark
const {decodeHexVec, encodeHexVec} = require("fast-hex");
decodeHexVec(output: Uint8Array|Buffer, input: Uint8Array|Buffer): void;
encodeHexVec(input: Uint8Array|Buffer): string;
Benchmark
Development notes:
buffer.toString()
, (b) via the C++ API
v8::Local<v8::Value> node::Encode(v8::Isolate* isolate, const char* buf, size_t len, enum encoding encoding)
.
Haven't looked yet at what cases cause v8 to automatically use external
strings.stream.write
uses StringBytes::Encode
internally. Can't replace this
without modifying node.js, unless we intercept stream.write
in JS.Supported compilers: