From 3b1066fade0f98ec836cb4d6a2f4fc106dee1836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Sun, 4 Feb 2024 21:40:40 -0600 Subject: [PATCH] feat: more compression ig --- docs/guide/bkon.md | 7 +++++++ src/core/streams/bkon.ts | 22 +++++++--------------- src/core/streams/buffer.ts | 10 ++++++++++ 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/docs/guide/bkon.md b/docs/guide/bkon.md index 923aaebb6..d67bc5120 100644 --- a/docs/guide/bkon.md +++ b/docs/guide/bkon.md @@ -2,6 +2,13 @@ A compact binary representation similar to JSON objects. This documentation uses some funky (yet concise) syntax I developed (don't worry, my C++ isn't that bad!). +1. JSON compressed as BKON, written directly to disk (a direct 50% reduction). +2. BKON further compressed with LZ4 (a clean 90% reduction). +3. The raw JSON file as fetched from a random data source. +4. BKON decompressed once again to JSON. Note that this file is a little larger than the original JSON file as it contains a lot more decimal places due to float conversion precision. + +![an image of file sizes](https://i.imgur.com/9rBnOuS.png) + ## Primitives ```cpp diff --git a/src/core/streams/bkon.ts b/src/core/streams/bkon.ts index 8aa8cbece..742fd728e 100644 --- a/src/core/streams/bkon.ts +++ b/src/core/streams/bkon.ts @@ -190,9 +190,13 @@ export class BkonWriteStream extends WriteStream { } } } else { - // floats... ignore for now - this.uint8(ValueType.Uint32); - this.uint32(0); + if (object < -0x80000000 || object > 0x7fffffff) { + this.uint8(ValueType.Float64); + this.float64(object); + } else { + this.uint8(ValueType.Float32); + this.float32(object); + } } } else if (typeof object === 'bigint') { if (object < 0) { @@ -275,15 +279,3 @@ export class BkonWriteStream extends WriteStream { this.ascii('BKON'); } } - -// const write = new BkonWriteStream(); -// const data = (await fetch( -// 'https://raw.githubusercontent.com/tresabhi/blitzkrieg-assets/dev/definitions/tanks.json', -// ).then((response) => response.json())) as Value; -// write.bkon(data); -// const buffer = write.buffer; -// const read = new BkonReadStream(buffer); -// read.bkon(); - -// writeFile('test.bkon', buffer); -// writeFile('test.json', JSON.stringify(data)); diff --git a/src/core/streams/buffer.ts b/src/core/streams/buffer.ts index 19457a0a3..c8f0f561f 100644 --- a/src/core/streams/buffer.ts +++ b/src/core/streams/buffer.ts @@ -143,4 +143,14 @@ export class WriteStream { } return this; } + float32(value: number) { + const array = new Float32Array([value]); + this.array.push(...new Uint8Array(array.buffer)); + return this; + } + float64(value: number) { + const array = new Float64Array([value]); + this.array.push(...new Uint8Array(array.buffer)); + return this; + } }