Skip to content

Commit

Permalink
[perf] avoid double memory usage in append opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeart committed Dec 5, 2023
1 parent 7b9b0cf commit 9ae91d8
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 25 deletions.
14 changes: 2 additions & 12 deletions packages/@glimmer/debug/lib/opcode-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ import { MachineOp, Op } from '@glimmer/vm';

import type { NormalizedMetadata } from './metadata';

function fillNulls<T>(count: number): T[] {
let arr = new Array(count);

for (let i = 0; i < count; i++) {
arr[i] = null;
}

return arr;
}

export function opcodeMetadata(
op: VmMachineOp | VmOp,
isMachine: 0 | 1
Expand All @@ -24,8 +14,8 @@ export function opcodeMetadata(
return value || null;
}

const METADATA: Nullable<NormalizedMetadata>[] = fillNulls(Op.Size);
const MACHINE_METADATA: Nullable<NormalizedMetadata>[] = fillNulls(MachineOp.Size);
const METADATA: Nullable<NormalizedMetadata>[] = new Array(Op.Size).fill(null);
const MACHINE_METADATA: Nullable<NormalizedMetadata>[] = new Array(Op.Size).fill(null);
MACHINE_METADATA[MachineOp.PushFrame] = {
name: 'PushFrame',
mnemonic: 'pushf',
Expand Down
5 changes: 3 additions & 2 deletions packages/@glimmer/runtime/lib/opcodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
import { debug, logOpcode, opcodeMetadata, recordStackSize } from '@glimmer/debug';
import { LOCAL_DEBUG, LOCAL_SHOULD_LOG } from '@glimmer/local-debug-flags';
import { valueForRef } from '@glimmer/reference';
import { assert, fillNulls, LOCAL_LOGGER, unwrap } from '@glimmer/util';
import { assert, LOCAL_LOGGER, unwrap } from '@glimmer/util';
import { $fp, $pc, $ra, $sp, Op } from '@glimmer/vm';

import type { LowLevelVM, VM } from './vm';
Expand Down Expand Up @@ -51,8 +51,9 @@ export type DebugState = {
state: unknown;
};

const OPCODES_PLACEHOLDER = new Array(Op.Size).fill(null);
export class AppendOpcodes {
private evaluateOpcode: Evaluate[] = fillNulls<Evaluate>(Op.Size).slice();
private evaluateOpcode: Evaluate[] = OPCODES_PLACEHOLDER.slice();

add<Name extends VmOp>(name: Name, evaluate: Syscall): void;
add<Name extends VmMachineOp>(name: Name, evaluate: MachineOpcode, kind: 'machine'): void;
Expand Down
2 changes: 1 addition & 1 deletion packages/@glimmer/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export {
isSerializationFirstNode,
SERIALIZATION_FIRST_NODE_STRING,
} from './lib/is-serialization-first-node';
export { assign, entries, fillNulls, values } from './lib/object-utils';
export { assign, entries, values } from './lib/object-utils';
export * from './lib/platform-utils';
export * from './lib/present';
export {
Expand Down
10 changes: 0 additions & 10 deletions packages/@glimmer/util/lib/object-utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
export let assign = Object.assign;

export function fillNulls<T>(count: number): T[] {
let arr = new Array(count);

for (let i = 0; i < count; i++) {
arr[i] = null;
}

return arr;
}

export function values<T>(obj: { [s: string]: T }): T[] {
return Object.values(obj);
}
Expand Down

0 comments on commit 9ae91d8

Please sign in to comment.