Skip to content
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

Merged
merged 5 commits into from
May 31, 2018

Conversation

philcchen
Copy link
Contributor

@philcchen philcchen commented May 30, 2018

Optimization around for-each loops (https://jsperf.com/fast-array-foreach ) for a comparison.

Memoization of normalizeKey function, would like a sanity check to make sure that normalizeKey never gets called with a mutable object.

Note: the memoization here isn't necessarily a runtime optimization, but a garbage collection optimization/trade-off

Was seeing excessive GC (especially by Firefox) when running without the memoized keys in production environment

@@ -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) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@themadcreator I believe that any key passed in here should be immutable, but given it's typed as any, would want a sanity check?

Copy link
Contributor

Choose a reason for hiding this comment

The 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 Map key which might be just as slow as the String(key) below

@@ -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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 Map key which might be just as slow as the String(key) below

@@ -11,6 +11,8 @@ import { IAccessor } from "../core/interfaces";
import * as Utils from "./";
import { makeEnum } from "./makeEnum";

const lMemoize = require("lodash.memoize");
Copy link
Contributor

Choose a reason for hiding this comment

The 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 import statements for some reason.

Copy link
Contributor

Choose a reason for hiding this comment

The 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

data.forEach((datum, index) => {
const dataLen = data.length;
for (let index = 0; index < dataLen; index++ ) {
const datum = data[index];
if (datum == null) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return -> continue

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return -> continue

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return -> continue

const dataLength = data.length;
for (let datumIndex = 0; datumIndex < dataLength; datumIndex++) {
const datum = data[datumIndex];

if (datum == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return -> continue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(addressed, but diff doesn't recognize)

const data = dataToDraw.get(dataset);
const dataLen = data.length;
for (let index = 0; index < dataLen; index++) {
const datum = data[index];
if (datum == null) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return -> continue

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return -> continue

@blueprint-bot
Copy link

Fix line plot indices

Demo: quicktests | fiddle

@themadcreator themadcreator merged commit 3c51210 into develop May 31, 2018
@themadcreator themadcreator deleted the philcchen/normalizeKey branch May 31, 2018 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants