-
Notifications
You must be signed in to change notification settings - Fork 78
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
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6ad3976
perf: faster addMessagesSorted
mahboubii 10562af
save messageTime to reduce call to getTime()
mahboubii 4b7103a
add unit-tests to CI
mahboubii 6b8a7aa
move CI unit-tests to build
mahboubii dec599a
more test for addMessagesSorted
mahboubii 8a9fe2d
addMessageSorted supports msg with updated created_at
mahboubii 2620d57
Merge branch 'master' into perf-addMessagesSorted
9b0b34a
rename test/unit-tests to test/unit
mahboubii 9d8aa9a
Merge branch 'perf-addMessagesSorted' of github.com:GetStream/stream-…
mahboubii e7b8522
Merge branch 'master' into perf-addMessagesSorted
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import chai from 'chai'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { ChannelState } from '../src/channel_state'; | ||
|
||
const expect = chai.expect; | ||
|
||
const generateMsg = (msg = {}) => { | ||
const date = msg.date || new Date().toISOString(); | ||
return { | ||
id: uuidv4(), | ||
text: 'x', | ||
html: '<p>x</p>\n', | ||
type: 'regular', | ||
user: { id: 'id' }, | ||
attachments: [], | ||
latest_reactions: [], | ||
own_reactions: [], | ||
reaction_counts: null, | ||
reaction_scores: {}, | ||
reply_count: 0, | ||
created_at: date, | ||
updated_at: date, | ||
mentioned_users: [], | ||
silent: false, | ||
status: 'received', | ||
__html: '<p>x</p>\n', | ||
...msg, | ||
}; | ||
}; | ||
|
||
describe('ChannelState addMessagesSorted', function () { | ||
it('empty state add single messages', async function () { | ||
const state = new ChannelState(); | ||
expect(state.messages).to.have.length(0); | ||
state.addMessagesSorted([ | ||
generateMsg({ id: '0', date: '2020-01-01T00:00:00.000Z' }), | ||
]); | ||
expect(state.messages).to.have.length(1); | ||
state.addMessagesSorted([ | ||
generateMsg({ id: '1', date: '2020-01-01T00:00:01.000Z' }), | ||
]); | ||
|
||
expect(state.messages).to.have.length(2); | ||
expect(state.messages[0].id).to.be.equal('0'); | ||
expect(state.messages[1].id).to.be.equal('1'); | ||
}); | ||
|
||
it('empty state add multiple messages', async function () { | ||
const state = new ChannelState(); | ||
state.addMessagesSorted([ | ||
generateMsg({ id: '1', date: '2020-01-01T00:00:00.001Z' }), | ||
generateMsg({ id: '2', date: '2020-01-01T00:00:00.002Z' }), | ||
generateMsg({ id: '0', date: '2020-01-01T00:00:00.000Z' }), | ||
]); | ||
|
||
expect(state.messages).to.have.length(3); | ||
expect(state.messages[0].id).to.be.equal('0'); | ||
expect(state.messages[1].id).to.be.equal('1'); | ||
expect(state.messages[2].id).to.be.equal('2'); | ||
}); | ||
|
||
it('update a message in place 1', async function () { | ||
const state = new ChannelState(); | ||
state.addMessagesSorted([generateMsg({ id: '0' })]); | ||
state.addMessagesSorted([{ ...state.messages[0].asMutable(), text: 'update' }]); | ||
|
||
expect(state.messages).to.have.length(1); | ||
expect(state.messages[0].text).to.be.equal('update'); | ||
}); | ||
|
||
it('update a message in place 2', async function () { | ||
const state = new ChannelState(); | ||
state.addMessagesSorted([ | ||
generateMsg({ id: '1', date: '2020-01-01T00:00:00.001Z' }), | ||
generateMsg({ id: '2', date: '2020-01-01T00:00:00.002Z' }), | ||
generateMsg({ id: '0', date: '2020-01-01T00:00:00.000Z' }), | ||
]); | ||
|
||
state.addMessagesSorted([{ ...state.messages[1].asMutable(), text: 'update' }]); | ||
|
||
expect(state.messages).to.have.length(3); | ||
expect(state.messages[1].text).to.be.equal('update'); | ||
expect(state.messages[0].id).to.be.equal('0'); | ||
expect(state.messages[1].id).to.be.equal('1'); | ||
expect(state.messages[2].id).to.be.equal('2'); | ||
}); | ||
|
||
it('update a message in place 3', async function () { | ||
const state = new ChannelState(); | ||
state.addMessagesSorted([ | ||
generateMsg({ id: '1', date: '2020-01-01T00:00:00.001Z' }), | ||
generateMsg({ id: '2', date: '2020-01-01T00:00:00.002Z' }), | ||
generateMsg({ id: '0', date: '2020-01-01T00:00:00.000Z' }), | ||
generateMsg({ id: '3', date: '2020-01-01T00:00:00.003Z' }), | ||
]); | ||
|
||
state.addMessagesSorted([{ ...state.messages[0].asMutable(), text: 'update 0' }]); | ||
expect(state.messages).to.have.length(4); | ||
expect(state.messages[0].text).to.be.equal('update 0'); | ||
|
||
state.addMessagesSorted([{ ...state.messages[2].asMutable(), text: 'update 2' }]); | ||
expect(state.messages).to.have.length(4); | ||
expect(state.messages[2].text).to.be.equal('update 2'); | ||
|
||
state.addMessagesSorted([{ ...state.messages[3].asMutable(), text: 'update 3' }]); | ||
expect(state.messages).to.have.length(4); | ||
expect(state.messages[3].text).to.be.equal('update 3'); | ||
}); | ||
|
||
it('add a message with same created_at', async function () { | ||
const state = new ChannelState(); | ||
|
||
for (let i = 0; i < 10; i++) { | ||
state.addMessagesSorted([ | ||
generateMsg({ id: `${i}`, date: `2020-01-01T00:00:00.00${i}Z` }), | ||
]); | ||
} | ||
|
||
for (let i = 10; i < state.messages.length - 1; i++) { | ||
for (let j = i + 1; i < state.messages.length - 1; j++) | ||
expect(state.messages[i].created_at.getTime()).to.be.lessThan( | ||
state.messages[j].created_at.getTime(), | ||
); | ||
} | ||
|
||
expect(state.messages).to.have.length(10); | ||
state.addMessagesSorted([ | ||
generateMsg({ id: 'id', date: `2020-01-01T00:00:00.007Z` }), | ||
]); | ||
expect(state.messages).to.have.length(11); | ||
expect(state.messages[7].id).to.be.equal('7'); | ||
expect(state.messages[8].id).to.be.equal('id'); | ||
}); | ||
|
||
it('add lots of messages in order', async function () { | ||
const state = new ChannelState(); | ||
|
||
for (let i = 100; i < 300; i++) { | ||
state.addMessagesSorted([ | ||
generateMsg({ id: `${i}`, date: `2020-01-01T00:00:00.${i}Z` }), | ||
]); | ||
} | ||
|
||
expect(state.messages).to.have.length(200); | ||
for (let i = 100; i < state.messages.length - 1; i++) { | ||
for (let j = i + 1; j < state.messages.length - 1; j++) | ||
expect(state.messages[i].created_at.getTime()).to.be.lessThan( | ||
state.messages[j].created_at.getTime(), | ||
); | ||
} | ||
}); | ||
|
||
it('add lots of messages out of order', async function () { | ||
const state = new ChannelState(); | ||
|
||
const messages = []; | ||
for (let i = 100; i < 300; i++) { | ||
messages.push(generateMsg({ id: `${i}`, date: `2020-01-01T00:00:00.${i}Z` })); | ||
} | ||
// shuffle | ||
for (let i = messages.length - 1; i > 0; i--) { | ||
const j = Math.floor(Math.random() * (i + 1)); | ||
[messages[i], messages[j]] = [messages[j], messages[i]]; | ||
} | ||
|
||
state.addMessagesSorted(messages); | ||
|
||
expect(state.messages).to.have.length(200); | ||
for (let i = 0; i < 200; i++) { | ||
expect(state.messages[i].id).to.be.equal(`${i + 100}`); | ||
} | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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?
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.
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