From f14aedc2200c5daad2cc46c1631692e3f339394c Mon Sep 17 00:00:00 2001 From: Craig Gidney Date: Sun, 16 Oct 2016 13:24:34 -0400 Subject: [PATCH] Optimizing hotspots - Turned on slow webgl checks in tests --- src/base/Seq.js | 8 ++++++- src/circuit/CircuitDefinition.js | 39 +++++++++++++++++++++++--------- src/circuit/GateColumn.js | 3 +-- src/draw/Painter.js | 6 ++--- test/TestUtil.js | 2 ++ 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/base/Seq.js b/src/base/Seq.js index 683bddf2..85b6bf4c 100644 --- a/src/base/Seq.js +++ b/src/base/Seq.js @@ -510,7 +510,13 @@ class Seq { * @template T */ sum() { - return this.fold((a, e) => a + e, 0); + let total = 0; + let first = true; + for (let e of this._iterable) { + total = first ? e : total + e; + first = false; + } + return total; } /** diff --git a/src/circuit/CircuitDefinition.js b/src/circuit/CircuitDefinition.js index 2cb07b0a..f9f7b019 100644 --- a/src/circuit/CircuitDefinition.js +++ b/src/circuit/CircuitDefinition.js @@ -658,10 +658,19 @@ class CircuitDefinition { * @returns {boolean} */ colHasEnabledSwapGate(col) { - let pts = Seq.range(this.numWires). - map(row => new Point(col, row)). - filter(pt => this.gateInSlot(pt.x, pt.y) === Gates.Special.SwapHalf); - return !pts.any(pt => this.gateAtLocIsDisabledReason(pt.x, pt.y) !== undefined) && pts.count() === 2; + if (col < 0 || col >= this.columns.length) { + return false; + } + let count = 0; + for (let row = 0; row < this.numWires; row++) { + if (this.gateInSlot(col, row) === Gates.Special.SwapHalf) { + if (this.gateAtLocIsDisabledReason(col, row) !== undefined) { + return false; + } + count++; + } + } + return count === 2; } /** @@ -672,7 +681,12 @@ class CircuitDefinition { if (col < 0 || col >= this.columns.length) { return false; } - return seq(this.columns[col].gates).any(e => e !== undefined && e.affectsOtherWires()); + for (let gate of this.columns[col].gates) { + if (gate !== undefined && gate.affectsOtherWires()) { + return true; + } + } + return false; } /** @@ -835,12 +849,15 @@ class CircuitDefinition { } let col = this.columns[colIndex]; - return Seq.range(col.gates.length). - filter(row => - col.gates[row] !== undefined && - col.gates[row].customStatPostProcesser !== undefined && - this.gateAtLocIsDisabledReason(colIndex, row) === undefined). - toArray(); + let result = []; + for (let row = 0; row < col.gates.length; row++) { + if (col.gates[row] !== undefined && + col.gates[row].customStatPostProcesser !== undefined && + this.gateAtLocIsDisabledReason(colIndex, row) === undefined) { + result.push(row); + } + } + return result; } /** diff --git a/src/circuit/GateColumn.js b/src/circuit/GateColumn.js index f32b550e..818ccc3c 100644 --- a/src/circuit/GateColumn.js +++ b/src/circuit/GateColumn.js @@ -196,8 +196,7 @@ class GateColumn { let row = args.outerRow; let rangeVals = seq(args.gate.getUnmetContextKeys()). - filter(key => key.startsWith("Input Range ")). - filter(key => args.context.has(key)). + filter(key => key.startsWith("Input Range ") && args.context.has(key)). map(key => args.context.get(key)). toArray(); diff --git a/src/draw/Painter.js b/src/draw/Painter.js index 00be5b2d..9a114037 100644 --- a/src/draw/Painter.js +++ b/src/draw/Painter.js @@ -3,7 +3,7 @@ import {Format} from "src/base/Format.js" import {Point} from "src/math/Point.js" import {Rect} from "src/math/Rect.js" import {RestartableRng} from "src/base/RestartableRng.js" -import {Seq} from "src/base/Seq.js" +import {seq, Seq} from "src/base/Seq.js" import {Util} from "src/base/Util.js" class Painter { @@ -242,7 +242,7 @@ class Painter { let lines; let measures; let height; - let forcedLines = new Seq(text.split("\n")); + let forcedLines = seq(text.split("\n")); for (let df = 0; ; df++) { // Note: potential for quadratic behavior. fontSize = maxFontSize - df; this.ctx.font = fontSize + "px " + fontFamily; @@ -250,7 +250,7 @@ class Painter { flatMap(line => Util.breakLine(line, area.w, s => this.ctx.measureText(s).width)). toArray(); measures = lines.map(e => this.ctx.measureText(e)); - height = new Seq(measures).map(heightOf).sum(); + height = seq(measures.map(heightOf)).sum(); if (height <= area.h || fontSize <= 4) { break; } diff --git a/test/TestUtil.js b/test/TestUtil.js index 92bada59..b95c6e42 100644 --- a/test/TestUtil.js +++ b/test/TestUtil.js @@ -3,6 +3,8 @@ import {describe} from "src/base/Describe.js" import {equate} from "src/base/Equate.js" import {WglTexturePool} from "src/webgl/WglTexturePool.js" import {DetailedError} from "src/base/DetailedError.js" +import {Config} from "src/Config.js" +Config.CHECK_WEB_GL_ERRORS_EVEN_ON_HOT_PATHS = true; /** @type {!int} */ let assertionSubjectIndexForNextTest = 1;