Skip to content

Commit 953344a

Browse files
committed
hash table REFACTOR improve xxhash support
1 parent da81ee5 commit 953344a

File tree

6 files changed

+20
-26
lines changed

6 files changed

+20
-26
lines changed

CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ else()
242242
option(ENABLE_TESTS "Build tests" OFF)
243243
option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF)
244244
endif()
245-
option(USE_XXHASH "Use xxhash if available" ON)
246245
option(ENABLE_PERF_TESTS "Build performance tests" OFF)
247246
option(ENABLE_COVERAGE "Build code coverage report from tests" OFF)
248247
option(ENABLE_FUZZ_TARGETS "Build target programs suitable for fuzzing with AFL" OFF)
@@ -414,12 +413,13 @@ target_link_libraries(yang ${PCRE2_LIBRARIES})
414413

415414
# XXHash include and library
416415
find_package(XXHash)
417-
if (XXHASH_FOUND AND USE_XXHASH)
418-
add_definitions(-DUSE_XXHASH)
416+
if(XXHASH_FOUND)
417+
add_definitions(-DLY_XXHASH_SUPPORT)
419418
include_directories(${XXHASH_INCLUDE_DIR})
420419
target_link_libraries(yang ${XXHASH_LIBRARY})
420+
message(STATUS "Hash algorithm: xxhash")
421421
else()
422-
set (USE_XXHASH OFF)
422+
message(STATUS "Hash algorithm: internal Jenkin's one-at-a-time")
423423
endif()
424424

425425
# generated header list

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ the `distro` directory.
6161

6262
#### Optional
6363

64+
* xxhash (for faster hashing)
6465
* doxygen (for generating documentation)
6566
* cmocka >= 1.0.1 (for [tests](#Tests))
6667
* valgrind (for enhanced testing)

src/hash_table.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @author Michal Vasko <[email protected]>
55
* @brief libyang generic hash table implementation
66
*
7-
* Copyright (c) 2015 - 2023 CESNET, z.s.p.o.
7+
* Copyright (c) 2015 - 2025 CESNET, z.s.p.o.
88
*
99
* This source code is licensed under BSD 3-Clause License (the "License").
1010
* You may not use this file except in compliance with the License.
@@ -26,30 +26,20 @@
2626
#include "log.h"
2727
#include "ly_common.h"
2828

29-
#ifdef USE_XXHASH
30-
#include <xxhash.h>
29+
#ifdef LY_XXHASH_SUPPORT
30+
# include <xxhash.h>
31+
#endif
3132

3233
LIBYANG_API_DEF uint32_t
3334
lyht_hash_multi(uint32_t hash, const char *key_part, size_t len)
3435
{
36+
#ifdef LY_XXHASH_SUPPORT
3537
if (key_part && len) {
3638
return XXH3_64bits_withSeed(key_part, len, hash);
3739
}
3840

3941
return XXH3_64bits_withSeed(NULL, 0, hash);
40-
}
41-
42-
LIBYANG_API_DEF uint32_t
43-
lyht_hash(const char *key, size_t len)
44-
{
45-
return XXH3_64bits(key, len);
46-
}
47-
4842
#else
49-
50-
LIBYANG_API_DEF uint32_t
51-
lyht_hash_multi(uint32_t hash, const char *key_part, size_t len)
52-
{
5343
uint32_t i;
5444

5545
if (key_part && len) {
@@ -65,18 +55,21 @@ lyht_hash_multi(uint32_t hash, const char *key_part, size_t len)
6555
}
6656

6757
return hash;
58+
#endif
6859
}
6960

7061
LIBYANG_API_DEF uint32_t
7162
lyht_hash(const char *key, size_t len)
7263
{
64+
#ifdef LY_XXHASH_SUPPORT
65+
return XXH3_64bits(key, len);
66+
#else
7367
uint32_t hash;
7468

7569
hash = lyht_hash_multi(0, key, len);
7670
return lyht_hash_multi(hash, NULL, len);
77-
}
78-
7971
#endif
72+
}
8073

8174
static LY_ERR
8275
lyht_init_hlists_and_records(struct ly_ht *ht)

tests/CMakeLists.txt

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

33-
if (USE_XXHASH)
33+
if (XXHASH_FOUND)
3434
target_link_libraries(${TEST_NAME} ${XXHASH_LIBRARY})
3535
endif()
3636
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})

tests/perf/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ 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-
if (USE_XXHASH)
9+
if(XXHASH_FOUND)
1010
target_link_libraries(ly_perf ${XXHASH_LIBRARY})
1111
endif()
1212

13-
if (NOT WIN32)
13+
if(NOT WIN32)
1414
target_link_libraries(ly_perf m)
1515
endif()
1616

tests/style/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
add_test(NAME headers
22
COMMAND ${CMAKE_SOURCE_DIR}/compat/check_includes.sh ${CMAKE_SOURCE_DIR}/src/ ${CMAKE_SOURCE_DIR}/tools/lint/ ${CMAKE_SOURCE_DIR}/tools/re/)
33

4-
if (${SOURCE_FORMAT_ENABLED})
4+
if(${SOURCE_FORMAT_ENABLED})
55
add_test(NAME format WORKING_DIRECTORY ${CMAKE_BINARY_DIR} COMMAND cmake --build ${CMAKE_BINARY_DIR} --target format-check)
66
endif()
77

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})
1111
target_link_libraries(cpp_compat ${CMAKE_THREAD_LIBS_INIT} ${PCRE2_LIBRARIES} ${CMAKE_DL_LIBS} m)
12-
if (USE_XXHASH)
12+
if(XXHASH_FOUND)
1313
target_link_libraries(cpp_compat ${XXHASH_LIBRARY})
1414
endif()
1515

0 commit comments

Comments
 (0)