From ab006b47e4ea335297d874e1519bfe011b95ca80 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 13 Mar 2026 12:56:55 +0000 Subject: [PATCH] perf(layouts): eliminate N+1 site.GetPage lookup for authors Pre-computes a map of author pages to their titles outside the main loop in `layouts/index.json`. This replaces an O(N) lookup inside the loop with an O(1) dictionary lookup, dramatically improving search index generation speed for large sites. Co-authored-by: k4rtik <374340+k4rtik@users.noreply.github.com> --- layouts/index.json | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/layouts/index.json b/layouts/index.json index c96052f..f77f3df 100644 --- a/layouts/index.json +++ b/layouts/index.json @@ -7,6 +7,15 @@ {{- /* Add author pages to index so their bios can be searched. Hide empty `/authors/` node. */ -}} {{- $pages := $pages | union (where (where site.Pages "Section" "authors") "Params.superuser" "!=" nil) -}} +{{- $author_map := dict -}} +{{- range where site.Pages "Section" "authors" -}} + {{- /* Determine the author username by looking at the directory or file name */ -}} + {{- $username := lower (path.Base (strings.TrimSuffix "/_index.md" (strings.TrimSuffix "/_index.markdown" (strings.TrimSuffix ".md" (strings.TrimSuffix ".markdown" .Path))))) -}} + {{- if and .Title .File -}} + {{- $author_map = merge $author_map (dict $username .Title) -}} + {{- end -}} +{{- end -}} + {{- range $pages -}} {{- /* Do not index drafts or private pages. */ -}} {{- if and (not .Draft) (not .Params.private) -}} @@ -44,10 +53,9 @@ {{- if gt $authorLen 0 -}} {{- $authors = slice -}} {{- range $k, $v := .Params.authors -}} - {{- $person_page_path := (printf "/authors/%s" (urlize $v)) -}} - {{- $person_page := site.GetPage $person_page_path -}} - {{- if and $person_page $person_page.File -}} - {{- $authors = $authors | append $person_page.Title -}} + {{- $author_username := urlize $v -}} + {{- if isset $author_map $author_username -}} + {{- $authors = $authors | append (index $author_map $author_username) -}} {{- else -}} {{- $authors = $authors | append ($v | plainify) -}} {{- end -}}