Skip to content

Optimise Ajv cache usage #1062

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

Merged
merged 4 commits into from
May 10, 2025
Merged

Conversation

0bex0
Copy link
Contributor

@0bex0 0bex0 commented May 8, 2025

Introducing a key which can be used to for caching in by Ajv library.
Currently the whole schema is used as a cache key in each ajv instance1,
for very large schemas this leads to a lot of unnecessary memory usage since
a whole string representation of the schema is stored with each compile().

The ajv cache is never used, since there's already a cache implementation
in the code base, but by passing a much shorter key to ajv we optimise
memory usage.

Footnotes

  1. https://ajv.js.org/guide/managing-schemas.html#cache-key-schema-vs-key-vs-id

0bex0 and others added 4 commits May 2, 2025 18:30
Co-authored-by: Roberto Bruggemann <1833249+rbruggem@users.noreply.github.com>
Copy link
Owner

@cdimascio cdimascio left a comment

Choose a reason for hiding this comment

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

Thanks. Appreciate this update! Looks good 👍

@cdimascio cdimascio merged commit 990995b into cdimascio:master May 10, 2025
5 checks passed
@0bex0 0bex0 deleted the use-ajv-cache branch May 14, 2025 16:03
@martinslota
Copy link

I'm noticing in our test suite that express-openapi-validator consumes more memory from version 5.5.0 onwards.

I have a feeling that this change might (ironically) be the cause of that: It turns out that despite the recommendation in ajv's docs, the addSchema() method stores the schema in this.schemas under the supplied key, but before it does that, it calls the _addSchema() method which sticks the schema in this._cache as well. So, at the end of the day, the schema gets cached twice when using addSchema() instead of compile().

@martinslota
Copy link

I'm noticing in our test suite that express-openapi-validator consumes more memory from version 5.5.0 onwards.

I have a feeling that this change might (ironically) be the cause of that: It turns out that despite the recommendation in ajv's docs, the addSchema() method stores the schema in this.schemas under the supplied key, but before it does that, it calls the _addSchema() method which sticks the schema in this._cache as well. So, at the end of the day, the schema gets cached twice when using addSchema() instead of compile().

A small follow-up on this: I have now taken version 5.5.0 and reverted the changes in this commit using patch-package and ran our tests again. The extra memory pressure disappeared.

@0bex0
Copy link
Contributor Author

0bex0 commented May 20, 2025

I'm noticing in our test suite that express-openapi-validator consumes more memory from version 5.5.0 onwards.

I have a feeling that this change might (ironically) be the cause of that: It turns out that despite the recommendation in ajv's docs, the addSchema() method stores the schema in this.schemas under the supplied key, but before it does that, it calls the _addSchema() method which sticks the schema in this._cache as well. So, at the end of the day, the schema gets cached twice when using addSchema() instead of compile().

I believe you're right, and _addSchema continues to cache the key using the schema object itself meaning the memory issue w.r.t the cache itself is not addressed either. I will create a PR to revert this change both the redundant caching and object cache key need to be addressed in the ajv library itself.

0bex0 added a commit to lune-climate/express-openapi-validator that referenced this pull request May 20, 2025
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.

3 participants