On 7/26/24 11:00 PM, Jeff Davis wrote:
Results (in ms):

               "C"   "libc_c"   overhead
    master:    6350     7855     24%
    v4-0001:   6091     6324      4%

I got more overhead in my quick benchmarking when I ran the same benchmark. Also tried your idea with caching the last lookup (PoC patch attached) and it basically removed all overhead, but I guess it will not help if you have two different non.default locales in the same query.

            "C"   "libc_c" overhead
before:     6695  8376     25%
after:      6605  7340     11%
cache last: 6618  6677      1%

But even without that extra optimization I think this patch is worth merging and the patch is small, simple and clean and easy to understand and a just a clear speed up. Feels like a no brainer. I think that it is ready for committer.

And then we can discuss after committing if an additional cache of the last locale is worth it or not.

Andreas
From 5f634670569a3ef8249ff1747af2157b6939f505 Mon Sep 17 00:00:00 2001
From: Andreas Karlsson <andr...@proxel.se>
Date: Sun, 28 Jul 2024 00:04:43 +0200
Subject: [PATCH] WIP: Ugly caching of last locale

---
 src/backend/utils/adt/pg_locale.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c
index 4628fcd8dd..e0de7aa625 100644
--- a/src/backend/utils/adt/pg_locale.c
+++ b/src/backend/utils/adt/pg_locale.c
@@ -1566,6 +1566,9 @@ init_database_collation(void)
 	ReleaseSysCache(tup);
 }
 
+static Oid last_collid = InvalidOid;
+static pg_locale_t last_locale = NULL;
+
 /*
  * Create a locale_t from a collation OID.  Results are cached for the
  * lifetime of the backend.  Thus, do not free the result with freelocale().
@@ -1587,6 +1590,9 @@ pg_newlocale_from_collation(Oid collid)
 	if (collid == DEFAULT_COLLATION_OID)
 		return &default_locale;
 
+	if (collid == last_collid)
+		return last_locale;
+
 	cache_entry = lookup_collation_cache(collid);
 
 	if (cache_entry->locale == 0)
@@ -1712,6 +1718,9 @@ pg_newlocale_from_collation(Oid collid)
 		cache_entry->locale = resultp;
 	}
 
+	last_collid = collid;
+	last_locale = cache_entry->locale;
+
 	return cache_entry->locale;
 }
 
-- 
2.43.0

Reply via email to