-
Notifications
You must be signed in to change notification settings - Fork 221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
For-each loop optimization and memoize normalizeKey #3469
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -598,7 +598,10 @@ export class Pie extends Plot { | |
const writer = new Typesettable.Writer(measurer, context); | ||
const dataset = this.datasets()[0]; | ||
const data = this._getDataToDraw().get(dataset); | ||
data.forEach((datum, datumIndex) => { | ||
const dataLen = data.length; | ||
for (let datumIndex = 0; datumIndex < dataLen; datumIndex++) { | ||
const datum = data[datumIndex]; | ||
|
||
let value = this.sectorValue().accessor(datum, datumIndex, dataset); | ||
if (!Utils.Math.isValidNumber(value)) { | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
@@ -647,6 +650,6 @@ export class Pie extends Plot { | |
xAlign: "center", | ||
yAlign: "center", | ||
}, g.node()); | ||
}); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -703,7 +703,10 @@ export class Plot extends Component { | |
const drawer = this._datasetToDrawer.get(dataset); | ||
let validDatumIndex = 0; | ||
|
||
dataset.data().forEach((datum: any, datumIndex: number) => { | ||
const data = dataset.data(); | ||
const dataLen = data.length; | ||
for (let datumIndex = 0; datumIndex < dataLen; datumIndex++) { | ||
const datum = data[datumIndex]; | ||
const position = this._pixelPoint(datum, datumIndex, dataset); | ||
if (Utils.Math.isNaN(position.x) || Utils.Math.isNaN(position.y)) { | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
@@ -724,7 +727,7 @@ export class Plot extends Component { | |
validDatumIndex, | ||
}); | ||
validDatumIndex++; | ||
}); | ||
} | ||
}); | ||
|
||
return lightweightPlotEntities; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -408,7 +408,10 @@ export class Rectangle<X, Y> extends XYPlot<X, Y> { | |
const yMin = Math.min.apply(null, yRange); | ||
const yMax = Math.max.apply(null, yRange); | ||
const data = dataToDraw.get(dataset); | ||
data.forEach((datum, datumIndex) => { | ||
const dataLength = data.length; | ||
for (let datumIndex = 0; datumIndex < dataLength; datumIndex++) { | ||
const datum = data[datumIndex]; | ||
|
||
if (datum == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (addressed, but diff doesn't recognize) |
||
return; | ||
} | ||
|
@@ -447,7 +450,7 @@ export class Rectangle<X, Y> extends XYPlot<X, Y> { | |
yAlign: "center", | ||
}, g.node()); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
private _overlayLabel(labelXRange: Range, labelYRange: Range, datumIndex: number, datasetIndex: number, | ||
|
@@ -457,7 +460,8 @@ export class Rectangle<X, Y> extends XYPlot<X, Y> { | |
for (let i = datasetIndex; i < datasets.length; i++) { | ||
const dataset = datasets[i]; | ||
const data = dataToDraw.get(dataset); | ||
for (let j = (i === datasetIndex ? datumIndex + 1 : 0); j < data.length; j++) { | ||
const dataLen = data.length; | ||
for (let j = (i === datasetIndex ? datumIndex + 1 : 0); j < dataLen; j++) { | ||
if (Utils.DOM.intersectsBBox(labelXRange, labelYRange, this._entityBBox(data[j], j, dataset, attrToProjector))) { | ||
return true; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -277,12 +277,15 @@ export class Scatter<X, Y> extends XYPlot<X, Y> { | |
const dataToDraw = this._getDataToDraw(); | ||
const attrToProjector = this._getAttrToProjector(); | ||
this.datasets().forEach((dataset) => { | ||
dataToDraw.get(dataset).forEach((datum, index) => { | ||
const data = dataToDraw.get(dataset); | ||
const dataLen = data.length; | ||
for (let index = 0; index < dataLen; index++) { | ||
const datum = data[index]; | ||
if (datum == null) { | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
this._drawLabel(datum, index, dataset, attrToProjector); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ import { IAccessor } from "../core/interfaces"; | |
import * as Utils from "./"; | ||
import { makeEnum } from "./makeEnum"; | ||
|
||
const lMemoize = require("lodash.memoize"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. even though this keeps the library size smaller, i think i'd prefer to use the normal import syntax. Unfortunately the modular lodash packages don't support There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are some instructions on how to get treeshaking to work with lodash, but i couldn't get it to work https://medium.com/@martin_hotell/tree-shake-lodash-with-webpack-jest-and-typescript-2734fa13b5cd |
||
|
||
export type GenericStackedDatum<D> = { | ||
value: number; | ||
offset: number; | ||
|
@@ -75,10 +77,14 @@ export function stack( | |
datasets.reverse(); | ||
} | ||
|
||
datasets.forEach((dataset) => { | ||
for (const dataset of datasets) { | ||
const keyToStackedDatum = new Utils.Map<string, StackedDatum>(); | ||
dataset.data().forEach((datum, index) => { | ||
const key = normalizeKey(keyAccessor(datum, index, dataset)); | ||
const data = dataset.data(); | ||
const dataLen = data.length; | ||
for (let index = 0; index < dataLen; index++) { | ||
const datum = data[index]; | ||
const accessedKey = keyAccessor(datum, index, dataset); | ||
const key = normalizeKey(accessedKey); | ||
const value = +valueAccessor(datum, index, dataset); | ||
let offset: number; | ||
const offsetMap = (value >= 0) ? positiveOffsets : negativeOffsets; | ||
|
@@ -92,14 +98,14 @@ export function stack( | |
keyToStackedDatum.set(key, { | ||
offset: offset, | ||
value: value, | ||
axisValue: keyAccessor(datum, index, dataset), | ||
axisValue: accessedKey, | ||
originalDatum: datum, | ||
originalDataset: dataset, | ||
originalIndex: index, | ||
}); | ||
}); | ||
} | ||
datasetToKeyToStackedDatum.set(dataset, keyToStackedDatum); | ||
}); | ||
} | ||
return datasetToKeyToStackedDatum; | ||
} | ||
|
||
|
@@ -120,8 +126,9 @@ export function stackedExtents<D>(stackingResult: GenericStackingResult<D>): { | |
stackingResult.forEach((stack) => { | ||
stack.forEach((datum: GenericStackedDatum<D>, key) => { | ||
// correctly handle negative bar stacks | ||
const maximalValue = Utils.Math.max([datum.offset + datum.value, datum.offset], datum.offset); | ||
const minimalValue = Utils.Math.min([datum.offset + datum.value, datum.offset], datum.offset); | ||
const offsetValue = datum.offset + datum.value; | ||
const maximalValue = Utils.Math.max([offsetValue, datum.offset], datum.offset); | ||
const minimalValue = Utils.Math.min([offsetValue, datum.offset], datum.offset); | ||
|
||
const { axisValue } = datum; | ||
|
||
|
@@ -153,13 +160,16 @@ export function stackedExtents<D>(stackingResult: GenericStackingResult<D>): { | |
export function stackedExtent(stackingResult: StackingResult, keyAccessor: IAccessor<any>, filter: IAccessor<boolean>) { | ||
const extents: number[] = []; | ||
stackingResult.forEach((stackedDatumMap: Utils.Map<string, StackedDatum>, dataset: Dataset) => { | ||
dataset.data().forEach((datum, index) => { | ||
const data = dataset.data(); | ||
const dataLen = data.length; | ||
for (let index = 0; index < dataLen; index++) { | ||
const datum = data[index]; | ||
if (filter != null && !filter(datum, index, dataset)) { | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
const stackedDatum = stackedDatumMap.get(normalizeKey(keyAccessor(datum, index, dataset))); | ||
extents.push(stackedDatum.value + stackedDatum.offset); | ||
}); | ||
} | ||
}); | ||
const maxStackExtent = Utils.Math.max(extents, 0); | ||
const minStackExtent = Utils.Math.min(extents, 0); | ||
|
@@ -173,6 +183,6 @@ export function stackedExtent(stackingResult: StackingResult, keyAccessor: IAcce | |
* @param {any} key The key to be normalized | ||
* @return {string} The stringified key | ||
*/ | ||
export function normalizeKey(key: any) { | ||
export const normalizeKey = lMemoize((key: any) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @themadcreator I believe that any There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this memoization will actually help because in order to create the cache key and store the value, it will convert the argument into a |
||
return String(key); | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return
->continue