From b71577cc52d7f933ee7e5dc50e5b61457f04e3f4 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Tue, 13 Nov 2018 17:06:41 -0800 Subject: More robust implementation for optimizer --- packages/order-utils/test/abi/calldata.ts | 41 +++++++++++++++++-------------- 1 file changed, 23 insertions(+), 18 deletions(-) (limited to 'packages/order-utils') diff --git a/packages/order-utils/test/abi/calldata.ts b/packages/order-utils/test/abi/calldata.ts index 04bea9628..754d8f823 100644 --- a/packages/order-utils/test/abi/calldata.ts +++ b/packages/order-utils/test/abi/calldata.ts @@ -216,6 +216,11 @@ class Queue { pop(): T | undefined { return this.store.shift(); } + popBack(): T | undefined { + if (this.store.length === 0) return undefined; + const backElement = this.store.splice(-1, 1)[0]; + return backElement; + } merge(q: Queue) { this.store = this.store.concat(q.store); } @@ -378,34 +383,34 @@ export class Calldata { throw new Error('expected root'); } - const subtreesByHash: { [key: string]: DependentCalldataBlock[] } = {}; + const blocksByHash: { [key: string]: CalldataBlock } = {}; // 1. Create a queue of subtrees by hash // Note that they are ordered the same as const subtreeQueue = this.createQueue(this.root); let block: CalldataBlock | undefined; - while ((block = subtreeQueue.pop()) !== undefined) { + console.log('*'.repeat(100), ' OPTIMIZING ', '*'.repeat(100)); + while ((block = subtreeQueue.popBack()) !== undefined) { console.log(block.getName()); + if (block instanceof DependentCalldataBlock) { + const blockHashBuf = block.getDependency().computeHash(); + const blockHash = ethUtil.bufferToHex(blockHashBuf); + if (blockHash in blocksByHash) { + const blockWithSameHash = blocksByHash[blockHash]; + if (blockWithSameHash !== block.getDependency()) { + block.setAlias(blockWithSameHash); + } + } + continue; + } - if (block instanceof DependentCalldataBlock === false) continue; const blockHashBuf = block.computeHash(); - const blockHashHex = ethUtil.bufferToHex(blockHashBuf); - if (blockHashHex in subtreesByHash === false) { - subtreesByHash[blockHashHex] = [block as DependentCalldataBlock]; - } else { - subtreesByHash[blockHashHex].push(block as DependentCalldataBlock); + const blockHash = ethUtil.bufferToHex(blockHashBuf); + if (blockHash in blocksByHash === false) { + blocksByHash[blockHash] = block; } } - - // Iterate through trees that have the same hash and - _.each(subtreesByHash, (subtrees: DependentCalldataBlock[], hash: string) => { - if (subtrees.length === 1) return; // No optimization - // Optimize - const lastSubtree = subtrees[subtrees.length - 1]; - for (let i = 0; i < subtrees.length - 1; ++i) { - subtrees[i].setAlias(lastSubtree.getDependency()); - } - }); + console.log('*'.repeat(100), ' FINISHED OPTIMIZING ', '*'.repeat(100)); } public toHexString(optimize: boolean = false, annotate: boolean = false): string { -- cgit v1.2.3