[libc-commits] [PATCH] D136228: [libc] Implement `hsearch` related facilities.

Schrodinger ZHU Yifan via Phabricator via libc-commits libc-commits at lists.llvm.org
Tue Oct 18 22:35:36 PDT 2022


SchrodingerZhu created this revision.
Herald added subscribers: libc-commits, ecnelises, tschuett, kristof.beyls.
Herald added projects: libc-project, All.
SchrodingerZhu requested review of this revision.

This patch add `hsearch(_r)`, `hdestroy(_r)`, `hcreate(_r)` to libc. In the same time, `ACTION`, `ENTRY` and `struct hsearch_data`
are introduced via the newly added header `search.h`.

The hashtable backend is swisstable. In short, this table is an open-addressing table but it looks up multiple 2nd-level hash in a 
large machine word per probe. This enables faster lookup speed, especially for those platforms with SIMD registers. This implementation
is a combined effort of abseil's version and hashbrown's version. The structure mainly follows the latter one while the recent optimization
for aarch64 from abseil is adopt.

The 2nd-level hash is generated from the highest 7-bit of the hashword, thus the hash function is expected to encode the information using the
whole word and it should have a good avalanche effect. To fulfill the condition, a rewrite of wyhash in c++ is introduced.

By now, the random seed of the hashtable is only utilizes limited random source. This is because using system entropy pool can introduce other errors and
getrandom is not portable while `hsearch` is within the POSIX standard.

MUSL and Glibc seems to disagree with the ERRNO setting behavior. POSIX.1-2001 only states possible error for non-reentrant version. This implementation currently choose to follow the glibc's behavior.

The swisstable header contains more features than required ones. One reason is that the table can be used for future extension. The other is that glibc and musl
also disagrees on whether the table can be resized. The swisstable is designed to provide template parameters to control whether DELETE or RESIZE are supported, and using constexpr branches to optimize the situtations when some features are disabled.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136228

Files:
  libc/config/linux/x86_64/entrypoints.txt
  libc/config/linux/x86_64/headers.txt
  libc/include/CMakeLists.txt
  libc/include/llvm-libc-types/CMakeLists.txt
  libc/include/llvm-libc-types/action.h
  libc/include/llvm-libc-types/entry.h
  libc/include/llvm-libc-types/struct_hsearch_data.h
  libc/include/search.h.def
  libc/spec/posix.td
  libc/src/CMakeLists.txt
  libc/src/__support/CMakeLists.txt
  libc/src/__support/builtin_wrappers.h
  libc/src/__support/swisstable.h
  libc/src/__support/swisstable/asimd.h
  libc/src/__support/swisstable/common.h
  libc/src/__support/swisstable/dispatch.h
  libc/src/__support/swisstable/generic.h
  libc/src/__support/swisstable/sse2.h
  libc/src/__support/wyhash.h
  libc/src/search/CMakeLists.txt
  libc/src/search/hashtable/CMakeLists.txt
  libc/src/search/hashtable/global.cpp
  libc/src/search/hashtable/global.h
  libc/src/search/hashtable/search_impl.h
  libc/src/search/hashtable/utils.h
  libc/src/search/hcreate.cpp
  libc/src/search/hcreate.h
  libc/src/search/hcreate_r.cpp
  libc/src/search/hcreate_r.h
  libc/src/search/hdestroy.cpp
  libc/src/search/hdestroy.h
  libc/src/search/hdestroy_r.cpp
  libc/src/search/hdestroy_r.h
  libc/src/search/hsearch.cpp
  libc/src/search/hsearch.h
  libc/src/search/hsearch_r.cpp
  libc/src/search/hsearch_r.h
  libc/test/src/CMakeLists.txt
  libc/test/src/__support/CMakeLists.txt
  libc/test/src/__support/swisstable/CMakeLists.txt
  libc/test/src/__support/swisstable/bitmask_test.cpp
  libc/test/src/__support/swisstable/group_test.cpp
  libc/test/src/__support/swisstable/hashtable_test.cpp
  libc/test/src/__support/swisstable/safe_mem_size_test.cpp
  libc/test/src/__support/swisstable/test_utils.h
  libc/test/src/__support/wyhash_test.cpp
  libc/test/src/search/CMakeLists.txt
  libc/test/src/search/hsearch_test.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136228.468785.patch
Type: text/x-patch
Size: 99545 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20221019/1521dcb7/attachment-0001.bin>


More information about the libc-commits mailing list