Skip to content

Commit

Permalink
Use plain comparison instead of naive hashing
Browse files Browse the repository at this point in the history
It voids any kind of clashes, and is possibly easier to understand.
It results in a slight memory overhead sorting a little more cache
data, but it should not matter much here.
  • Loading branch information
b4n committed Feb 5, 2021
1 parent b08ae0f commit f600937
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions src/sciwrappers.c
Expand Up @@ -148,27 +148,33 @@ void sci_set_mark_long_lines(ScintillaObject *sci, gint type, gint column, const
* expensive operation */
static gint sci_text_height_cached(ScintillaObject *sci)
{
static guint cache_hash = 0;
struct height_spec {
gchar *font;
gint size;
gint zoom;
gint extra;
};
static struct height_spec cache = {0};
static gint cache_value = 0;
guint hash;
gchar *font;
gint size;

/* hash font, size and zoom factor using djb's algorithm which should be
* good enough for this use case. */
font = sci_get_string(sci, SCI_STYLEGETFONT, 0);
hash = g_str_hash(font);
g_free(font);
size = SSM(sci, SCI_STYLEGETSIZEFRACTIONAL, 0, 0);
hash = hash * 33 + (gint) (size & 0x00FF);
hash = hash * 33 + (gint) ((size & 0xFF00) >> 8);
hash = hash * 33 + (gint) SSM(sci, SCI_GETZOOM, 0, 0);
hash = hash * 33 + (gint) SSM(sci, SCI_GETEXTRAASCENT, 0, 0);
hash = hash * 33 + (gint) SSM(sci, SCI_GETEXTRADESCENT, 0, 0);

if (hash != cache_hash)
struct height_spec current;

current.font = sci_get_string(sci, SCI_STYLEGETFONT, 0);
current.size = SSM(sci, SCI_STYLEGETSIZEFRACTIONAL, 0, 0);
current.zoom = SSM(sci, SCI_GETZOOM, 0, 0);
current.extra = SSM(sci, SCI_GETEXTRAASCENT, 0, 0) + SSM(sci, SCI_GETEXTRADESCENT, 0, 0);

if (g_strcmp0(current.font, cache.font) == 0 &&
current.size == cache.size &&
current.zoom == cache.zoom &&
current.extra == cache.extra)
{
g_free(current.font);
}
else
{
cache_hash = hash;
g_free(cache.font);
cache = current;

cache_value = SSM(sci, SCI_TEXTHEIGHT, 0, 0);
}

This comment has been minimized.

Copy link
@elextr

elextr Feb 6, 2021

Member

LGTM

Expand Down

1 comment on commit f600937

@kugel-
Copy link
Member

@kugel- kugel- commented on f600937 Feb 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

Please sign in to comment.