selector: Hash differently

This will be relevant for a bloom filter. And bloom filters want 12bit
hashes, so we try to produce hash values < 4096.
This commit is contained in:
Benjamin Otte 2020-01-24 17:56:53 +01:00
parent 146b921246
commit 2d5a8974b3
2 changed files with 30 additions and 5 deletions

View File

@ -138,7 +138,7 @@ gtk_css_selector_equal (const GtkCssSelector *a,
static guint
gtk_css_selector_hash_one (const GtkCssSelector *selector)
{
return GPOINTER_TO_UINT (selector->class) ^ selector->class->hash_one (selector);
return selector->class->hash_one (selector);
}
static inline gpointer *
@ -277,7 +277,7 @@ gtk_css_selector_default_match_one (const GtkCssSelector *selector,
static guint
gtk_css_selector_default_hash_one (const GtkCssSelector *selector)
{
return 0;
return GPOINTER_TO_UINT (selector->class);
}
static int
@ -610,7 +610,7 @@ match_name (const GtkCssSelector *selector,
static guint
hash_name (const GtkCssSelector *a)
{
return a->name.name;
return gtk_css_hash_name (a->name.name);
}
static int
@ -642,7 +642,7 @@ match_class (const GtkCssSelector *selector,
static guint
hash_class (const GtkCssSelector *a)
{
return a->style_class.style_class;
return gtk_css_hash_class (a->style_class.style_class);
}
static int
@ -679,7 +679,7 @@ match_id (const GtkCssSelector *selector,
static guint
hash_id (const GtkCssSelector *a)
{
return a->id.name;
return gtk_css_hash_id (a->id.name);
}
static int

View File

@ -459,6 +459,31 @@ void gtk_css_change_print (GtkCssChange
const char * gtk_css_pseudoclass_name (GtkStateFlags flags);
/* These hash functions are selected so they achieve 2 things:
* 1. collision free among each other
* Hashing the CSS selectors "button", ".button" and "#button" should give different results.
* So we multiply the hash values with distinct prime numbers.
* 2. generate small numbers
* It's why the code uses quarks instead of interned strings. Interned strings are random
* pointers, quarks are numbers increasing from 0.
* Both of these goals should achieve a bloom filter for selector matching that is as free
* of collisions as possible.
*/
static inline guint
gtk_css_hash_class (GQuark klass)
{
return klass * 5;
}
static inline guint
gtk_css_hash_name (GQuark name)
{
return name * 7;
}
static inline guint
gtk_css_hash_id (GQuark id)
{
return id * 11;
}
G_END_DECLS