From 0d3e06cecf57aa62acbeccf6bc6b0e8b6abc1127 Mon Sep 17 00:00:00 2001 From: Will Beason Date: Fri, 6 Mar 2026 22:35:32 -0600 Subject: [PATCH] go: Reduce memory usage and improve speed of Decode by 26% Use handwritten version of strings.Map. This avoids having to dynamically allocate and resize the byte slice, and allows the compiler to fully remove memory allocations from the operation. As a result, the time spent in StripCode is almost completely removed. --- go/olc.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/go/olc.go b/go/olc.go index cda9ca96..ba3a0689 100644 --- a/go/olc.go +++ b/go/olc.go @@ -188,18 +188,19 @@ func upper(b byte) byte { // The code is truncated to the first 15 digits, as Decode won't use more, // to avoid underflow errors. func StripCode(code string) string { - code = strings.Map( - func(r rune) rune { - if r == Separator || r == Padding { - return -1 - } - return rune(upper(byte(r))) - }, - code) - if len(code) > maxCodeLen { - return code[:maxCodeLen] + result := make([]byte, maxCodeLen) + pos := 0 + for _, r := range code { + if r == Separator || r == Padding { + continue + } + result[pos] = upper(byte(r)) + pos++ + if pos >= maxCodeLen { + break + } } - return code + return string(result[:pos]) } // Because the OLC codes are an area, they can't start at 180 degrees, because they would then have something > 180 as their upper bound.