From 4ed7600fd6d65e8fec639e187c267bcb3b2735fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Fri, 22 May 2026 11:30:17 +0200 Subject: [PATCH] hugolib: Use AllTranslated in IsTranslated MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Which makes it faster: ``` │ benchcmp.bench │ feat-speedupistranslated.bench │ │ sec/op │ sec/op vs base │ IsTranslatedOneLanguage-10 996.2n ± ∞ ¹ 685.6n ± ∞ ¹ -31.18% (p=0.029 n=4) ¹ need >= 6 samples for confidence interval at level 0.95 │ benchcmp.bench │ feat-speedupistranslated.bench │ │ B/op │ B/op vs base │ IsTranslatedOneLanguage-10 832.0 ± ∞ ¹ 536.0 ± ∞ ¹ -35.58% (p=0.029 n=4) ¹ need >= 6 samples for confidence interval at level 0.95 │ benchcmp.bench │ feat-speedupistranslated.bench │ │ allocs/op │ allocs/op vs base │ IsTranslatedOneLanguage-10 18.00 ± ∞ ¹ 13.00 ± ∞ ¹ -27.78% (p=0.029 n=4) ¹ need >= 6 samples for confidence interval at level 0.95 ```` --- hugolib/page.go | 2 +- hugolib/page_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/hugolib/page.go b/hugolib/page.go index 759e4fe36..04be4428e 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -475,7 +475,7 @@ func (ps *pageState) String() string { // IsTranslated returns whether this content file is translated to // other language(s). func (ps *pageState) IsTranslated() bool { - return len(ps.Translations()) > 0 + return len(ps.AllTranslations()) > 1 } // TranslationKey returns the key used to identify a translation of this content. diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 1d34df54c..b634b76ef 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -1991,3 +1991,28 @@ func content(c resource.ContentProvider) string { } return ccs } + +func BenchmarkIsTranslatedOneLanguage(b *testing.B) { + // Set it reasonably high to get a balance between cached and uncached calls to IsTranslated. + const numPages = 3000 + + files := ` +-- hugo.toml -- +disableKinds = ["taxonomy", "term"] +` + for i := range numPages { + files += fmt.Sprintf(` +-- content/sect/p%d.md --`, i) + } + + bb := Test(b, files, TestOptSkipRender()) + p := bb.H.Sites[0].RegularPages() + + b.ResetTimer() + + for i := range b.N { + if p[i%numPages].IsTranslated() { + b.Fatalf("Page %d should not be translated", i) + } + } +}