Speed up suggestion insertion by using a single transaction #11702
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.
When adding @-mention support for Gutenberg, I noticed that it was taking a really long time for mentions to appear the first time for a site. Part of this is due to the API call taking 3-6 seconds when there are 1500+ users on the site (a site with only a single user responded in around 300ms).
Most of the delay was due to the time it took for the data to be inserted in the database one entry at a time. On my device, inserting 1500+ suggestions for a site took ~16 seconds after the API call returned the data. This PR wraps all those insertions into a single transaction which reduced the insertion time down to under 300 milliseconds in my tests.
This change is important for Gutenberg because we are going to be using this for a UI that is only used for providing suggestions. Having that UI not work for >20 seconds the first time it is loaded is obviously an awful user experience.
Testing
@
immediately followed by a single letter (until that point there won't be any suggestions available). Given how long the API call itself can take, I would suggest watching it using Stetho, Charles, or adding a log message toSuggestionService::handleSuggestionsUpdatedResponse
so you know exactly when suggestions should be available.PR submission checklist:
RELEASE-NOTES.txt
if necessary.