Skip to content

Revert "Optimise Ajv cache usage" #1070

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

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

Conversation

0bex0
Copy link
Contributor

@0bex0 0bex0 commented May 20, 2025

Reverts #1062

Comment highlighted issues with the ajv library which make this fix redundant, while inadvertently adding extra memory usage by using a secondary cache which is not used.

@cdimascio
Copy link
Owner

@0bex0 curious whether you've created an issue with ajv regarding the double caching. Would love to solve it there, upgrade the dep, and retain your fix (assuming it's feasible within a quick timeframe)

@cdimascio
Copy link
Owner

cdimascio commented May 21, 2025

Alternatively, we might maintain our own cache, essentially compile the schema on first use, then cache the schema via a map owned by the validator, rather than using ajv's addSchema.

Essentially, this approach https://ajv.js.org/guide/managing-schemas.html#caching-schemas-in-your-code

@0bex0
Copy link
Contributor Author

0bex0 commented May 21, 2025

Alternatively, we might maintain our own cache, essentially compile the schema on first use, then cache the schema via a map owned by the validator, rather than using ajv's addSchema.

Essentially, this approach https://ajv.js.org/guide/managing-schemas.html#caching-schemas-in-your-code

The ajv.compileAsync call still caches the full schema object and SchemaEnv object within the ajv instance. The whole point of this internal cache is to avoid the re-compilation of schemas, but it's dependent on the exact same object reference for a schema to be used for the cache to actually work, which does not work with the express-openapi-validator implementation, since the same object reference is never used for any two schemas due to the logic in Validator._schemaBody.

I'm not sure (without any updates to the ajv library) there's a simple way to improve the memory usage w.r.t to the caching in this library at the moment. I'm going to raise an issue in the ajv library to at least fix the double caching and then this fix is retained, that would at least abate the problem of using the whole schema object as a cache key.

@0bex0
Copy link
Contributor Author

0bex0 commented May 21, 2025

I have added an issue here: ajv-validator/ajv#2554 (comment) and will address it in due course. This may not be possible until next week due to other commitments, but I have a fix in mind, yet to test it.

@0bex0
Copy link
Contributor Author

0bex0 commented May 22, 2025

In order to be backwards compatible, the double caching logic has to stay in place in the Ajv library, however I proposed adding an instance option which would allow users more flexibility w.r.t to how they choose to cache SchemaEnv objects.

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