Skip to content

Commit ca88692

Browse files
use xxhash instead of jenkins hash
In several benchmarks, xxhash scores far better than the jenkins one at a time hash function. See https://xxhash.com/ for a comparison.
1 parent e71b6ef commit ca88692

File tree

9 files changed

+61
-31
lines changed

9 files changed

+61
-31
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
options: "-DENABLE_TESTS=ON",
2626
packager: "sudo apt-get",
2727
# no expect because stdout seems to be redirected
28-
packages: "libcmocka-dev shunit2",
28+
packages: "libcmocka-dev libxxhash-dev shunit2",
2929
snaps: "",
3030
build-cmd: "make"
3131
}
@@ -36,7 +36,7 @@ jobs:
3636
cc: "clang",
3737
options: "-DENABLE_TESTS=ON",
3838
packager: "sudo apt-get",
39-
packages: "libcmocka-dev shunit2",
39+
packages: "libcmocka-dev libxxhash-dev shunit2",
4040
snaps: "",
4141
build-cmd: "make"
4242
}
@@ -47,7 +47,7 @@ jobs:
4747
cc: "gcc",
4848
options: "",
4949
packager: "sudo apt-get",
50-
packages: "libcmocka-dev valgrind shunit2",
50+
packages: "libcmocka-dev libxxhash-dev valgrind shunit2",
5151
snaps: "",
5252
build-cmd: "make"
5353
}
@@ -59,7 +59,7 @@ jobs:
5959
options: "",
6060
packager: "sudo apt-get",
6161
# no valgrind because it does not support DWARF5 yet generated by clang 14
62-
packages: "libcmocka-dev shunit2",
62+
packages: "libcmocka-dev libxxhash-dev shunit2",
6363
snaps: "",
6464
build-cmd: "make"
6565
}
@@ -70,7 +70,7 @@ jobs:
7070
cc: "clang",
7171
options: "-DENABLE_TESTS=ON -DPATH_EXPECT=",
7272
packager: "brew",
73-
packages: "cmocka shunit2 tcl-tk",
73+
packages: "cmocka xxhash shunit2 tcl-tk",
7474
snaps: "",
7575
build-cmd: "make"
7676
}
@@ -81,7 +81,7 @@ jobs:
8181
cc: "clang",
8282
options: "-DCMAKE_C_FLAGS=-fsanitize=address,undefined -DENABLE_TESTS=ON -DENABLE_VALGRIND_TESTS=OFF",
8383
packager: "sudo apt-get",
84-
packages: "libcmocka-dev",
84+
packages: "libcmocka-dev libxxhash-dev",
8585
snaps: "",
8686
build-cmd: "make"
8787
}
@@ -92,7 +92,7 @@ jobs:
9292
cc: "gcc",
9393
options: "",
9494
packager: "sudo apt-get",
95-
packages: "libcmocka-dev abi-dumper abi-compliance-checker",
95+
packages: "libcmocka-dev libxxhash-dev abi-dumper abi-compliance-checker",
9696
snaps: "core universal-ctags",
9797
build-cmd: "make abi-check"
9898
}
@@ -103,7 +103,7 @@ jobs:
103103
cc: "gcc",
104104
options: "",
105105
packager: "sudo apt-get",
106-
packages: "cmake debhelper libcmocka-dev python3-pip",
106+
packages: "cmake debhelper libcmocka-dev libxxhash-dev python3-pip",
107107
snaps: "",
108108
build-cmd: ""
109109
}
@@ -210,7 +210,7 @@ jobs:
210210
uses: SimenB/github-actions-cpu-cores@v1
211211

212212
- name: Install Windows dependencies
213-
run: vcpkg install --triplet=${{ matrix.triplet }} pcre2 pthreads dirent dlfcn-win32 cmocka getopt
213+
run: vcpkg install --triplet=${{ matrix.triplet }} pcre2 pthreads dirent dlfcn-win32 cmocka getopt xxhash
214214

215215
- name: Configure
216216
shell: bash

.github/workflows/devel-push.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
cc: "gcc",
3535
options: "-DENABLE_COVERAGE=ON",
3636
packager: "sudo apt-get",
37-
packages: "libcmocka-dev lcov",
37+
packages: "libcmocka-dev libxxhash-dev lcov",
3838
snaps: "",
3939
make-prepend: "",
4040
make-target: ""

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,11 @@ find_package(PCRE2 10.21 REQUIRED)
411411
include_directories(${PCRE2_INCLUDE_DIRS})
412412
target_link_libraries(yang ${PCRE2_LIBRARIES})
413413

414+
# XXHash include and library
415+
find_package(XXHash REQUIRED)
416+
include_directories(${XXHASH_INCLUDE_DIR})
417+
target_link_libraries(yang ${XXHASH_LIBRARY})
418+
414419
# generated header list
415420
foreach(h IN LISTS gen_headers)
416421
list(APPEND g_headers ${PROJECT_BINARY_DIR}/libyang/${h})

CMakeModules/FindXXHash.cmake

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Try to find XXHash
2+
# Once done this will define
3+
#
4+
# Read-Only variables:
5+
# XXHASH_FOUND - system has XXHash
6+
# XXHASH_INCLUDE_DIR - the XXHash include directory
7+
# XXHASH_LIBRARY - Link these to use XXHash
8+
9+
find_path(XXHASH_INCLUDE_DIR
10+
NAMES
11+
xxhash.h
12+
PATHS
13+
/usr/include
14+
/usr/local/include
15+
/opt/local/include
16+
/sw/include
17+
${CMAKE_INCLUDE_PATH}
18+
${CMAKE_INSTALL_PREFIX}/include
19+
)
20+
21+
find_library(XXHASH_LIBRARY
22+
NAMES
23+
xxhash
24+
libxxhash
25+
PATHS
26+
PATHS
27+
/usr/lib
28+
/usr/lib64
29+
/usr/local/lib
30+
/usr/local/lib64
31+
/opt/local/lib
32+
/sw/lib
33+
${CMAKE_LIBRARY_PATH}
34+
${CMAKE_INSTALL_PREFIX}/lib
35+
)
36+
37+
include(FindPackageHandleStandardArgs)
38+
find_package_handle_standard_args(XXHash FOUND_VAR XXHASH_FOUND REQUIRED_VARS XXHASH_INCLUDE_DIR XXHASH_LIBRARY)

src/hash_table.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <stdint.h>
2121
#include <stdlib.h>
2222
#include <string.h>
23+
#include <xxhash.h>
2324

2425
#include "compat.h"
2526
#include "dict.h"
@@ -29,30 +30,17 @@
2930
LIBYANG_API_DEF uint32_t
3031
lyht_hash_multi(uint32_t hash, const char *key_part, size_t len)
3132
{
32-
uint32_t i;
33-
3433
if (key_part && len) {
35-
for (i = 0; i < len; ++i) {
36-
hash += key_part[i];
37-
hash += (hash << 10);
38-
hash ^= (hash >> 6);
39-
}
40-
} else {
41-
hash += (hash << 3);
42-
hash ^= (hash >> 11);
43-
hash += (hash << 15);
34+
return XXH3_64bits_withSeed(key_part, len, hash);
4435
}
4536

46-
return hash;
37+
return XXH3_64bits_withSeed(NULL, 0, hash);
4738
}
4839

4940
LIBYANG_API_DEF uint32_t
5041
lyht_hash(const char *key, size_t len)
5142
{
52-
uint32_t hash;
53-
54-
hash = lyht_hash_multi(0, key, len);
55-
return lyht_hash_multi(hash, NULL, len);
43+
return XXH3_64bits(key, len);
5644
}
5745

5846
static LY_ERR

src/hash_table.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ LIBYANG_API_DECL uint32_t lyht_hash_multi(uint32_t hash, const char *key_part, s
4949
/**
5050
* @brief Compute hash from a string.
5151
*
52-
* Bob Jenkin's one-at-a-time hash
53-
* http://www.burtleburtle.net/bob/hash/doobs.html
54-
*
55-
* Spooky hash is faster, but it works only for little endian architectures.
52+
* Uses XXH3_64bits internally See: https://xxhash.com
5653
*
5754
* @param[in] key Key to hash.
5855
* @param[in] len Length of @p key.

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function(ly_add_utest)
3030
endif()
3131
endif()
3232

33+
target_link_libraries(${TEST_NAME} ${XXHASH_LIBRARY})
3334
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
3435
set_property(TEST ${TEST_NAME} APPEND PROPERTY ENVIRONMENT "MALLOC_CHECK_=3")
3536

tests/perf/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set(format_sources
66
add_executable(ly_perf ${CMAKE_CURRENT_SOURCE_DIR}/perf.c $<TARGET_OBJECTS:yangobj>)
77
set_target_properties(ly_perf PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")
88
target_link_libraries(ly_perf ${CMAKE_THREAD_LIBS_INIT} ${PCRE2_LIBRARIES} ${CMAKE_DL_LIBS})
9+
target_link_libraries(ly_perf ${XXHASH_LIBRARY})
910
if (NOT WIN32)
1011
target_link_libraries(ly_perf m)
1112
endif()

tests/style/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ endif()
88
# just compile
99
add_executable(cpp_compat cpp_compat.c $<TARGET_OBJECTS:yangobj>)
1010
target_include_directories(cpp_compat BEFORE PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
11-
target_link_libraries(cpp_compat ${CMAKE_THREAD_LIBS_INIT} ${PCRE2_LIBRARIES} ${CMAKE_DL_LIBS} m)
11+
target_link_libraries(cpp_compat ${CMAKE_THREAD_LIBS_INIT} ${PCRE2_LIBRARIES} ${CMAKE_DL_LIBS} m ${XXHASH_LIBRARY})
1212
target_compile_options(cpp_compat PUBLIC "-Werror=c++-compat")

0 commit comments

Comments
 (0)