-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
perf(compile): code cache #26528
perf(compile): code cache #26528
Conversation
This is very promising. Have you thought of including the initial code cache inside the compiled executable as well? |
Yeah, maybe at a later time and as a separate option. The problem is it will make the binary much larger. |
It all depends on the use case. For some use cases, improving the startup time on the second attempt onwards as opposed to in the first attempt in favor of a reduced binary size will make a lot of sense. For other cases, it will not make any sense at all. For example: containers. There's only one startup attempt in that case. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@@ -337,6 +380,23 @@ impl ModuleLoader for EmbeddedModuleLoader { | |||
))), | |||
} | |||
} | |||
|
|||
fn code_cache_ready( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated to this PR, we should make this method be able to return sync or async response to save some ticks maybe.
@dsherret, how do I verify code cache is actually working on Deno 2.1.0? |
I ran a benchmark and the improvements I found are very subtle, I wonder if code cache is indeed working:
|
@felipecrs if you compile with |
Thank you, I guess there's something wrong then: ❯ ./pkgx --version 2>&1 | grep cache
DEBUG RS - denort::cache::cache_db:170 - Opening cache /home/felipecrs/.cache/deno/node_analysis_cache_v2...
DEBUG RS - denort::standalone::code_cache:56 - Failed to deserialize code cache: Cache key mismatch
❯ ./pkgx --version 2>&1 | grep cache
DEBUG RS - denort::cache::cache_db:170 - Opening cache /home/felipecrs/.cache/deno/node_analysis_cache_v2...
DEBUG RS - denort::standalone::code_cache:56 - Failed to deserialize code cache: Cache key mismatch
❯ deno clean
Removed /home/felipecrs/.cache/deno (7865 files, 58.14MB)
❯ ./pkgx --version 2>&1 | grep cache
DEBUG RS - denort::cache::cache_db:170 - Opening cache /home/felipecrs/.cache/deno/node_analysis_cache_v2...
DEBUG RS - denort::standalone::code_cache:56 - Failed to deserialize code cache: Cache key mismatch
DEBUG RS - denort::cache::cache_db:389 - Created parent directory for cache db.
❯ ./pkgx --version 2>&1 | grep cache
DEBUG RS - denort::cache::cache_db:170 - Opening cache /home/felipecrs/.cache/deno/node_analysis_cache_v2...
DEBUG RS - denort::standalone::code_cache:56 - Failed to deserialize code cache: Cache key mismatch Any ideas? |
@felipecrs can you open an issue with a reproduction? |
|
Ok, I just benchmarked against ❯ deno compile --lock=deno.lock --allow-all --output "$INIT_CWD/pkgx" ./entrypoint.ts
Check file:///home/felipecrs/repos/pkgx/entrypoint.ts
Compile file:///home/felipecrs/repos/pkgx/entrypoint.ts to /home/felipecrs/repos/pkgx/pkgx
❯ mv pkgx pkgx-cc
❯ deno compile --lock=deno.lock --allow-all --output "$INIT_CWD/pkgx" --no-code-cache ./entrypoint.ts
Compile file:///home/felipecrs/repos/pkgx/entrypoint.ts to /home/felipecrs/repos/pkgx/pkgx
❯ mv pkgx pkgx-no-cc
❯ hyperfine --warmup 2 --shell none --runs 50 './pkgx-no-cc [email protected] --version' './pkgx-cc [email protected] --version'
Benchmark 1: ./pkgx-no-cc [email protected] --version
Time (mean ± σ): 550.1 ms ± 18.8 ms [User: 575.0 ms, System: 219.5 ms]
Range (min … max): 525.6 ms … 606.3 ms 50 runs
Benchmark 2: ./pkgx-cc [email protected] --version
Time (mean ± σ): 532.3 ms ± 16.5 ms [User: 560.5 ms, System: 212.0 ms]
Range (min … max): 515.2 ms … 572.9 ms 50 runs
Summary
./pkgx-cc [email protected] --version ran
1.03 ± 0.05 times faster than ./pkgx-no-cc [email protected] --version And indeed, we have an improvement. It is not fair to say the improvement is subtle, because it depends entirely on the application being tested and how long it takes to start. Thank you very much, this is a really nice addition to Deno. |
The performance improvement will be most noticable when there's files that are quite large. For a bunch of smallish files it won't be that much (in the example above I'm importing a large bundled file) |
Adds a lazily created code cache to
deno compile
by default.The code cache is created on first run to a single file in the temp directory and is only written once. After it's been written, the code cache becomes read only on subsequent runs. Only the modules loaded during startup are cached (dynamic imports are not code cached).
The code cache can be disabled by compiling with
--no-code-cache
.Closes #26502