Skip to content

Commit 005c6b8

Browse files
committed
Change collation cache to use simplehash.h.
Speeds up text comparison expressions when using a collation other than the database default collation. Does not affect larger operations such as ORDER BY, because the lookup is only done once. Discussion: https://postgr.es/m/7bb9f018d20a7b30b9a7f6231efab1b5e50c7720.camel@j-davis.com Reviewed-by: John Naylor, Andreas Karlsson
1 parent cdd6ab9 commit 005c6b8

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

src/backend/utils/adt/pg_locale.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@
5656

5757
#include "access/htup_details.h"
5858
#include "catalog/pg_collation.h"
59+
#include "common/hashfn.h"
5960
#include "mb/pg_wchar.h"
6061
#include "miscadmin.h"
6162
#include "utils/builtins.h"
6263
#include "utils/formatting.h"
6364
#include "utils/guc_hooks.h"
64-
#include "utils/hsearch.h"
6565
#include "utils/lsyscache.h"
6666
#include "utils/memutils.h"
6767
#include "utils/pg_locale.h"
@@ -129,10 +129,27 @@ typedef struct
129129
bool ctype_is_c; /* is collation's LC_CTYPE C? */
130130
bool flags_valid; /* true if above flags are valid */
131131
pg_locale_t locale; /* locale_t struct, or 0 if not valid */
132-
} collation_cache_entry;
133132

134-
static HTAB *collation_cache = NULL;
133+
/* needed for simplehash */
134+
uint32 hash;
135+
char status;
136+
} collation_cache_entry;
135137

138+
#define SH_PREFIX collation_cache
139+
#define SH_ELEMENT_TYPE collation_cache_entry
140+
#define SH_KEY_TYPE Oid
141+
#define SH_KEY collid
142+
#define SH_HASH_KEY(tb, key) murmurhash32((uint32) key)
143+
#define SH_EQUAL(tb, a, b) (a == b)
144+
#define SH_GET_HASH(tb, a) a->hash
145+
#define SH_SCOPE static inline
146+
#define SH_STORE_HASH
147+
#define SH_DECLARE
148+
#define SH_DEFINE
149+
#include "lib/simplehash.h"
150+
151+
static MemoryContext CollationCacheContext = NULL;
152+
static collation_cache_hash *CollationCache = NULL;
136153

137154
#if defined(WIN32) && defined(LC_MESSAGES)
138155
static char *IsoLocaleName(const char *);
@@ -1235,18 +1252,16 @@ lookup_collation_cache(Oid collation, bool set_flags)
12351252
Assert(OidIsValid(collation));
12361253
Assert(collation != DEFAULT_COLLATION_OID);
12371254

1238-
if (collation_cache == NULL)
1255+
if (CollationCache == NULL)
12391256
{
1240-
/* First time through, initialize the hash table */
1241-
HASHCTL ctl;
1242-
1243-
ctl.keysize = sizeof(Oid);
1244-
ctl.entrysize = sizeof(collation_cache_entry);
1245-
collation_cache = hash_create("Collation cache", 100, &ctl,
1246-
HASH_ELEM | HASH_BLOBS);
1257+
CollationCacheContext = AllocSetContextCreate(TopMemoryContext,
1258+
"collation cache",
1259+
ALLOCSET_DEFAULT_SIZES);
1260+
CollationCache = collation_cache_create(
1261+
CollationCacheContext, 16, NULL);
12471262
}
12481263

1249-
cache_entry = hash_search(collation_cache, &collation, HASH_ENTER, &found);
1264+
cache_entry = collation_cache_insert(CollationCache, collation, &found);
12501265
if (!found)
12511266
{
12521267
/*

0 commit comments

Comments
 (0)