|
| 1 | +import { readFileSync } from "node:fs"; |
| 2 | +import { bench, describe } from "vitest"; |
| 3 | +import decodeTile from "../mltDecoder"; |
| 4 | +import { StringFsstDictionaryVector } from "../vector/fsst-dictionary/stringFsstDictionaryVector"; |
| 5 | +import { decodeFsst } from "./fsstDecoder"; |
| 6 | + |
| 7 | +type FsstDictionary = { |
| 8 | + symbols: Uint8Array; |
| 9 | + symbolLengths: Uint32Array; |
| 10 | + compressed: Uint8Array; |
| 11 | + decodedLength: number; |
| 12 | +}; |
| 13 | + |
| 14 | +let checksum = 0; |
| 15 | + |
| 16 | +function loadDictionary(tilePath: string, tableName: string, columnName: string): FsstDictionary { |
| 17 | + const tileData = readFileSync(new URL(tilePath, import.meta.url)); |
| 18 | + const featureTable = decodeTile(new Uint8Array(tileData.buffer, tileData.byteOffset, tileData.byteLength)).find( |
| 19 | + (table) => table.name === tableName, |
| 20 | + ); |
| 21 | + const dictionaryVector = featureTable?.propertyVectors.find((vector) => vector?.name === columnName); |
| 22 | + if (!(dictionaryVector instanceof StringFsstDictionaryVector)) { |
| 23 | + throw new Error(`FSST benchmark dictionary ${tableName}.${columnName} not found`); |
| 24 | + } |
| 25 | + // Access internal buffers so this benchmark isolates decodeFsst instead of measuring cached vector access. |
| 26 | + const { |
| 27 | + dataBuffer: compressed, |
| 28 | + symbolOffsetBuffer: symbolOffsets, |
| 29 | + symbolTableBuffer: symbols, |
| 30 | + offsetBuffer, |
| 31 | + } = dictionaryVector as unknown as { |
| 32 | + dataBuffer: Uint8Array; |
| 33 | + symbolOffsetBuffer: Uint32Array; |
| 34 | + symbolTableBuffer: Uint8Array; |
| 35 | + offsetBuffer: Uint32Array; |
| 36 | + }; |
| 37 | + const symbolLengths = new Uint32Array(symbolOffsets.length - 1); |
| 38 | + for (let i = 0; i < symbolLengths.length; i++) { |
| 39 | + symbolLengths[i] = symbolOffsets[i + 1] - symbolOffsets[i]; |
| 40 | + } |
| 41 | + return { |
| 42 | + symbols, |
| 43 | + symbolLengths, |
| 44 | + compressed, |
| 45 | + decodedLength: offsetBuffer[offsetBuffer.length - 1], |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +function consume(decoded: Uint8Array): void { |
| 50 | + checksum = (checksum + decoded[0] + decoded[decoded.length >> 1] + decoded[decoded.length - 1]) | 0; |
| 51 | +} |
| 52 | + |
| 53 | +describe("decode whole FSST dictionary", () => { |
| 54 | + const typical = loadDictionary("../../../test/expected/tag0x01/amazon_here/5_16_10.mlt", "pois", "name"); |
| 55 | + bench( |
| 56 | + `typical: ${typical.compressed.length} compressed bytes to ${typical.decodedLength} decoded bytes`, |
| 57 | + () => consume(decodeFsst(typical.symbols, typical.symbolLengths, typical.compressed)), |
| 58 | + { warmupTime: 500, time: 5_000 }, |
| 59 | + ); |
| 60 | + |
| 61 | + const large = loadDictionary("../../../test/expected/tag0x01/omt/14_8299_10748.mlt", "poi", "name"); |
| 62 | + bench( |
| 63 | + `large: ${large.compressed.length} compressed bytes to ${large.decodedLength} decoded bytes`, |
| 64 | + () => consume(decodeFsst(large.symbols, large.symbolLengths, large.compressed)), |
| 65 | + { warmupTime: 500, time: 5_000 }, |
| 66 | + ); |
| 67 | + |
| 68 | + const xlarge = loadDictionary("../../../test/fixtures/osm/xlarge.mlt", "hiking", "name"); |
| 69 | + bench( |
| 70 | + `xlarge: ${xlarge.compressed.length} compressed bytes to ${xlarge.decodedLength} decoded bytes`, |
| 71 | + () => consume(decodeFsst(xlarge.symbols, xlarge.symbolLengths, xlarge.compressed)), |
| 72 | + { warmupTime: 500, time: 5_000 }, |
| 73 | + ); |
| 74 | +}); |
0 commit comments