|
| 1 | +import { bs } from "../../../lib/as-bs"; |
| 2 | +import { BACK_SLASH } from "../../custom/chars"; |
| 3 | +import { SERIALIZE_ESCAPE_TABLE } from "../../globals/tables"; |
| 4 | +import { bytes } from "../../util/bytes"; |
| 5 | + |
| 6 | +// @ts-ignore: decorator allowed |
| 7 | +@lazy const QUOTE_MASK = 0x0022_0022_0022_0022; |
| 8 | +// @ts-ignore: decorator allowed |
| 9 | +@lazy const U00_MARKER = 13511005048209500; |
| 10 | + |
| 11 | +export function serializeString_SWAR(src: string): void { |
| 12 | + const srcSize = bytes(src); |
| 13 | + let srcStart = changetype<usize>(src); |
| 14 | + const srcEnd = srcStart + srcSize; |
| 15 | + const srcEnd8 = srcEnd - 8; |
| 16 | + |
| 17 | + bs.proposeSize(srcSize + 4); |
| 18 | + store<u16>(bs.offset, 34); // " |
| 19 | + bs.offset += 2; |
| 20 | + |
| 21 | + while (srcStart <= srcEnd8) { |
| 22 | + const block = load<u64>(srcStart); |
| 23 | + store<u64>(bs.offset, block); |
| 24 | + |
| 25 | + const quotes = COMP(block, QUOTE_MASK); |
| 26 | + let mask = quotes; |
| 27 | + |
| 28 | + |
| 29 | + while (mask != 0) { |
| 30 | + const lane_index = usize(ctz(mask) >> 3); |
| 31 | + // console.log("lane index " + lane_index.toString()); |
| 32 | + const src_offset = srcStart + lane_index; |
| 33 | + const code = load<u16>(src_offset) << 2; |
| 34 | + const escaped = load<u32>(SERIALIZE_ESCAPE_TABLE + code); |
| 35 | + |
| 36 | + if ((escaped & 0xffff) != BACK_SLASH) { |
| 37 | + bs.growSize(10); |
| 38 | + const dst_offset = bs.offset + lane_index; |
| 39 | + store<u64>(dst_offset, U00_MARKER); |
| 40 | + store<u32>(dst_offset, escaped, 8); |
| 41 | + store<u64>(dst_offset, load<u64>(src_offset, 2), 12); // unsafe. can overflow here |
| 42 | + bs.offset += 10; |
| 43 | + } else { |
| 44 | + bs.growSize(2); |
| 45 | + const dst_offset = bs.offset + lane_index; |
| 46 | + store<u32>(dst_offset, escaped); |
| 47 | + store<u64>(dst_offset, load<u64>(src_offset, 2), 4); |
| 48 | + bs.offset += 2; |
| 49 | + } |
| 50 | + |
| 51 | + mask &= mask - 1; |
| 52 | + } |
| 53 | + |
| 54 | + srcStart += 8; |
| 55 | + bs.offset += 8; |
| 56 | + } |
| 57 | + |
| 58 | + while (srcStart <= srcEnd - 2) { |
| 59 | + const code = load<u16>(srcStart); |
| 60 | + if (code == 92 || code == 34 || code < 32) { |
| 61 | + const escaped = load<u32>(SERIALIZE_ESCAPE_TABLE + (code << 2)); |
| 62 | + if ((escaped & 0xffff) != BACK_SLASH) { |
| 63 | + bs.growSize(10); |
| 64 | + store<u64>(bs.offset, U00_MARKER); |
| 65 | + store<u32>(bs.offset, escaped, 8); |
| 66 | + bs.offset += 12; |
| 67 | + } else { |
| 68 | + bs.growSize(2); |
| 69 | + store<u32>(bs.offset, escaped); |
| 70 | + bs.offset += 4; |
| 71 | + } |
| 72 | + } else { |
| 73 | + store<u16>(bs.offset, code); |
| 74 | + bs.offset += 2; |
| 75 | + } |
| 76 | + srcStart += 2; |
| 77 | + } |
| 78 | + |
| 79 | + store<u16>(bs.offset, 34); // " |
| 80 | + bs.offset += 2; |
| 81 | +} |
| 82 | + |
| 83 | +// @ts-ignore: decorators allowed |
| 84 | +@inline function COMP(x: u64, y: u64): u64 { |
| 85 | + const LANE_MASK = 0xFF00_FF00_FF00_FF00; |
| 86 | + const xored = (x ^ LANE_MASK) ^ y; |
| 87 | + const mask = (((xored >> 1) | 0x8080808080808080) - xored) & 0x8080808080808080; |
| 88 | + return (mask << 1) - (mask >> 7) & 0x8080808080808080; |
| 89 | +} |
0 commit comments