From 59c6f68a39bc4c3b3d664040104666304193b550 Mon Sep 17 00:00:00 2001 From: Joel Marcotte Date: Tue, 5 May 2026 11:57:18 -0400 Subject: [PATCH] [SDBM-2597] Avoid allocation in isExtractFieldKeyword The previous map-based lookup called strings.ToUpper on every token, allocating a folded copy of the input. Switch to a flat slice walked with strings.EqualFold so the case-insensitive comparison is allocation-free. The set has 22 entries and the function only runs inside an EXTRACT(...) call, so the linear scan is trivial. Co-Authored-By: Claude Opus 4.7 (1M context) --- sqllexer_utils.go | 57 ++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/sqllexer_utils.go b/sqllexer_utils.go index b36f2f5..bdbca25 100644 --- a/sqllexer_utils.go +++ b/sqllexer_utils.go @@ -184,38 +184,43 @@ var ( // (https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT). // They appear as unquoted identifiers in `EXTRACT(field FROM source)` and must be // obfuscated to a placeholder so signatures match the pg_stat_statements form - // `EXTRACT($1 FROM source)`. - extractFieldKeywords = map[string]struct{}{ - "CENTURY": {}, - "DAY": {}, - "DECADE": {}, - "DOW": {}, - "DOY": {}, - "EPOCH": {}, - "HOUR": {}, - "ISODOW": {}, - "ISOYEAR": {}, - "JULIAN": {}, - "MICROSECONDS": {}, - "MILLENNIUM": {}, - "MILLISECONDS": {}, - "MINUTE": {}, - "MONTH": {}, - "QUARTER": {}, - "SECOND": {}, - "TIMEZONE": {}, - "TIMEZONE_HOUR": {}, - "TIMEZONE_MINUTE": {}, - "WEEK": {}, - "YEAR": {}, + // `EXTRACT($1 FROM source)`. Stored as a flat slice so isExtractFieldKeyword can + // use strings.EqualFold without allocating a folded copy of the input. + extractFieldKeywords = [...]string{ + "CENTURY", + "DAY", + "DECADE", + "DOW", + "DOY", + "EPOCH", + "HOUR", + "ISODOW", + "ISOYEAR", + "JULIAN", + "MICROSECONDS", + "MILLENNIUM", + "MILLISECONDS", + "MINUTE", + "MONTH", + "QUARTER", + "SECOND", + "TIMEZONE", + "TIMEZONE_HOUR", + "TIMEZONE_MINUTE", + "WEEK", + "YEAR", } ) // isExtractFieldKeyword reports whether value is a known PostgreSQL EXTRACT field // keyword (case-insensitive). func isExtractFieldKeyword(value string) bool { - _, ok := extractFieldKeywords[strings.ToUpper(value)] - return ok + for _, kw := range extractFieldKeywords { + if strings.EqualFold(value, kw) { + return true + } + } + return false } // extractContext tracks the two most recent non-whitespace, non-comment tokens