Remove unneeded Vec copy operations (#3909)

* Remove unneeded Vec copy operations

* a-e

* Tuple adjust
This commit is contained in:
Jaco
2021-08-28 08:12:17 +02:00
committed by GitHub
parent 7f949874e6
commit 5e83837c5b
6 changed files with 39 additions and 6 deletions
+6 -2
View File
@@ -20,8 +20,12 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
public createdAtHash?: Hash;
protected constructor (registry: Registry, ...values: T[]) {
super(...values);
protected constructor (registry: Registry, values: T[]) {
super(values.length);
for (let i = 0; i < values.length; i++) {
this[i] = values[i];
}
this.registry = registry;
}
File diff suppressed because one or more lines are too long
+26 -1
View File
@@ -7,7 +7,8 @@ import type { CodecTo } from '../types';
import { TypeRegistry } from '../create';
import { Text, U32 } from '../primitive';
import { Struct, Vec } from '.';
import { TEST_A } from './Struct.data';
import { Enum, Struct, Vec } from '.';
describe('Struct', (): void => {
const registry = new TypeRegistry();
@@ -84,6 +85,30 @@ describe('Struct', (): void => {
expect(s.toString()).toBe('{"foo":[{"bar":"1"},{"bar":"2"}]}');
});
it('decodes a previously problematic input', (): void => {
let data;
try {
data = new Struct(registry, {
a: 'u32',
b: 'H256',
c: 'H256',
swap: Enum.with({
A: 'u256',
B: 'u256'
}),
d: Vec.with('u8'),
e: 'u8'
}, TEST_A);
} catch (error) {
console.error(error);
throw error;
}
expect(data.get('d')).toHaveLength(50000);
});
it('decodes from a Map input', (): void => {
const input = new Struct(registry, {
a: U32,
+1 -1
View File
@@ -61,7 +61,7 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
? [typeToConstructor(registry, Types)]
: mapToTypeMap(registry, Types);
super(registry, ...decodeTuple(registry, Clazzes, value));
super(registry, decodeTuple(registry, Clazzes, value));
this._Types = Clazzes;
}
+1 -1
View File
@@ -25,7 +25,7 @@ export class Vec<T extends Codec> extends AbstractArray<T> {
constructor (registry: Registry, Type: Constructor<T> | string, value: Vec<Codec> | Uint8Array | string | unknown[] = []) {
const Clazz = typeToConstructor<T>(registry, Type);
super(registry, ...Vec.decodeVec(registry, Clazz, value));
super(registry, Vec.decodeVec(registry, Clazz, value));
this._Type = Clazz;
}
+1 -1
View File
@@ -39,7 +39,7 @@ export class VecFixed<T extends Codec> extends AbstractArray<T> {
constructor (registry: Registry, Type: Constructor<T> | string, length: number, value: VecFixed<any> | Uint8Array | string | any[] = [] as any[]) {
const Clazz = typeToConstructor<T>(registry, Type);
super(registry, ...decodeVecFixed(registry, Clazz, length, value));
super(registry, decodeVecFixed(registry, Clazz, length, value));
this._Type = Clazz;
}