Skip to content

Conversation

@Airfooox
Copy link
Owner

No description provided.

renovate bot and others added 16 commits January 19, 2023 18:13
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
* refactor: extract preset-to-option logic to separate function

this commit is a preparation of the introduction of a cache mechanism to speed
up the execution of cron-validate. Although the commit looks irrelevant if
considered in isolation, it greatly simplify the analysis of the next commit.

I also removed unused imports from index.ts

Signed-off-by: albertodiazdorado <albertodiazdorado@gmail.com>

* feat: load presets on module load

In the previous source code version we would load the built-in presets on every
invocation of 'cron' (via 'validateOptions'). This means that we do a lot of
repeated work on consecutive invocations of 'cron'. With the new design we load
the presets just once when we load the module 'options.ts', via the new function
'loadPresets()'.

In order to avoid circular dependencies, I modified 'presets.ts' to export
just vanilla JavaScript objects.

Signed-off-by: albertodiazdorado <albertodiazdorado@gmail.com>

* feat: improve performance by adding a cache mechanism

cron-validated was very slow when called in a loop. The reason is that we
repeated all these operations in every call:
1. Merge the preset and the overrides into a so-called 'unvalidatedConfig'
2. Dynamically generate a yup validation schema from the preset and the override
3. Validate the 'unvalidatedConfig' using the dynamically generated schema

Step 2 is particularly slow, but steps 1 and 3 aren't particularly fast either.
Luckily for us, all these 3 steps are perfectly cacheable. This Pull Request
introduces a so-called 'optionsCache' to avoid repeating the 3 steps if we
already executed them in the past (which happens all the time in a loop).

The cache is a map from 'cache keys' to 'validated options'. The cache key
is the concatenarion of 'presetId' and a stringified 'override' object. If two
function calls produce the same cache key, it invariably means that the user
wants to use the same 'Options' in both function calls. This is how the cache
produces a performance improvement of about x100 for consecutive calls.

Note that the construction of the cache key is not optimized. If the user uses
'cron-validate' two consecutive times with identical 'override' objects, but
swaps the order of the override object fields, then they will not benefit from
caching. This can be fixed by using deterministic stringification, but I do not
consider it necessary.

Signed-off-by: albertodiazdorado <albertodiazdorado@gmail.com>

---------

Signed-off-by: albertodiazdorado <albertodiazdorado@gmail.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
@Airfooox Airfooox self-assigned this Mar 13, 2025
@Airfooox Airfooox merged commit 907f5fc into master Mar 13, 2025
@Airfooox
Copy link
Owner Author

🎉 This PR is included in version 1.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants