Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | 106x 314x 314x 314x 79278x 79278x 2222x 2222x 2222x 1851x 1851x 1851x 1x 1x 1x 1x 1851x 77427x 77427x 71530x 71530x 71530x 77427x 8362x 8362x 2x 2x 2055x 5x 2x 2x 248x 151x 3089x 106x 249x 249x 249x 249x 249x 249x 1691x 6798x 1690x 1690x 1690x 1690x 1689x 1689x 1x 248x 248x 248x 248x 248x 249x 249x 1690x 1690x 1690x 1690x 1690x 1690x 1690x 32x 32x 1690x 1x 1x 248x 5068x 742x 23x 5x 496x 10x 2x 496x 10x 10x 1112x 10x 3x 3x 138x 138x 138x 1539x 1539x 1539x 32x 1539x 1539x 1539x 138x 5069x | import type { BeMusicEvent, BmsonEventExtensions, LRXEventExtensions } from '@be-music/chart-source';
/** Lookups after which a load's string table also indexes two-character ids by their character codes. */
const PAIR_TABLE_AFTER = 2048;
/**
* String table shared by every chart packed during one collection load. Event channels and values are a small
* vocabulary (`"01"`, `"11"`, `"0A"`, …), so each packed event stores two small integers instead of two strings.
*/
export class PackedEventStrings {
private readonly values: string[] = [];
private readonly ids = new Map<string, number>();
/**
* Ids of two-character ASCII strings, nearly every channel and object id, by their two character codes: looked up per
* event without hashing the string. Built only once a load has looked up {@link PAIR_TABLE_AFTER} values, so a small
* load does not pay for the table; `-1` marks a pair not seen yet.
*/
private asciiPairIds: Int32Array | undefined;
private lookups = 0;
idOf(value: string): number {
const pairIds = this.asciiPairIds ?? this.buildPairTableWhenBusy();
if (pairIds !== undefined && value.length === 2) {
const high = value.charCodeAt(0);
const low = value.charCodeAt(1);
if (high < 128 && low < 128) {
const slot = (high << 7) | low;
let id = pairIds[slot]!;
if (id < 0) {
id = this.values.length;
this.values.push(value);
this.ids.set(value, id);
pairIds[slot] = id;
}
return id;
}
}
let id = this.ids.get(value);
if (id === undefined) {
id = this.values.length;
this.values.push(value);
this.ids.set(value, id);
}
return id;
}
private buildPairTableWhenBusy(): Int32Array | undefined {
this.lookups += 1;
if (this.lookups < PAIR_TABLE_AFTER) return undefined;
const pairIds = new Int32Array(128 * 128).fill(-1);
for (const [value, id] of this.ids) {
if (value.length === 2 && value.charCodeAt(0) < 128 && value.charCodeAt(1) < 128) {
pairIds[(value.charCodeAt(0) << 7) | value.charCodeAt(1)] = id;
}
}
this.asciiPairIds = pairIds;
return pairIds;
}
get size(): number {
return this.values.length;
}
snapshot(): string[] {
return this.values.slice();
}
valueOf(id: number): string {
return this.values[id]!;
}
}
/** A column of signed integers, as narrow as its values allow. */
export type PackedIntegers = Int8Array | Int16Array | Int32Array;
/** A column of string-table ids, as narrow as its largest id allows. */
export type PackedIds = Uint8Array | Uint16Array | Uint32Array;
/**
* Column-oriented copy of a chart's `events`. Each column is as narrow as its values allow, which for a typical chart
* comes to 6 to 10 bytes per event (20 at the widest) instead of the ~130 bytes an event object, its `position` tuple
* and their headers take on the heap. Every chart of a library keeps its columns for as long as it is listed, and a
* library read from the parsed-chart cache copies them all, so their size is what the library weighs.
*/
export interface PackedEvents {
readonly length: number;
readonly measures: PackedIntegers;
readonly channels: PackedIds;
readonly values: PackedIds;
readonly numerators: PackedIntegers | Float64Array;
readonly denominators: PackedIntegers | Float64Array;
/** bmson per-note extensions, by event index. Only bmson charts carry them. */
readonly bmson: ReadonlyMap<number, BmsonEventExtensions> | undefined;
/** LRX per-note values, by event index. Only LRX charts carry them. */
readonly lrx?: ReadonlyMap<number, LRXEventExtensions>;
}
const EVENT_KEYS = new Set(['measure', 'channel', 'position', 'value', 'bmson', 'lrx']);
/**
* Packs `events` into columns. Returns `undefined` when an event carries a field this format doesn't model, in which
* case the caller keeps the original array so nothing is lost.
*/
export function packEvents(events: readonly BeMusicEvent[], strings: PackedEventStrings): PackedEvents | undefined {
const length = events.length;
let integral = true;
let measureBits = 0;
let numeratorBits = 0;
let denominatorBits = 0;
for (const event of events) {
for (const key in event) {
if (!EVENT_KEYS.has(key)) return undefined;
}
Iif (!isInt32(event.measure)) return undefined;
measureBits |= bitsForSignedWidth(event.measure);
const [numerator, denominator] = event.position;
if (integral && isInt32(numerator) && isInt32(denominator)) {
numeratorBits |= bitsForSignedWidth(numerator);
denominatorBits |= bitsForSignedWidth(denominator);
} else {
integral = false;
}
}
const measures = createIntegerColumn(length, measureBits);
const largestPossibleId = strings.size + length * 2 - 1;
const channels = createIdColumn(length, largestPossibleId);
const values = createIdColumn(length, largestPossibleId);
const numerators = integral ? createIntegerColumn(length, numeratorBits) : new Float64Array(length);
const denominators = integral ? createIntegerColumn(length, denominatorBits) : new Float64Array(length);
let bmson: Map<number, BmsonEventExtensions> | undefined;
let lrx: Map<number, LRXEventExtensions> | undefined;
for (let index = 0; index < length; index += 1) {
const event = events[index]!;
measures[index] = event.measure;
channels[index] = strings.idOf(event.channel);
values[index] = strings.idOf(event.value);
numerators[index] = event.position[0];
denominators[index] = event.position[1];
if ('bmson' in event) {
bmson ??= new Map();
bmson.set(index, event.bmson!);
}
if (event.lrx !== undefined) {
lrx ??= new Map();
lrx.set(index, event.lrx);
}
}
return {
length,
measures,
channels: narrowIds(channels),
values: narrowIds(values),
numerators,
denominators,
bmson,
...(lrx ? { lrx } : {}),
};
}
// Invert negative values so both bounds of a signed width are covered by one bit mask.
function bitsForSignedWidth(value: number): number {
return value ^ (value >> 31);
}
function createIntegerColumn(length: number, bits: number): PackedIntegers {
if (bits <= 0x7f) return new Int8Array(length);
if (bits <= 0x7fff) return new Int16Array(length);
return new Int32Array(length);
}
function createIdColumn(length: number, maximum: number): PackedIds {
if (maximum <= 0xff) return new Uint8Array(length);
if (maximum <= 0xffff) return new Uint16Array(length);
return new Uint32Array(length);
}
/** `column` in the narrowest unsigned type that holds its largest id. */
function narrowIds(column: PackedIds): PackedIds {
if (column instanceof Uint8Array) return column;
let maximum = 0;
for (let index = 0; index < column.length; index += 1) {
if (column[index]! > maximum) maximum = column[index]!;
}
if (maximum <= 0xff) return new Uint8Array(column);
Iif (maximum <= 0xffff && column instanceof Uint32Array) return new Uint16Array(column);
return column;
}
/** Rebuilds the event objects {@link packEvents} packed, with the same fields in the same order. */
export function unpackEvents(packed: PackedEvents, strings: PackedEventStrings): BeMusicEvent[] {
const events: BeMusicEvent[] = Array.from({ length: packed.length });
const { measures, channels, values, numerators, denominators, bmson, lrx } = packed;
for (let index = 0; index < packed.length; index += 1) {
const event: BeMusicEvent = {
measure: measures[index]!,
channel: strings.valueOf(channels[index]!),
position: [numerators[index]!, denominators[index]!],
value: strings.valueOf(values[index]!),
};
const extensions = bmson?.get(index);
if (extensions !== undefined) {
event.bmson = extensions;
}
const lrxExtensions = lrx?.get(index);
if (lrxExtensions !== undefined) event.lrx = lrxExtensions;
events[index] = event;
}
return events;
}
function isInt32(value: number): boolean {
return Number.isInteger(value) && value >= -0x80000000 && value <= 0x7fffffff;
}
|