Optimization for createClass with shortcut typeDef creation (#4849)

This commit is contained in:
Jaco
2022-05-25 15:07:42 +03:00
committed by GitHub
parent 8649c17dd7
commit a7d826f839
5 changed files with 28 additions and 16 deletions
+10 -5
View File
@@ -208,10 +208,15 @@ export function getTypeClass<T extends Codec = Codec> (registry: Registry, typeD
}
export function createClassUnsafe<T extends Codec = Codec, K extends string = string> (registry: Registry, type: K): CodecClass<T> {
return getTypeClass(
registry,
registry.isLookupType(type)
? registry.lookup.getTypeDef(type)
: getTypeDef(type)
return (
// just retrieve via name, no creation via typeDef
registry.getUnsafe(type) ||
// we don't have an existing type, create the class via typeDef
getTypeClass(
registry,
registry.isLookupType(type)
? registry.lookup.getTypeDef(type)
: getTypeDef(type)
)
);
}
+6 -8
View File
@@ -32,13 +32,11 @@ function checkInstance (created: Codec, matcher: Uint8Array): void {
assert(isOk, () => `${rawType}:: Decoded input doesn't match input, received ${u8aToHex(matcher, 512)} (${matcher.length} bytes), created ${u8aToHex(u8a, 512)} (${u8a.length} bytes)`);
}
function checkPedantic (created: Codec, [value]: unknown[], isPedantic = false): void {
if (isPedantic) {
if (isU8a(value)) {
checkInstance(created, value);
} else if (isHex(value)) {
checkInstance(created, u8aToU8a(value));
}
function checkPedantic (created: Codec, [value]: unknown[]): void {
if (isU8a(value)) {
checkInstance(created, value);
} else if (isHex(value)) {
checkInstance(created, u8aToU8a(value));
}
}
@@ -47,7 +45,7 @@ function checkPedantic (created: Codec, [value]: unknown[], isPedantic = false):
function initType<T extends Codec> (registry: Registry, Type: CodecClass, params: unknown[] = [], { blockHash, isOptional, isPedantic }: CreateOptions = {}): T {
const created = new (isOptional ? Option.with(Type) : Type)(registry, ...params);
checkPedantic(created, params, isPedantic);
isPedantic && checkPedantic(created, params);
if (blockHash) {
created.createdAtHash = createTypeUnsafe<IU8a>(registry, 'Hash', [blockHash]);
+1 -1
View File
@@ -212,7 +212,7 @@ export class TypeRegistry implements Registry {
}
/**
* @description Returns tru if the type is in a Compat format
* @description Returns true if the type is in a Compat format
*/
public isLookupType (value: string): boolean {
return /Lookup\d+$/.test(value);
@@ -321,8 +321,9 @@ function extractAliases (params: Record<string, SiTypeParameter[]>, isContract?:
function extractTypeInfo (lookup: PortableRegistry, portable: PortableType[]): [Record<number, PortableType>, Record<string, string>, Record<number, string>, Record<string, SiTypeParameter[]>] {
const nameInfo: [number, string, SiTypeParameter[]][] = [];
const types: Record<number, PortableType> = {};
const porCount = portable.length;
for (let i = 0; i < portable.length; i++) {
for (let i = 0; i < porCount; i++) {
const type = portable[i];
const extracted = extractName(portable, portable[i]);
@@ -337,8 +338,9 @@ function extractTypeInfo (lookup: PortableRegistry, portable: PortableType[]): [
const lookups: Record<string, string> = {};
const names: Record<number, string> = {};
const params: Record<string, SiTypeParameter[]> = {};
const dupCount = dedup.length;
for (let i = 0; i < dedup.length; i++) {
for (let i = 0; i < dupCount; i++) {
const [lookupIndex, name, p] = dedup[i];
names[lookupIndex] = name;