Skip to content

Commit

Permalink
perf: optimize options normalization (trufflesuite#4317)
Browse files Browse the repository at this point in the history
Co-authored-by: jeffsmale90 <6363749+jeffsmale90@users.noreply.github.com>
  • Loading branch information
davidmurdoch and jeffsmale90 committed Jun 24, 2023
1 parent 79a793a commit 20a5f09
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 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,19 @@ 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 (const [key, propDefinition] of entries) {
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 +80,7 @@ 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 (const [key, propDefinition] of entries) {

const legacyName = propDefinition.legacyName || key;
const value = options[legacyName];
Expand All @@ -112,19 +105,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

0 comments on commit 20a5f09

Please sign in to comment.