Skip to content

Commit

Permalink
Optimizing hotspots
Browse files Browse the repository at this point in the history
- Turned on slow webgl checks in tests
  • Loading branch information
Strilanc committed Oct 16, 2016
1 parent aef92c6 commit f14aedc
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
8 changes: 7 additions & 1 deletion src/base/Seq.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
39 changes: 28 additions & 11 deletions src/circuit/CircuitDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/circuit/GateColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
6 changes: 3 additions & 3 deletions src/draw/Painter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -242,15 +242,15 @@ 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;
lines = forcedLines.
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;
}
Expand Down
2 changes: 2 additions & 0 deletions test/TestUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f14aedc

Please sign in to comment.