Optimization of registry.getOrUnknown with lookup cache (#4853)

This commit is contained in:
Jaco
2022-05-26 10:15:22 +03:00
committed by GitHub
parent f701c398c3
commit 5730005eba
4 changed files with 26 additions and 12 deletions
+1
View File
@@ -6,6 +6,7 @@ Changes:
- Add codec support for specialized `Option<bool>`
- Optimization for `createClass` with shortcut typeDef creation
- Optimization of `registry.getOrUnknown` with lookup cache
## 8.5.1 May 22, 2022
+3 -3
View File
@@ -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);
+13 -6
View File
@@ -45,9 +45,17 @@ const mappings: Mapper[] = [
// flattens tuples with one value, `(AccountId)` -> `AccountId`
flattenSingleTuple(),
// converts ::Type to Type, <T as Trait<I>>::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<string, string>();
// 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;
+9 -3
View File
@@ -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 <T extends Codec = Codec, K extends string = string> (type: K): CodecClass<DetectCodec<T, K>> {
return this.createClassUnsafe(type) as unknown as CodecClass<DetectCodec<T, K>>;
return createClassUnsafe<DetectCodec<T, K>>(this, type);
}
/**
@@ -267,7 +267,7 @@ export class TypeRegistry implements Registry {
* @description Creates an instance of a type as registered
*/
public createType <T extends Codec = Codec, K extends string = string> (type: K, ...params: unknown[]): DetectCodec<T, K> {
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);
}
}
}