diff options
| author | Paul Buetow <paul@buetow.org> | 2026-02-15 14:14:32 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-02-15 14:14:45 +0200 |
| commit | 00e5de525bde5d0d77d9553c6126908f2fdfde20 (patch) | |
| tree | 0686bc72be5f7a5bc5fd18f5dcdd6d0cfb868ac3 /lib/template.source.sh | |
| parent | e6aa888599062843409d037b4007be43ef3b0f02 (diff) | |
Improve --generate performance with incremental builds (62s -> 2s)
Add mtime-based skip logic to avoid regenerating unchanged files:
- generate::fromgmi skips .gmi files where all outputs are newer
- template::_generate_file skips templates when output is fresh
- Diff-before-overwrite in templates, gemfeed, and notes indexes
to preserve mtimes and prevent cascading cache invalidation
- Global dependency check (.lastgen sentinel) for header/footer/CSS
- Job throttling via wait -n capped at nproc cores
- Add --force flag and FORCE_REBUILD env var to bypass skip logic
- Fix misleading atom feed "empty cache" log message
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'lib/template.source.sh')
| -rw-r--r-- | lib/template.source.sh | 49 |
1 files changed, 46 insertions, 3 deletions
diff --git a/lib/template.source.sh b/lib/template.source.sh index 9a2ff38..9488ea8 100644 --- a/lib/template.source.sh +++ b/lib/template.source.sh @@ -1,6 +1,7 @@ template::generate () { log INFO 'Generating files from templates' local -i num_tpl_files=0 + declare -A _TPL_DIR_NEWEST_MTIME while read -r tpl_path; do if test -n "$CONTENT_FILTER" && ! $GREP -q "$CONTENT_FILTER" <<< "$tpl_path"; then @@ -19,12 +20,47 @@ template::draft () { template::generate } +# Compute the newest mtime among .gmi.tpl files and non-template .gmi files +# in a given directory. Result is cached in _TPL_DIR_NEWEST_MTIME associative array. +template::_dir_newest_mtime () { + local -r dir="$1"; shift + + if [[ -n "${_TPL_DIR_NEWEST_MTIME[$dir]+x}" ]]; then + echo "${_TPL_DIR_NEWEST_MTIME[$dir]}" + return + fi + + # Find newest mtime among .gmi.tpl files and non-template, non-index .gmi files + local newest=0 + local mtime + while read -r mtime _; do + mtime=${mtime%%.*} + if (( mtime > newest )); then + newest=$mtime + fi + done < <(find "$dir" -maxdepth 1 \( -name '*.gmi.tpl' -o -name '*.gmi' \) -type f \ + ! -name 'index.gmi' -printf '%T@ %p\n') + + _TPL_DIR_NEWEST_MTIME[$dir]="$newest" + echo "$newest" +} + template::_generate_file () { local -r tpl_path="$1"; shift local -r tpl_dir="$(dirname "$tpl_path")" local -r tpl="$(basename "$tpl_path")" local -r dest="${tpl/.tpl/}" + # Skip if output is newer than the template and all relevant siblings + if [[ "$FORCE_REBUILD" != yes ]] && [[ -f "$tpl_dir/$dest" ]]; then + local -r dest_mtime=$(stat -c '%Y' "$tpl_dir/$dest") + local -r dir_newest=$(template::_dir_newest_mtime "$tpl_dir") + if (( dest_mtime >= dir_newest )); then + log VERBOSE "Skipping unchanged template $tpl_path" + return + fi + fi + cd "$tpl_dir" || log PANIC "Unable to chdir to $tpl_dir" log INFO "Generating $tpl_path -> $dest" @@ -33,9 +69,16 @@ template::_generate_file () { export CURRENT_GMI="$dest" template::_generate < "$tpl" > "$dest.tmp" - mv "$dest.tmp" "$dest" - log INFO "Done generating $dest" - cd - + + # Only overwrite if content actually changed, preserving mtime for caches + if [[ -f "$dest" ]] && diff -q "$dest.tmp" "$dest" >/dev/null 2>&1; then + rm "$dest.tmp" + log VERBOSE "Template output unchanged for $dest" + else + mv "$dest.tmp" "$dest" + log INFO "Done generating $dest" + fi + cd - >/dev/null } template::_generate () { |
