Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

perf: optimize options normalization #4317

Merged
merged 4 commits into from
Jun 24, 2023
Merged
Changes from 2 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
37 changes: 16 additions & 21 deletions src/packages/options/src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import { hasOwn } from "@ganache/utils";

export type NamespacedOptions = { [key: string]: Base.Config };

export type ProviderOptions<O extends NamespacedOptions> = Partial<
{
[K in keyof O]: ExternalConfig<O[K]>;
}
>;
export type ProviderOptions<O extends NamespacedOptions> = Partial<{
[K in keyof O]: ExternalConfig<O[K]>;
}>;

export type InternalOptions<O extends NamespacedOptions> = {
[K in keyof O]: InternalConfig<O[K]>;
Expand Down Expand Up @@ -38,22 +36,22 @@ const checkForConflicts = (
}
};

function fill(defaults: any, options: any, target: any, namespace: any) {
const def = defaults[namespace];
function fill(options: any, target: any, def: any, namespace: any) {
const config = (target[namespace] = target[namespace] || {});
const flavor = options.flavor;

const suppliedOptions = new Set<string>();
const keys = Object.keys(def);
const entries = Object.entries(def) as [string, any][];
if (hasOwn(options, namespace)) {
const namespaceOptions = options[namespace];

for (let i = 0, l = keys.length; i < l; i++) {
const key = keys[i];
const propDefinition = def[key];
for (let i = 0, l = entries.length; i < l; i++) {
const entry = entries[i];
const key = entry[0];
const propDefinition = entry[1];
davidmurdoch marked this conversation as resolved.
Show resolved Hide resolved
let value = namespaceOptions[key];
if (value !== undefined) {
const normalized = propDefinition.normalize(namespaceOptions[key]);
const normalized = propDefinition.normalize(value);
if (normalized !== undefined) {
checkForConflicts(
key,
Expand Down Expand Up @@ -85,9 +83,10 @@ function fill(defaults: any, options: any, target: any, namespace: any) {
}
}
} else {
for (let i = 0, l = keys.length; i < l; i++) {
const key = keys[i];
const propDefinition = def[key];
for (let i = 0, l = entries.length; i < l; i++) {
const entry = entries[i];
const key = entry[0];
const propDefinition = entry[1];
davidmurdoch marked this conversation as resolved.
Show resolved Hide resolved

const legacyName = propDefinition.legacyName || key;
const value = options[legacyName];
Expand All @@ -112,19 +111,15 @@ function fill(defaults: any, options: any, target: any, namespace: any) {

export class OptionsConfig<O extends NamespacedOptions> {
#defaults: Defaults<O>;
#namespaces: UnionToTuple<keyof Defaults<O>>;

constructor(defaults: Defaults<O>) {
this.#defaults = defaults;
this.#namespaces = Object.keys(defaults) as UnionToTuple<keyof Defaults<O>>;
}

normalize(options: ProviderOptions<O>) {
const defaults = this.#defaults;

const out = {} as InternalOptions<O>;
this.#namespaces.forEach(namespace => {
fill(defaults, options, out, namespace as keyof Defaults<O>);
Object.entries(this.#defaults).forEach(([namespace, definition]) => {
fill(options, out, definition, namespace as keyof Defaults<O>);
});
return out;
}
Expand Down