Skip to content

messagesReducer: Address a performance TODO for EVENT_NEW_MESSAGE.#4409

Merged
gnprice merged 4 commits intozulip:masterfrom
chrisbobbe:pr-immutable-messages-followup
Jan 22, 2021
Merged

messagesReducer: Address a performance TODO for EVENT_NEW_MESSAGE.#4409
gnprice merged 4 commits intozulip:masterfrom
chrisbobbe:pr-immutable-messages-followup

Conversation

@chrisbobbe
Copy link
Copy Markdown
Contributor

Discovered while working on #4390, which was recently merged.


messagesReducer: Address a performance TODO for EVENT_NEW_MESSAGE.

Using the recently added getNarrowsForMessage.

And leave bidirectional comments with narrowsReducer so that we
never end up with messages in state.narrows that don't exist in
state.messages.

@chrisbobbe chrisbobbe requested a review from gnprice January 14, 2021 16:05
Copy link
Copy Markdown
Member

@gnprice gnprice left a comment

Choose a reason for hiding this comment

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

Thanks! Small comments below.

Comment thread src/message/messagesReducer.js Outdated
Comment on lines +41 to +42
// `narrowsForMessage` will always have at least length 1 because of
// the home narrow.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this fact needed?

(I think the minimum possible length is actually 3, fwiw: either home, stream, and topic, or home, all-PMs, and PM thread.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah, I suppose that could be removed. I found myself wondering what Array.prototype.some does with an empty array (turns out it returns false for any condition), and I thought it might be helpful to the next reader if they knew they didn't have to wonder about it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Got it. I think it's best to see that behavior as the only way that some could reasonably work:

  • Its job is to return true just if there's some element that the condition is true for.
  • So if there are no elements at all, then there are certainly none that the condition is true for, and the return value should be false.

Or from another angle:

  • Taking away elements from an array should only ever turn the return value of some from true to false, never from false to true.
  • So if there's any array where some returns false for a given condition, then taking away all its elements should leave it still false.
  • Moreover there was just one element the condition was true for and you take that one away, the return value should become false.
  • So if you have an array where some returns true, and you take a bunch of elements away and you're down to just one left, then even if the return value is true at that point, it should be false after you take that last element away.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh OK, makes sense! Thanks.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it's best to see that behavior as the only way that some could reasonably work:

  • Its job is to return true just if there's some element that the condition is true for.
  • So if there are no elements at all, then there are certainly none that the condition is true for, and the return value should be false.

This is also basically exactly what we want to know in this piece of code, too:

  • We want to add the message just if there's some narrow it belongs in where we're caught up.
  • So we get an array of narrows this message belongs in, and we ask if any of them is a narrow where we're caught up.

If for some reason there were no narrows this message belongs in, then the answer would be no, there's no narrow this message belongs in where we're caught up. Which is exactly what Array#some gives us.

Comment thread src/message/messagesReducer.js Outdated
Comment on lines +50 to +54
// We only need to record the message if we intend to show it right
// away in any narrow, i.e., before the next MESSAGE_FETCH_COMPLETE.
// We only intend to show it right away in a narrow if we know it's
// that narrow's most recent message, i.e., we know we're currently
// looking at (caught up with) the newest messages in the narrow.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it's not quite about whether we'll "show it right away" -- we could be caught up simply if the user has previously gone and looked at that narrow, and seen the latest messages in it, and so we've stayed caught up since then.

What's really going on is basically a converse to the warning added below about making sure this contains everything that's in state.narrows: it's that we don't really have a use for anything to be in state.messages if it isn't in state.narrows. The only times we try to access a message from here are when we've found its ID under some narrow in state.narrows.

@chrisbobbe chrisbobbe force-pushed the pr-immutable-messages-followup branch from bd3dd8a to 0f19fef Compare January 22, 2021 22:33
@chrisbobbe
Copy link
Copy Markdown
Contributor Author

Thanks for the review! Revision pushed.

@gnprice gnprice force-pushed the pr-immutable-messages-followup branch from 0f19fef to 42bd74b Compare January 22, 2021 22:39
We don't expect this to happen, and a similar check has been done
for the same Redux action in narrowsReducer for some time now. But
we haven't yet made it quite clear to Flow that `flags` will
normally be present here, and we'll want to do that for an upcoming
change.
…vent.

Like we did recently, in narrowsReducer, in 47d4f97.

Also nudge a TODO down a few lines, closer to where it belongs.
Using the recently added `getNarrowsForMessage`.

And leave bidirectional comments with narrowsReducer so that we
never end up with messages in `state.narrows` that don't exist in
`state.messages`.
@gnprice gnprice force-pushed the pr-immutable-messages-followup branch from 42bd74b to d7c25bd Compare January 22, 2021 22:40
@gnprice gnprice merged commit d7c25bd into zulip:master Jan 22, 2021
@gnprice
Copy link
Copy Markdown
Member

gnprice commented Jan 22, 2021

Looks good, thanks! Merged.

The rebase required a small fix to replace action.ownUser with action.ownUserId, to avoid a type error after rebasing past #4424.

@chrisbobbe
Copy link
Copy Markdown
Contributor Author

The rebase required a small fix to replace action.ownUser with action.ownUserId, to avoid a type error after rebasing past #4424.

Whew—thanks for catching that and doing that.

@gnprice
Copy link
Copy Markdown
Member

gnprice commented Jan 22, 2021

You can thank Flow for catching it 😉 -- which was probably the trickier part.

@chrisbobbe chrisbobbe deleted the pr-immutable-messages-followup branch November 4, 2021 22:03
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.

2 participants