/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @format * @oncall memory_lab */ /** * Unit tests for `describeArrayLength` (heap-shapes.ts) on hand-built * pointer-compressed nodes: a number array's length comes from its store at * the slot width of its elements kind, a dictionary gives only a lower bound, * and visible elements give the highest index + 1. * * npm run build-pkg && npm run test-units * * Runs against the BUILT dist/. */ import './test-isolation.mjs'; import assert from 'assert/strict'; import fs from 'fs'; import path from 'path'; import {test} from 'node:test'; import {fileURLToPath, pathToFileURL} from 'url'; const here = path.dirname(fileURLToPath(import.meta.url)); const built = path.join(here, '..', 'dist', 'heap-shapes.js'); if (!fs.existsSync(built)) { console.error( 'check-array-length: dist/heap-shapes.js is missing; build first.', ); process.exit(1); } const {describeArrayLength} = await import(pathToFileURL(built).href); let nextId = 1; const node = (type, name, self_size, references = []) => ({ id: nextId++, type, name, self_size, references, }); const edge = (type, name_or_index, toNode) => ({type, name_or_index, toNode}); /** A hidden class recording `field: value`, as recent captures do. */ const hiddenClass = fields => node( 'object shape', 'system / Map', 40, Object.entries(fields).map(([k, v]) => edge('internal', k, node('string', v, 16)), ), ); /** A compressed heap: `Array` objects are 16 bytes, so slots are 4. */ const snapshot = { nodes: { forEach(cb) { for (let i = 0; i < 40; i++) { if (cb(node('object', 'Array', 16)) === false) return; } }, }, }; const array = (store, kind, extra = []) => node('object', 'Array', 16, [ edge('internal', 'elements', store), ...(kind != null ? [edge('internal', 'map', hiddenClass({elements_kind_name: kind}))] : []), ...extra, ]); test('a small-integer array reads its capacity at 4-byte slots', () => { const info = describeArrayLength( snapshot, array( node('array', '(object elements)', 8 + 1000 * 4), 'PACKED_SMI_ELEMENTS', ), ); assert.equal(info.method, 'store-capacity'); assert.equal(info.length, 1000); assert.equal(info.slotBytes, 4); assert.equal(info.exact, false); assert.equal(info.elementsKind, 'PACKED_SMI_ELEMENTS'); }); test('a double array reads its capacity at 8-byte slots', () => { const store = node('array', '(object elements)', 8 + 1000 * 8); const info = describeArrayLength( snapshot, array(store, 'PACKED_DOUBLE_ELEMENTS'), ); assert.equal(info.length, 1000); assert.equal(info.slotBytes, 8); }); test('without a recorded kind, a store with a map edge reads as doubles', () => { const doubleMap = hiddenClass({}); const store = node('array', '(object elements)', 8 + 500 * 8, [ edge('internal', 'map', doubleMap), ]); const info = describeArrayLength(snapshot, array(store, null)); assert.equal(info.length, 500); assert.match(info.note, /does not record the elements kind/); }); test("without a recorded kind, the store map's read-only root decides", () => { const roots = node('synthetic', '(Read-only roots)', 0); const rootedMap = rootName => { const m = hiddenClass({}); m.referrers = [{name_or_index: rootName, fromNode: roots, toNode: m}]; return m; }; const dict = node('array', '(object elements)', 144, [ edge('internal', 'map', rootedMap('number_dictionary_map')), ]); const d = describeArrayLength(snapshot, array(dict, null)); assert.equal(d.method, 'dictionary'); assert.equal(d.upper, null); const dbl = node('array', '(object elements)', 8 + 16 * 8, [ edge('internal', 'map', rootedMap('fixed_double_array_map')), ]); const f = describeArrayLength(snapshot, array(dbl, null)); assert.equal(f.method, 'store-capacity'); assert.equal(f.length, 16); }); test('dictionary elements give a lower bound only', () => { const value = node('object', 'Object', 12); const store = node('array', '(object elements)', 64, [ edge('internal', '100000', value), ]); const info = describeArrayLength( snapshot, array(store, 'DICTIONARY_ELEMENTS'), ); assert.equal(info.method, 'dictionary'); assert.equal(info.lower, 100001); assert.equal(info.upper, null); assert.equal(info.exact, false); }); test('visible elements give the highest index + 1', () => { const obj = () => node('object', 'Object', 12); const store = node( 'array', '(object elements)', 8 + 10 * 4, Array.from({length: 7}, (_, i) => edge('internal', String(i), obj())), ); const info = describeArrayLength(snapshot, array(store, 'PACKED_ELEMENTS')); assert.equal(info.method, 'element-edges'); assert.equal(info.length, 7); assert.equal(info.upper, 10); assert.equal(info.exact, false); }); test('an empty store is an exact 0', () => { const info = describeArrayLength( snapshot, array(node('array', '(object elements)', 8), 'PACKED_SMI_ELEMENTS'), ); assert.equal(info.method, 'empty'); assert.equal(info.length, 0); assert.equal(info.exact, true); });