You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After upgrading ajv from v6 to v7 (with #8703) we've observed a slower initialization of serverless command.
It appears that schema compilation (ajv.compile(schema) execution time for very same schema jumped from ca 0.15s to 0.8s.
It also affects our test runs which locally for me jumped from 13s to 50s
I believe we need to find a way to bring back previous performance or if that will appear as not easily doable revert back to v6
Proposed solution
After discussing it in a AJV tracker -> ajv-validator/ajv#1386, it appears that solution could be to prepare a pre-compiled AJV instances for reuse (they're called "standalone" on AJV side, but they're not really standalone).
As our schema is not fixed (may be extended by plugins) we cannot simply ship serverless with pre-generated AJV validate function, still assuming that generating pre-compiled instance takes reasonable time (I didn't test it) we may take the approach in which we store it in users cache (~/.serverless/artifact) and reuse pre-compiled validate (per schema and AJV version) in all further serverless run.
) either reuse already generated validate method, or generate new one (following this instructions)
Additional notes:
As produced validate function source code is not really standalone (and may need access to ajv dependency) I believe we need to load it via require-from-string with a made up filename which is located in context of serverless package - that way generated validate will have access to same dependencies as serverless
We may consider (with other PR) refactor getEnsureArtifact so it directly resolves with memoized function, and not that it resolves with function which when invoked resolves with memoized function (I think this approach is not really needed as it was assumed and it just additionally confuses)
We may consider to further optimize for this case, and use different variant of getEnsureArtifact, not one that uses different folder per serveless version, but per ajv version (thanks to that, after upgrading serverless, users will not have validate rebuilt if underlying ajv was not upgraded) - and for that we may use some getEnsureArtifact generator that indeed returns function which returns a memoized function.
The text was updated successfully, but these errors were encountered:
Use case description
After upgrading
ajv
from v6 to v7 (with #8703) we've observed a slower initialization ofserverless
command.It appears that schema compilation (
ajv.compile(schema)
execution time for very same schema jumped from ca0.15s
to0.8s
.It also affects our test runs which locally for me jumped from
13s
to50s
I believe we need to find a way to bring back previous performance or if that will appear as not easily doable revert back to v6
Proposed solution
After discussing it in a AJV tracker -> ajv-validator/ajv#1386, it appears that solution could be to prepare a pre-compiled AJV instances for reuse (they're called "standalone" on AJV side, but they're not really standalone).
As our schema is not fixed (may be extended by plugins) we cannot simply ship
serverless
with pre-generated AJV validate function, still assuming that generating pre-compiled instance takes reasonable time (I didn't test it) we may take the approach in which we store it in users cache (~/.serverless/artifact
) and reuse pre-compiledvalidate
(per schema and AJV version) in all furtherserverless
run.Implementation proposal
serverless/lib/classes/ConfigSchemaHandler/index.js
Lines 101 to 103 in b4fef7d
serverless/lib/classes/ConfigSchemaHandler/index.js
Line 106 in b4fef7d
lib/classes/ConfigSchemaHandler/resolve-ajv-validate.js
modulelib/classes/ConfigSchemaHandler/resolve-ajv-validate.js
introduce an optimizedvalidate
function resolver:schema
option (i think schema may host circular references, so we should probably add some prevention for that)lib/util/getEnsureArtifact.js
(for reference it's used e.g. here:serverless/lib/plugins/aws/customResources/generateZip.js
Line 14 in b4fef7d
validate
method, or generate new one (following this instructions)Additional notes:
validate
function source code is not really standalone (and may need access toajv
dependency) I believe we need to load it viarequire-from-string
with a made upfilename
which is located in context ofserverless
package - that way generatedvalidate
will have access to same dependencies asserverless
getEnsureArtifact
so it directly resolves with memoized function, and not that it resolves with function which when invoked resolves with memoized function (I think this approach is not really needed as it was assumed and it just additionally confuses)getEnsureArtifact
, not one that uses different folder perserveless
version, but perajv
version (thanks to that, after upgradingserverless
, users will not havevalidate
rebuilt if underlyingajv
was not upgraded) - and for that we may use somegetEnsureArtifact
generator that indeed returns function which returns a memoized function.The text was updated successfully, but these errors were encountered: