Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[perf | size] simplify array iterations #1512

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/@glimmer/runtime/lib/compiled/expressions/concat.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import type { Dict, Maybe } from '@glimmer/interfaces';
import type { Reference } from '@glimmer/reference';
import { createComputeRef, valueForRef } from '@glimmer/reference';
import { enumerate } from '@glimmer/util';

export function createConcatRef(partsRefs: Reference[]) {
return createComputeRef(() => {
let parts = new Array<string>();
const parts: string[] = [];

for (const [i, ref] of enumerate(partsRefs)) {
let value = valueForRef(ref) as Maybe<Dict>;
for (const ref of partsRefs) {
const value = valueForRef(ref);

if (value !== null && value !== undefined) {
parts[i] = castToString(value);
parts.push(castToString(value));
}
}

Expand All @@ -23,8 +21,10 @@ export function createConcatRef(partsRefs: Reference[]) {
});
}

function castToString(value: Dict) {
if (typeof value.toString !== 'function') {
function castToString(value: string | object) {
if (typeof value === 'string') {
lifeart marked this conversation as resolved.
Show resolved Hide resolved
return value;
} else if (typeof value.toString !== 'function') {
return '';
}

Expand Down
7 changes: 1 addition & 6 deletions packages/@glimmer/runtime/lib/compiled/opcodes/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,12 +534,7 @@ function mergeClasses(classes: (string | Reference)[]): string | Reference<unkno
}

function allStringClasses(classes: (string | Reference<unknown>)[]): classes is string[] {
for (let i = 0; i < classes.length; i++) {
if (typeof classes[i] !== 'string') {
return false;
}
}
return true;
return classes.every((c) => typeof c === 'string');
}

function setDeferredAttr(
Expand Down
7 changes: 3 additions & 4 deletions packages/@glimmer/runtime/lib/compiled/opcodes/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,12 @@ APPEND_OPCODES.add(Op.FlushElement, (vm) => {
APPEND_OPCODES.add(Op.CloseElement, (vm) => {
let modifiers = vm.elements().closeElement();

if (modifiers) {
if (modifiers !== null) {
modifiers.forEach((modifier) => {
vm.env.scheduleInstallModifier(modifier);
let { manager, state } = modifier;
let d = manager.getDestroyable(state);
const d = modifier.manager.getDestroyable(modifier.state);

if (d) {
if (d !== null) {
vm.associateDestroyable(d);
}
});
Expand Down
12 changes: 2 additions & 10 deletions packages/@glimmer/runtime/lib/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,13 @@ export function isScopeReference(s: ScopeSlot): s is Reference {

export class PartialScopeImpl implements PartialScope {
static root(self: Reference<unknown>, size = 0, owner: Owner): PartialScope {
let refs: Reference<unknown>[] = new Array(size + 1);

for (let i = 0; i <= size; i++) {
refs[i] = UNDEFINED_REFERENCE;
}
let refs: Reference<unknown>[] = new Array(size + 1).fill(UNDEFINED_REFERENCE);

return new PartialScopeImpl(refs, owner, null, null, null).init({ self });
}

static sized(size = 0, owner: Owner): Scope {
let refs: Reference<unknown>[] = new Array(size + 1);

for (let i = 0; i <= size; i++) {
refs[i] = UNDEFINED_REFERENCE;
}
let refs: Reference<unknown>[] = new Array(size + 1).fill(UNDEFINED_REFERENCE);

return new PartialScopeImpl(refs, owner, null, null, null);
}
Expand Down