From 5730005eba27f44e8cb79107d7c2e29abcbaf0a9 Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 26 May 2022 09:15:22 +0200 Subject: [PATCH] Optimization of `registry.getOrUnknown` with lookup cache (#4853) --- CHANGELOG.md | 1 + packages/types-codec/src/native/Text.ts | 6 +++--- packages/types-codec/src/utils/sanitize.ts | 19 +++++++++++++------ packages/types/src/create/registry.ts | 12 +++++++++--- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cafd3aa98a..8556a2f246 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Changes: - Add codec support for specialized `Option` - Optimization for `createClass` with shortcut typeDef creation +- Optimization of `registry.getOrUnknown` with lookup cache ## 8.5.1 May 22, 2022 diff --git a/packages/types-codec/src/native/Text.ts b/packages/types-codec/src/native/Text.ts index 8993101ae5..38ec95dd80 100644 --- a/packages/types-codec/src/native/Text.ts +++ b/packages/types-codec/src/native/Text.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; -import type { AnyU8a, Inspect, IText, IU8a, Registry } from '../types'; +import type { AnyString, AnyU8a, Inspect, IText, IU8a, Registry } from '../types'; import { assert, compactAddLength, compactFromU8a, compactToU8a, hexToU8a, isHex, isString, isU8a, stringToU8a, u8aToHex, u8aToString } from '@polkadot/util'; @@ -11,7 +11,7 @@ import { Raw } from './Raw'; const MAX_LENGTH = 128 * 1024; /** @internal */ -function decodeText (value?: null | Text | string | AnyU8a | { toString: () => string }): [string, number] { +function decodeText (value?: null | AnyString | AnyU8a | { toString: () => string }): [string, number] { if (isU8a(value)) { if (!value.length) { return ['', 0]; @@ -54,7 +54,7 @@ export class Text extends String implements IText { #override: string | null = null; - constructor (registry: Registry, value?: null | Text | string | AnyU8a | { toString: () => string }) { + constructor (registry: Registry, value?: null | AnyString | AnyU8a | { toString: () => string }) { const [str, decodedLength] = decodeText(value); super(str); diff --git a/packages/types-codec/src/utils/sanitize.ts b/packages/types-codec/src/utils/sanitize.ts index 6665258e96..bec6725380 100644 --- a/packages/types-codec/src/utils/sanitize.ts +++ b/packages/types-codec/src/utils/sanitize.ts @@ -45,9 +45,17 @@ const mappings: Mapper[] = [ // flattens tuples with one value, `(AccountId)` -> `AccountId` flattenSingleTuple(), // converts ::Type to Type, >::Proposal -> Proposal - removeColons() + removeColons(), + // remove all trailing spaces - this should always be the last + trim() ]; +// given a string, trim it +export function trim (): Mapper { + return (value: string): string => + value.trim(); +} + // given a starting index, find the closing > export function findClosing (value: string, start: number): number { let depth = 0; @@ -257,10 +265,11 @@ const sanitizeMap = new Map(); // eslint-disable-next-line @typescript-eslint/ban-types export function sanitize (value: String | string, options?: SanitizeOptions): string { - let result = value.toString(); + const startValue = value.toString(); + let result = startValue; if (!options) { - const memoized = sanitizeMap.get(result); + const memoized = sanitizeMap.get(startValue); if (memoized) { return memoized; @@ -271,10 +280,8 @@ export function sanitize (value: String | string, options?: SanitizeOptions): st result = mappings[i](result, options); } - result = result.trim(); - if (!options) { - sanitizeMap.set(value.toString(), result); + sanitizeMap.set(startValue, result); } return result; diff --git a/packages/types/src/create/registry.ts b/packages/types/src/create/registry.ts index 9624da4a8c..7dede52cc5 100644 --- a/packages/types/src/create/registry.ts +++ b/packages/types/src/create/registry.ts @@ -9,7 +9,7 @@ import type { CallFunction, CodecHasher, Definitions, DetectCodec, RegisteredTyp import { DoNotConstruct, Json, Raw } from '@polkadot/types-codec'; import { constructTypeClass, createClassUnsafe, createTypeUnsafe } from '@polkadot/types-create'; -import { assert, assertReturn, BN_ZERO, formatBalance, isFunction, isString, isU8a, lazyMethod, logger, objectSpread, stringCamelCase, stringify } from '@polkadot/util'; +import { assert, assertReturn, BN_ZERO, formatBalance, isFunction, isNumber, isString, isU8a, lazyMethod, logger, objectSpread, stringCamelCase, stringify } from '@polkadot/util'; import { blake2AsU8a } from '@polkadot/util-crypto'; import { expandExtensionTypes, fallbackExtensions, findUnknownExtensions } from '../extrinsic/signedExtensions'; @@ -253,7 +253,7 @@ export class TypeRegistry implements Registry { * @describe Creates an instance of the class */ public createClass (type: K): CodecClass> { - return this.createClassUnsafe(type) as unknown as CodecClass>; + return createClassUnsafe>(this, type); } /** @@ -267,7 +267,7 @@ export class TypeRegistry implements Registry { * @description Creates an instance of a type as registered */ public createType (type: K, ...params: unknown[]): DetectCodec { - return this.createTypeUnsafe(type, params); + return createTypeUnsafe(this, type, params); } /** @@ -345,6 +345,12 @@ export class TypeRegistry implements Registry { Type = class extends BaseType {}; this.#classes.set(name, Type); + + // In the case of lookups, we also want to store the actual class against + // the lookup name, instad of having to traverse again + if (knownTypeDef && isNumber(knownTypeDef.lookupIndex)) { + this.#classes.set(this.createLookupType(knownTypeDef.lookupIndex), Type); + } } }