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

Improved looping; safely mutate listeners queue #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

crazy4groovy
Copy link

Admittedly I can't run the tests on my Windows machine :(
So take this PR with a grain of salt.

npm-run-all --silent -p build:main build:integrations build:combined -s size docs

Error: ENOENT: no such file or directory, open 'D:\dev\projects\unistore\src\integrations*.js'
Error: ENOENT: no such file or directory, open 'D:\dev\projects\unistore\src\combined*.js'
Build output to dist:
554 B: unistore.es.js
323 B: unistore.js
391 B: unistore.umd.js
325 B
ERROR There is no matching file for full/preact.js in D:\dev\projects\unistore

Cheers

@crazy4groovy crazy4groovy changed the title Improved looping; use const where possible; simplify assign usage Improved looping; simplify assign usage Jan 20, 2018
package.json Outdated
@@ -89,7 +90,7 @@
"eslint-config-developit": "^1.1.1",
"gzip-size-cli": "^2.1.0",
"jest": "^21.2.1",
"microbundle": "^0.2.4",
"microbundle": "^0.3.1",
Copy link
Author

Choose a reason for hiding this comment

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

Needed update to work on Windows

src/index.js Outdated
}
listeners = out;
const i = listeners.indexOf(listener);
if (i > -1) listeners.splice(i, 1);
Copy link
Author

Choose a reason for hiding this comment

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

Simple array mutation

Copy link
Owner

Choose a reason for hiding this comment

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

This shifts the indices within the Array during event firing, which can cause the wrong set of listeners to be invoked.

Copy link
Author

Choose a reason for hiding this comment

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

The listeners are called in descending order, so I think the shifted index is irrelevant - the others before it will still be called.

src/util.js Outdated
for (let i in props) obj[i] = props[i];
return obj;
// Lighter Object.assign clone
export function assign(_) {
Copy link
Author

Choose a reason for hiding this comment

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

multi object support

Copy link
Owner

Choose a reason for hiding this comment

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

This is expensive: now assign() has variable arity and is unlikely to be optimized.

Copy link
Author

Choose a reason for hiding this comment

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

It saves bytes compared to its uses.
Also, I'm pretty sure that this is a safe use for optimizations. See https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#what-is-safe-arguments-usage

Copy link
Author

Choose a reason for hiding this comment

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

On this note, why not just use Object.assign? Support is extremely good: http://kangax.github.io/compat-table/es6/#test-Object_static_methods

Copy link
Owner

@developit developit Jan 25, 2018

Choose a reason for hiding this comment

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

how are you checking the byte savings? we only care about gzipped output size, not the source size. Unistore supports IE9+ so Object.assign isn't available.

Copy link
Author

Choose a reason for hiding this comment

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

Would you "object" (pun intended) to documenting a need for a polyfill for older (IE <= 11) browser support? Could rip this out altogether.
If not:
I based my saved bytes comment off the fact that assign doesn't need multiple invocations for combining multiple objects, as is its use case in the code. I could test which net-size is smaller between the two ways.

src/index.js Outdated
}

function setState(update, overwrite, action) {
state = overwrite ? update : assign(assign({}, state), update);
let currentListeners = listeners;
Copy link
Owner

Choose a reason for hiding this comment

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

Remove this means that a subscribed function that unsubcribes itself will cause the next listener to be skipped.

Copy link
Author

Choose a reason for hiding this comment

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

Are you attempting to hedge the case where a listener cb unsubscribes itself, thus modifies the listeners array? I can see your point now. I may write a test case to prove your example.

Copy link
Owner

Choose a reason for hiding this comment

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

yup.

Copy link
Author

Choose a reason for hiding this comment

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

Does the new test satisfy your concern now?

src/index.js Outdated
for (let i=0; i<arguments.length; i++) args.push(arguments[i]);
let ret = action.apply(this, args);
const args = [state];
const l = arguments.length;
Copy link
Owner

Choose a reason for hiding this comment

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

Hoisting this adds bytes and can actually hurt performance.

mraleph has a great blog post describing the effect in detail here:
http://mrale.ph/blog/2014/12/24/array-length-caching.html

Copy link
Author

Choose a reason for hiding this comment

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

I'll agree with these points.

let out = [];
for (let i=0; i<listeners.length; i++) {
if (listeners[i]===listener) {
listener = null;
Copy link
Author

Choose a reason for hiding this comment

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

Looking at this, I'm not sure what value is added by setting listener to null here. Wouldn't listener just fall out of scope and be gc'ed anyway?

Copy link
Owner

Choose a reason for hiding this comment

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

Setting listener to null prevents the removal of any additional listeners in that loop.

expect(sub1).toHaveBeenCalledTimes(2);
expect(sub2).toHaveBeenCalledTimes(1);
expect(sub3).toHaveBeenCalledTimes(2);
expect(called).toEqual([3, 2, 1, 3, 1]);
Copy link
Author

Choose a reason for hiding this comment

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

I may be testing too much here IMO. Maybe use a sort() or something, just to check calls and not the order they were called.

package.json Outdated
@@ -28,15 +29,15 @@
"bundlesize": [
{
"path": "full/preact.js",
"maxSize": "750b"
"maxSize": "727b"
Copy link
Author

Choose a reason for hiding this comment

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

the other assign function cost an additional 17 bytes, so we should keep the existing one - good call!

@crazy4groovy crazy4groovy changed the title Improved looping; simplify assign usage Improved looping; safely mutate listeners queue Jan 27, 2018
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

2 participants