Skip to content

Commit

Permalink
feat: massive optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jul 1, 2024
1 parent 0a4faa1 commit d8eb6c1
Show file tree
Hide file tree
Showing 26 changed files with 6,704 additions and 9,426 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,21 @@ My library beats JSON (written in C++) on all counts *and*, I see many places wh

Serialization Benchmarks:

| Value | JavaScript (ops/s) | JSON-as (ops/s) | Difference |
|----------------------------|--------------------|-----------------|------------|
| "hello world" | 28,629,598 | 64,210,666 | + 124% |
| 12345 | 31,562,431 | 56,329,066 | + 78% |
| 1.2345 | 15,977,278 | 20,322,939 | + 27% |
| [[],[[]],[[],[[]]]] | 8,998,624 | 34,453,102 | + 283% |
| { x: f64, y: f64, z: f64 } | 15,583,686 | 17,604,821 | + 12% |
| Value | JavaScript (ops/s) | JSON-AS (ops/s) | JSON-AS (Pages) | JSON-AS (SIMD+Pages)| Max Throughput |
|----------------------------|--------------------|--------------------|---------------------|---------------------|----------------|
| "hello world" | 7,124,361 | 44,290,480 (6.2x) | 73,601,235 (10.3x) | NOT IMPLEMENTED | 1.91 GB/s |
| 12345 | 9,611,677 | 66,900,642 (6.9x) | 145,924,333 (15.2x) | NOT IMPLEMENTED | 0.58 GB/s |
| 1.2345 | 7,227,259 | 20,322,939 (2.8x) | NOT IMPLEMENTED | NOT IMPLEMENTED | 0.16 GB/s |
| [[],[[]],[[],[[]]]] | 5,655,429 | 34,453,102 (6.0x) | NOT IMPLEMENTED | NOT IMPLEMENTED | 1.32 GB/s |
| { x: f64, y: f64, z: f64 } | 3,878,604 | 44,557,996 (11.5x) | 113,203,242 (29.2x) | 172,023,231 (44.4x) | 8.61 GB/s |



Deserialization Benchmarks:

| Value | JavaScript (ops/s) | JSON-AS (ops/s) | Difference|
|----------------------------|--------------------|-----------------|-----------|
| "hello world" | 12,210,131 | 24,274,496 | + 98% |
| "hello world" | 10,571,462 | 24,274,496 | + 98% |
| "12345" | 21,376,873 | 254,640,930 | + 1,191% |
| 1.2345 | 23,193,902 | 221,869,840 | + 987% |
| [[],[[]],[[],[[]]]] | 4,777,227 | 74,921,123 | + 1,568% |
Expand Down
135 changes: 135 additions & 0 deletions assembly/bl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { OBJECT, TOTAL_OVERHEAD } from "rt/common";
@inline const MAX_LEN: usize = 65536;
const STORE: usize[] = [];
let STORE_LEN: usize = 0;
const CACHE = memory.data(i32(MAX_LEN));
// Configurable amount of referenceable strings
let POINTER = changetype<usize>(CACHE);
@inline const MAX_CACHE = CACHE + MAX_LEN;

export namespace bl {
@inline export function write_b(buf: usize, bytes: usize = changetype<OBJECT>(buf - TOTAL_OVERHEAD).rtSize): void {
memory.copy(POINTER, buf, bytes);
POINTER += bytes;
if (MAX_CACHE <= POINTER) bl.shrink();
}
@inline export function write_b_u(buf: usize, bytes: usize = changetype<OBJECT>(buf - TOTAL_OVERHEAD).rtSize): void {
memory.copy(POINTER, buf, bytes);
POINTER += bytes;
}
@inline export function write_s(str: string, bytes: usize = changetype<OBJECT>(changetype<usize>(str) - TOTAL_OVERHEAD).rtSize): void {
memory.copy(POINTER, changetype<usize>(str), bytes);
POINTER += bytes;
if (MAX_CACHE <= POINTER) bl.shrink();
}
@inline export function write_s_u(str: string, bytes: usize = changetype<OBJECT>(changetype<usize>(str) - TOTAL_OVERHEAD).rtSize): void {
memory.copy(POINTER, changetype<usize>(str), bytes);
POINTER += bytes;
}
@inline export function write_s_se(str: string, start: usize, end: usize): void {
const bytes = end - start;
memory.copy(POINTER, changetype<usize>(str) + start, bytes);
POINTER += bytes;
if (MAX_CACHE <= POINTER) bl.shrink();
}
@inline export function write_s_se_u(str: string, start: usize, end: usize): void {
const bytes = end - start;
memory.copy(POINTER, changetype<usize>(str) + start, bytes);
POINTER += bytes;
}
@inline export function write_16(char: i32): void {
store<u16>(POINTER, char);
POINTER += 2;
if (MAX_CACHE <= POINTER) bl.shrink();
}
@inline export function write_16_u(char: i32): void {
store<u16>(POINTER, char);
POINTER += 2;
}

@inline export function write_32(chars: i32): void {
store<u32>(POINTER, chars);
POINTER += 4;
if (MAX_CACHE <= POINTER) bl.shrink();
}
@inline export function write_32_u(chars: i32): void {
store<u32>(POINTER, chars);
POINTER += 4;
}

@inline export function write_64(chars: i64): void {
store<u64>(POINTER, chars);
POINTER += 8;
if (MAX_CACHE <= POINTER) bl.shrink();
}

@inline export function write_64_u(chars: i64): void {
store<u64>(POINTER, chars);
POINTER += 8;
}

@inline export function write_128(chars: v128): void {
store<v128>(POINTER, chars);
POINTER += 16;
if (MAX_CACHE <= POINTER) bl.shrink();
}
@inline export function write_128_u(chars: v128): void {
store<v128>(POINTER, chars);
POINTER += 16;
if (MAX_CACHE <= POINTER) bl.shrink();
}
@inline export function shrink(): void {
const len = POINTER - CACHE;
STORE_LEN += len;
const out = __new(
len,
idof<ArrayBuffer>()
);
memory.copy(out, CACHE, len);
bl.reset();
STORE.push(out);
}
@inline export function out<T>(): T {
const len = POINTER - CACHE;
let out = __new(
len + STORE_LEN,
idof<T>()
);

memory.copy(out, CACHE, len);
if (STORE_LEN) {
out += len;
for (let i = 0; i < STORE.length; i++) {
const ptr = changetype<usize>(unchecked(STORE[i]));
const storeLen = changetype<OBJECT>(ptr - TOTAL_OVERHEAD).rtSize;
memory.copy(out, ptr, storeLen);
//__unpin(ptr);
out += storeLen;
}
STORE_LEN = 0;
}
bl.reset();

return changetype<T>(out);
}

@inline export function out_u<T>(): T {
const len = POINTER - CACHE;
const out = __new(
len + STORE_LEN,
idof<T>()
);

memory.copy(out, CACHE, len);
bl.reset();

return changetype<T>(out);
}

@inline export function _out(out: usize): void {
memory.copy(out, CACHE, POINTER - CACHE);
}
@inline export function reset(): void {
POINTER = CACHE;
}
}
14 changes: 14 additions & 0 deletions assembly/chars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,17 @@
@inline export const FORM_FEED = 12; // \f
// @ts-ignore: Decorator is valid here
@inline export const CARRIAGE_RETURN = 13; // \r

export const TRUE_PTR = changetype<usize>("true");
export const FALSE_PTR = changetype<usize>("false");
export const NULL_PTR = changetype<usize>("null");
export const EMPTY_BRACES_PTR = changetype<usize>("{}");
export const EMPTY_BRACKET_PTR = changetype<usize>("[]");
export const EMPTY_QUOTE_PTR = changetype<usize>("\"\"");
export const BACKSPACE_PTR = changetype<usize>("\\b");
export const TAB_PTR = changetype<usize>("\\t");
export const NEW_LINE_PTR = changetype<usize>("\\n");
export const FORM_FEED_PTR = changetype<usize>("\\f");
export const CARRIAGE_RETURN_PTR = changetype<usize>("\\r");
export const FOUR_DIGIT_ESC_PTR = changetype<usize>("\\u000");
export const TWO_DIGIT_ESC_PTR = changetype<usize>("\\u00");
Loading

0 comments on commit d8eb6c1

Please sign in to comment.