-
Notifications
You must be signed in to change notification settings - Fork 0
perf(tools): defer tool cache version check to background refresh #36
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,24 +26,43 @@ _source_cached_tool_init() { | |
| local cache_name="$1" | ||
| local binary_name="$2" | ||
| local init_command="$3" | ||
| local binary_path | ||
| local cache_file="$_zsh_tools_cache_dir/${cache_name}.zsh" | ||
| local tmp_file="${cache_file}.tmp" | ||
| local version_file="$_zsh_tools_cache_dir/${cache_name}.version" | ||
| local current_version | ||
|
|
||
| binary_path=$(command -v "$binary_name") || return 0 | ||
| current_version=$("$binary_path" --version 2>/dev/null | head -1) || current_version="unknown" | ||
|
|
||
| # Fast path: if cache and version file exist, source directly. | ||
| # The version check is deferred to a background refresh so startup stays fast. | ||
| if [[ -s "$cache_file" && -s "$version_file" ]]; then | ||
| local cached_version | ||
| cached_version=$(<"$version_file") | ||
| if [[ "$current_version" == "$cached_version" ]]; then | ||
| source "$cache_file" | ||
| return | ||
| fi | ||
| source "$cache_file" | ||
|
|
||
| # Background refresh: if the cache is older than 24 hours, verify the | ||
| # binary version and rebuild when it has changed. | ||
| () { | ||
| emulate -L zsh | ||
| if [[ -n "$(command find "$cache_file" -mmin +1440 -print 2>/dev/null)" ]]; then | ||
| local binary_path current_version cached_version | ||
| binary_path=$(command -v "$binary_name") || return 0 | ||
| current_version=$("$binary_path" --version 2>/dev/null | head -1) || current_version="unknown" | ||
| cached_version=$(<"$version_file") | ||
| [[ "$current_version" == "$cached_version" ]] && return 0 | ||
|
|
||
| if eval "$init_command" >| "$tmp_file" 2>/dev/null; then | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a potential race condition here. If multiple shell instances (e.g., opening multiple terminal tabs at once) start simultaneously and find the cache stale, they will all attempt to write to the same |
||
| mv "$tmp_file" "$cache_file" | ||
| echo -n "$current_version" > "$version_file" | ||
| else | ||
| rm -f "$tmp_file" | ||
| fi | ||
| fi | ||
| } &! | ||
|
|
||
| return | ||
| fi | ||
|
|
||
| # Slow path: cache missing — check version and build synchronously. | ||
| local binary_path current_version | ||
| binary_path=$(command -v "$binary_name") || return 0 | ||
| current_version=$("$binary_path" --version 2>/dev/null | head -1) || current_version="unknown" | ||
|
|
||
| if eval "$init_command" >| "$tmp_file" 2>/dev/null; then | ||
| mv "$tmp_file" "$cache_file" | ||
| echo -n "$current_version" > "$version_file" | ||
|
|
||
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.
When the binary version matches the cached version, the cache file's modification time remains unchanged. This causes the
findcommand at line 42 to continue identifying the cache as 'stale' (older than 24 hours) on every subsequent shell startup. Updating the timestamp of the cache file when the version matches prevents these unnecessary background checks for another 24 hours.