Skip to content

Commit

Permalink
Use XXHash for hash table hashing of arbitrary sized keys
Browse files Browse the repository at this point in the history
Much faster than FNV
  • Loading branch information
kovidgoyal committed Dec 16, 2024
1 parent 948ae2d commit 1f723b8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
10 changes: 3 additions & 7 deletions kitty/kitty-verstable.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@

#include "../3rdparty/verstable.h"
#include <stdint.h>
#include <xxhash.h>

#ifndef __kitty_verstable_extra_hash_functions__
#define __kitty_verstable_extra_hash_functions__
// FNV-1a (matches vt_hash_string)
static inline uint64_t
vt_hash_bytes(const void *data, const size_t size) {
uint64_t hash = 0xcbf29ce484222325ull;
for (size_t i = 0; i < size; i++) hash = ( (uint64_t)(((unsigned char*)data)[i] ^ hash )) * 0x100000001b3ull;
return hash;
}

#define vt_hash_bytes XXH3_64bits

static inline uint64_t vt_hash_float(float x) { return vt_hash_integer((uint64_t)x); }
static inline bool vt_cmpr_float(float a, float b) { return a == b; }
Expand Down
13 changes: 11 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ def libcrypto_flags() -> Tuple[List[str], List[str]]:
return cflags, ldflags


@lru_cache(maxsize=2)
def xxhash_flags() -> tuple[list[str], list[str]]:
return pkg_config('libxxhash', '--cflags-only-I'), pkg_config('libxxhash', '--libs')



def at_least_version(package: str, major: int, minor: int = 0) -> None:
q = f'{major}.{minor}'
if subprocess.run([PKGCONFIG, package, f'--atleast-version={q}']
Expand Down Expand Up @@ -606,9 +612,11 @@ def kitty_env(args: Options) -> Env:
ans.secondary_version = version[1]
ans.xt_version = '.'.join(map(str, version))

xxhash = xxhash_flags()
at_least_version('harfbuzz', 1, 5)
cflags.extend(pkg_config('libpng', '--cflags-only-I'))
cflags.extend(pkg_config('lcms2', '--cflags-only-I'))
cflags.extend(xxhash[0])
# simde doesnt come with pkg-config files but some Linux distros add
# them and on macOS when building with homebrew it is required
with suppress(SystemExit, subprocess.CalledProcessError):
Expand Down Expand Up @@ -640,7 +648,7 @@ def kitty_env(args: Options) -> Env:
gl_libs = ['-framework', 'OpenGL'] if is_macos else pkg_config('gl', '--libs')
libpng = pkg_config('libpng', '--libs')
lcms2 = pkg_config('lcms2', '--libs')
ans.ldpaths += pylib + platform_libs + gl_libs + libpng + lcms2 + libcrypto_ldflags
ans.ldpaths += pylib + platform_libs + gl_libs + libpng + lcms2 + libcrypto_ldflags + xxhash[1]
if is_macos:
ans.ldpaths.extend('-framework Cocoa'.split())
elif not is_openbsd:
Expand Down Expand Up @@ -1015,8 +1023,9 @@ def files(
headers = list_files(os.path.join('kittens', kitten, '*.h')) + list(extra_headers)
return kitten, sources, headers, f'kittens/{kitten}/{output}', includes, libraries

xxhash = xxhash_flags()
for kitten, sources, all_headers, dest, includes, libraries in (
files('transfer', 'rsync', libraries=pkg_config('libxxhash', '--libs'), includes=pkg_config('libxxhash', '--cflags-only-I')),
files('transfer', 'rsync', libraries=xxhash[1], includes=xxhash[0]),
):
final_env = kenv.copy()
final_env.cflags.extend(includes)
Expand Down

0 comments on commit 1f723b8

Please sign in to comment.