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

Removed unnecessary sort in sorted_state_adapter.ts#merge() #960

Merged
merged 1 commit into from
Mar 29, 2021

Conversation

jaidetree
Copy link
Contributor

@jaidetree jaidetree commented Mar 29, 2021

Hi Redux Toolkit maintainers,

While perusing the code, discovered the following:

Forked the repo, removed the sort, ran the tests with npm test and found no issues.

Since allEntities are sorted just after merging into state, seems like it would improve performance without the pre-sort.

Is a reason for the pre-sort I'm not seeing, that the tests are not capturing? If so I would really like to know why.

Thanks for your time!

Ran npm test and found no regressions

Co-authored-by: jmezzacappa <20446836+jmezzacappa@users.noreply.github.com>
@netlify
Copy link

netlify bot commented Mar 29, 2021

Deploy preview for redux-starter-kit-docs ready!

Built with commit f65238c

https://deploy-preview-960--redux-starter-kit-docs.netlify.app

@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit f65238c:

Sandbox Source
Vanilla Configuration
Vanilla Typescript Configuration

@markerikson
Copy link
Collaborator

Hmm. So that line of code probably came over as part of the original port I did from @ngrx/entity. Would be good to compare vs their current logic in that function.

At this point in the code, models represents any new/added items that are being worked on. I'm not 100% sure that those need to be inserted in sorted order, but I'm also not sure that skipping sorting them will avoid changing any current behavior.

@jaidetree
Copy link
Contributor Author

jaidetree commented Mar 29, 2021

https://github.com/ngrx/platform/blob/0bb49409413b784b1cc55c2b59e67ed760041aa5/modules/entity/src/sorted_state_adapter.ts#L193-L214

function merge(models: any[], state: any): void {
    models.sort(sort);

    const ids: any[] = [];

    let i = 0;
    let j = 0;

    while (i < models.length && j < state.ids.length) {
      const model = models[i];
      const modelId = selectIdValue(model, selectId);
      const entityId = state.ids[j];
      const entity = state.entities[entityId];

      if (sort(model, entity) <= 0) {
        ids.push(modelId);
        i++;
      } else {
        ids.push(entityId);
        j++;
      }
    }

Looking into the original implementation, the use of pre-sort makes sense there. The pre-sorted list of models are used to conditionally append models[i].id or original state.entities[j].id depending on what the comparer sort function returns. In the case of redux toolkit, the allEntities array is sorted after merging with the .sort(sort) method. Given this added context, it seems like the pre-sort is not needed anymore right?

@markerikson
Copy link
Collaborator

Yeah, seems that way.

I do sorta wonder how much perf impact there is from us just calling .sort() every time here, but eh, I think we can probably skip worrying about that for now :)

Thanks for looking into this!

@markerikson markerikson merged commit 4462647 into reduxjs:master Mar 29, 2021
@jaidetree
Copy link
Contributor Author

jaidetree commented Mar 30, 2021

Thanks for your support. Since there's some personal guilt due to it being such a minor contribution, I setup a quick benchmark test:

https://jsbench.me/wkkmvg41xd/1

EDIT:
Realized the ngrx/entity speedup didn't update the state.entities like the array.sort example did. Now they're behaviorally equivalent.

Results so far:

OS Browser array.sort speedup ngrx/entity speedup
OS X 11.2.3 Brave 1.21.77 ~40% -
OS X 11.2.3 Safari 14 - ~40%

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