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

replace mem dependency with fast-memoize (smaller, faster) #671

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

43081j
Copy link

@43081j 43081j commented Jul 12, 2021

the mem dependency pulls in a few levels deep worth of dependencies for something extremely simple.

here i've replaced it with fast-memoize which has no dependencies and seems much faster/simpler.

if the memoizeOptions repetition puts you off, we can probably just change it to create() { return memoizeCache; } since a map coincidentally follows the same interface fast-memoize expects.

let me know if there's any changes you want, feel free to let me know if you're not interested in this change too

Copy link
Owner

@brody4hire brody4hire left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution, my apologies for the delay. I approved the CI run, sorry I missed it before.

Unfortunately the mem/memoized cache is something I really do not understand at this point, so any resources that can help me better understand this improvement would be extremely helpful. I did notice you proposed a contribution to nano-memoize, don't know if that might give us even better improvement or not.

I think it would also be ideal if you could propose this enhancement to the main Prettier project first, where I think it might get some more expert feedback. In case this improvement would not be accepted by the main Prettier project, I would still be happy to consider it in prettierX with their feedback in mind.

In general, I think it would be ideal if we could find a way to add a module such as src/common/memoize. Or maybe if we could see if some of the duplicated code could be moved into its own, new npm package. You may see that I subscribe to this philosophy: https://blog.sindresorhus.com/small-focused-modules-9238d977a92a

Comment on lines +14 to +24
const memoizeOptions = {
cache: {
create() {
return {
has: (key) => memoizeCache.has(key),
get: (key) => memoizeCache.get(key),
set: (key, value) => memoizeCache.set(key, value)
};
}
}
};
Copy link
Owner

Choose a reason for hiding this comment

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

This looks like duplicated code to me

Comment on lines +14 to +24
const memoizeOptions = {
cache: {
create() {
return {
has: (key) => memoizeCache.has(key),
get: (key) => memoizeCache.get(key),
set: (key, value) => memoizeCache.set(key, value)
};
}
}
};
Copy link
Owner

Choose a reason for hiding this comment

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

looks like duplicated code to me

Comment on lines +25 to +26
const memoizedLoad = memoize(load, memoizeOptions);
const memoizedSearch = memoize(findPluginsInNodeModules, memoizeOptions);
Copy link
Owner

Choose a reason for hiding this comment

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

There are lint:typecheck errors in these lines but I would be happy to help resolve these once we get my other comments and questions resolved.

Comment on lines +11 to +21
const memoizeOptions = {
cache: {
create() {
return {
has: (key) => memoizeCache.has(key),
get: (key) => memoizeCache.get(key),
set: (key, value) => memoizeCache.set(key, value)
};
}
}
};
Copy link
Owner

Choose a reason for hiding this comment

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

Looks like duplicated code to me

}
}
};
const jsonStringifyMem = (fn) => memoize(fn, memoizeOptions);
Copy link
Owner

Choose a reason for hiding this comment

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

I would be happy to help get the lint:typecheck error resolved here once we get my other comments and questions resolved.

@43081j
Copy link
Author

43081j commented Jul 20, 2021

no worries, i'll try get it sorted in prettier's repo too.

really its just trying to cache the plugins list/loading between multiple calls. honestly i doubt a dependency is even needed in reality, prettier should have kept the plugins in a cache/context themselves and only called the function to populate it once IMO. instead of relying on caching a function's result.

anyway ill go sort it out in prettier and update this afterwards

@brody4hire
Copy link
Owner

no worries, i'll try get it sorted in prettier's repo too.

I see prettier/prettier#11230, thanks!

One comment I have from description in prettier/prettier#11230:

or we could just throw a WeakMap together ourselves

It makes me start to wonder if it would make sense to consider using macros ... see brody4hire/ask-me-anything#35

@brody4hire
Copy link
Owner

OK it looks like the upstream work moved into prettier/prettier#11241 and has not moved forward as smoothly as I had hoped and expected. I probably should have explained more clearly that upstream Prettier uses their build step to publish with all dependencies included.

I think it would be helpful for both upstream Prettier and this fork if we could get a better understanding of the motivation behind cleaning up this one dependency for the caching. My impression is that the "bloat" of the existing mem dependency, as bad as it is, would be relatively minor in comparison to the number of dependencies and sub-dependencies that are used to support the other parts of Prettier(X).

More important to me (at least) is ease of understanding. I am very sorry to say that I have not found any of the proposals to be very easy to understand. I think it would be ideal if we could find a way to make the cached config mechanisms both less bloated and easier to understand at the same time.

Another topic which I think belongs in a separate discussion is the effect of publishing to npm directly vs publishing from a build. Publishing from a build like Prettier does makes installation a little easier, seems to fix ESM support, and supports the browser more directly. Disadvantages would include need for explicit license statement for bundled dependencies and hiding of vulnerabilities such as possible REGEX-DOS in bundled dependencies.

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