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

perf: faster addMessagesSorted #470

Merged
merged 10 commits into from
Oct 20, 2020
Merged

perf: faster addMessagesSorted #470

merged 10 commits into from
Oct 20, 2020

Conversation

mahboubii
Copy link
Contributor

fixes #465

image

@github-actions
Copy link
Contributor

github-actions bot commented Oct 14, 2020

Size Change: +254 B (0%)

Total Size: 197 kB

Filename Size Change
dist/browser.es.js 41.8 kB +35 B (0%)
dist/browser.full-bundle.min.js 28.5 kB +106 B (0%)
dist/browser.js 42.3 kB +36 B (0%)
dist/index.es.js 41.8 kB +37 B (0%)
dist/index.js 42.4 kB +40 B (0%)

compressed-size-action

src/channel_state.ts Outdated Show resolved Hide resolved
let middle = 0;
let right = messages.length - 1;
while (left <= right) {
middle = Math.floor((right + left) / 2);
Copy link
Contributor

Choose a reason for hiding this comment

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

In most cases wouldn't a bottom up approach be faster than a binary search given the likelihood of a message being further up is less?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

L462 takes care of that. cases like pagination could add some 100 more messages to the other end of the array which will be O(n), right now this part is O(log(n)) which is quite fast already

Copy link
Contributor

@nhannah nhannah left a comment

Choose a reason for hiding this comment

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

Looks good lets test

@mahboubii mahboubii requested a review from ferhatelmas October 19, 2020 08:17
@@ -1658,7 +1658,7 @@ export class Channel<
if (!this.state.messages) {
this.state.messages = Immutable([]);
}
this.state.addMessagesSorted(messages, true);
this.state.addMessagesSorted(messages, false, true);
Copy link
Contributor

Choose a reason for hiding this comment

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

Having options an object with keys would help for readability.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

3 params are fine, in case it grows we can change it to object

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree in general but when they are booleans, it's trickier.

Copy link
Contributor

@ferhatelmas ferhatelmas left a comment

Choose a reason for hiding this comment

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

💯 LGTM

@mahboubii mahboubii merged commit bc10817 into master Oct 20, 2020
@delete-merged-branch delete-merged-branch bot deleted the perf-addMessagesSorted branch October 20, 2020 09:54
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.

Improve performance of channelState.addMessageSorted
4 participants