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]);