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

fix: improve resolver performance #623

Merged
merged 10 commits into from
May 16, 2024
Merged

fix: improve resolver performance #623

merged 10 commits into from
May 16, 2024

Conversation

Traveller23
Copy link
Contributor

@Traveller23 Traveller23 commented Mar 25, 2024

After a certain number of components have been registered, rendering after deserialization becomes very slow, and this issue has been discussed many times on discord.
So I tried to solve this problem, and I finally realized that the root of the problem is the resolveComponent method. In the original implemention, the time complexity of the search was O(N^2 * LogN), so as soon as the number of components increased, the time needed to search increased at a faster rate.
I did two things: made the search time complexity O(LogN), and created a reverse index of the map and cached it.

This problem only occurs in the prodcution version, i.e. after the code has been compiled and converted to ES5 form. So I guess the problem is also related to the target javascript version.

Copy link

changeset-bot bot commented Mar 25, 2024

🦋 Changeset detected

Latest commit: 722ab3b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@craftjs/core Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Mar 25, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
craftjs ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 16, 2024 11:01am

Copy link
Owner

@prevwong prevwong left a comment

Choose a reason for hiding this comment

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

Thanks, looks good. Just left some nits

Comment on lines 7 to 10
let currResolver: Resolver | undefined = undefined;
const reversedResolver = new Map<React.ElementType | string, string>();

export const resolveComponent = (
Copy link
Owner

Choose a reason for hiding this comment

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

nit:

type ReversedResolver = Map<React.ComponentType | string, string>;

type CachedResolverData = {
  resolver: Resolver; 
  reversed: ReversedResolver;
}

let CACHED_RESOLVER_DATA: CachedResolverData | null = null;

const getReversedResolver = (resolver: Resolver): ReversedResolver => {
  if ( CACHED_RESOLVER_DATA && CACHED_RESOLVER_DATA.resolver === resolver ) {
    return CACHED_RESOLVER_DATA.reversed;
  }

  CACHED_RESOLVER_DATA = {
    resolver,
    reversed: new Map();

  for ( const [name, comp] of Object.entries(resolver) {...}
}

const searchComponentInResolver = (
  resolver: Resolver,
  comp: React.ElementType | string
): string | null => {
  const name = getReversedResolver(resolver).get(comp);

   if (name !== null) {
     return name;
   }

  if (typeof comp === 'string') {
     return comp;
  }

  return null;
};

Comment on lines 15 to 17
const resolvedName = resolver[componentName]
? componentName
: searchComponentInResolver(resolver, comp);
Copy link
Owner

Choose a reason for hiding this comment

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

nit: Think we can just simplify this to searchComponentInResolver in all cases since it won't really impact efficiency after the initial call:

const resolvedName = searchComponentInResolver(resolver, comp);

@Traveller23
Copy link
Contributor Author

Okay, I'll change it in two days.

@Traveller23
Copy link
Contributor Author

@prevwong One question:
Is it possible to bring this code to the top?

if (typeof comp === 'string') {
   return comp;
}

It would save a lot of searching time if we just know that the type of the comp is string and return it.

@Traveller23
Copy link
Contributor Author

I'm done refactoring, please check it again.
If the assumption above are incorrect, please let me know and I'll refactor the code again.

Comment on lines 41 to 47
const componentName = getComponentName(comp);
if (Object.hasOwn(resolver, componentName)) {
return componentName;
}

const name = getReversedResolver(resolver).get(comp);
return name !== undefined ? name : null;
Copy link
Owner

Choose a reason for hiding this comment

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

Perhaps we can remove Line 41-44.

I think if the component doesn't exists in the reversed resolver, then we can assume that the component doesn't exist.

Even if the component's name/display name exist in the resolver, it might still belong to a different component, so this may lead to bugs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's true, I'll remove those lines now.

@prevwong prevwong changed the title Resolve performance issue within resolveComponent fix: improve resolver performance May 16, 2024
@Traveller23
Copy link
Contributor Author

@prevwong All done.

Copy link
Owner

@prevwong prevwong left a comment

Choose a reason for hiding this comment

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

Thank you! ❤️

@prevwong prevwong merged commit 2633f3a into prevwong:main May 16, 2024
1 of 2 checks passed
@github-actions github-actions bot mentioned this pull request May 16, 2024
prevwong pushed a commit that referenced this pull request May 17, 2024
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