Native lz4

This commit is contained in:
Kenny Daniel 2024-05-19 17:14:16 -07:00
parent c7fef01eff
commit 915f0254d3
No known key found for this signature in database
GPG Key ID: 90AB653A8CAD7E45
3 changed files with 41 additions and 8 deletions

@ -25,14 +25,12 @@
},
"dependencies": {
"hysnappy": "0.3.1",
"lz4": "0.6.5",
"pako": "2.1.0"
},
"devDependencies": {
"@babel/eslint-parser": "7.24.5",
"@rollup/plugin-node-resolve": "15.2.3",
"@rollup/plugin-terser": "0.4.4",
"@types/lz4": "0.6.4",
"@types/node": "20.12.12",
"@types/pako": "2.0.3",
"@vitest/coverage-v8": "1.6.0",

@ -1,6 +1,6 @@
import { snappyUncompressor } from 'hysnappy'
import lz4 from 'lz4'
import pako from 'pako'
import { LZ4 } from './lz4.js'
/**
* @type {import('hyparquet').Compressors}
@ -10,9 +10,5 @@ export const compressors = {
GZIP: input => pako.ungzip(input),
BROTLI: () => new Uint8Array(), // TODO
ZSTD: () => new Uint8Array(), // TODO
LZ4: (input, outputLength) => {
const out = Buffer.alloc(outputLength)
lz4.decodeBlock(Buffer.from(input), out)
return out
},
LZ4,
}

39
src/lz4.js Normal file

@ -0,0 +1,39 @@
/**
* LZ4 decompression
*
* @param {Uint8Array} input
* @param {number} outputLength
* @returns {Uint8Array}
*/
export function LZ4(input, outputLength) {
const output = new Uint8Array(outputLength)
let len = 0 // output position
for (let i = 0; i < input.length;) {
const token = input[i++]
let literals = token >> 4
if (literals) {
// literal length
let byte = literals + 240
while (byte === 255) literals += byte = input[i++]
// copy literals
output.set(input.subarray(i, i + literals), len)
len += literals
i += literals
if (i >= input.length) return output
}
const offset = input[i++] | input[i++] << 8
if (!offset || offset > len) throw new Error('lz4 offset out of range')
// match length
let matchLength = (token & 0xf) + 4
let byte = matchLength + 240
while (byte === 255) matchLength += byte = input[i++]
// copy match
let pos = len - offset
const end = len + matchLength
while (len < end) output[len++] = output[pos++]
}
return output
}