Skip to content

Commit

Permalink
[FIX] model: don't flatten command result
Browse files Browse the repository at this point in the history
`array.flat()` is slow because it creates a new array (and allocates memory)

With this commit, we only execute `array.flat()` if the command as been refused
by one of the plugins (for possibly several resons)

`checkDispatchAllowed` self-time could take up to 4% of the total time when loading
a spreadsheet with lots of initial revisions.

Now it's 0.0% :)

closes #3204

X-original-commit: 537b129
Signed-off-by: Rémi Rahir (rar) <[email protected]>
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
  • Loading branch information
LucasLefevre committed Nov 17, 2023
1 parent 307674d commit 1193498
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,21 +442,24 @@ export class Model extends EventBus<any> implements CommandDispatcher {
* Check if the given command is allowed by all the plugins and the history.
*/
private checkDispatchAllowed(command: Command): DispatchResult {
if (isCoreCommand(command)) {
return this.checkDispatchAllowedCoreCommand(command);
const results = isCoreCommand(command)
? this.checkDispatchAllowedCoreCommand(command)
: this.checkDispatchAllowedLocalCommand(command);
if (results.some((r) => r !== CommandResult.Success)) {
return new DispatchResult(results.flat());
}
return this.checkDispatchAllowedLocalCommand(command);
return DispatchResult.Success;
}

private checkDispatchAllowedCoreCommand(command: CoreCommand): DispatchResult {
private checkDispatchAllowedCoreCommand(command: CoreCommand) {
const results = this.corePlugins.map((handler) => handler.allowDispatch(command));
results.push(this.range.allowDispatch(command));
return new DispatchResult(results.flat());
return results;
}

private checkDispatchAllowedLocalCommand(command: LocalCommand): DispatchResult {
private checkDispatchAllowedLocalCommand(command: LocalCommand) {
const results = this.uiHandlers.map((handler) => handler.allowDispatch(command));
return new DispatchResult(results.flat());
return results;
}

private finalize() {
Expand Down

0 comments on commit 1193498

Please sign in to comment.