Skip to content

Commit

Permalink
tag/Pool: move code from calc_hash() to util/djb_hash.cxx
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Sep 11, 2023
1 parent f578b06 commit c391ada
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/tag/Pool.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
#include "Pool.hxx"
#include "Item.hxx"
#include "util/Cast.hxx"
#include "util/djb_hash.hxx"
#include "util/IntrusiveList.hxx"
#include "util/SpanCast.hxx"
#include "util/VarSize.hxx"

#include <array>
#include <cassert>
#include <cstdint>
#include <limits>

#include <string.h>
#include <stdlib.h>

Mutex tag_pool_lock;

struct TagPoolItem {
Expand Down Expand Up @@ -53,12 +52,7 @@ static std::array<IntrusiveList<TagPoolItem,
static inline std::size_t
calc_hash(TagType type, std::string_view p) noexcept
{
std::size_t hash = 5381;

for (auto ch : p)
hash = (hash << 5) + hash + ch;

return hash ^ type;
return djb_hash(AsBytes(p)) ^ type;
}

static constexpr TagPoolItem *
Expand Down
40 changes: 40 additions & 0 deletions src/util/djb_hash.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: BSD-2-Clause
// Copyright CM4all GmbH
// author: Max Kellermann <[email protected]>

#include "djb_hash.hxx"

#include <cassert>

[[gnu::always_inline]] [[gnu::hot]]
static constexpr std::size_t
djb_hash_update(std::size_t hash, std::byte b) noexcept
{
return (hash * 33) ^ static_cast<std::size_t>(b);
}

[[gnu::hot]]
std::size_t
djb_hash(std::span<const std::byte> src, std::size_t init) noexcept
{
std::size_t hash = init;

for (const auto i : src)
hash = djb_hash_update(hash, i);

return hash;
}

[[gnu::hot]]
std::size_t
djb_hash_string(const char *p, std::size_t init) noexcept
{
assert(p != nullptr);

std::size_t hash = init;

while (*p != 0)
hash = djb_hash_update(hash, static_cast<std::byte>(*p++));

return hash;
}
25 changes: 25 additions & 0 deletions src/util/djb_hash.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: BSD-2-Clause
// Copyright CM4all GmbH
// author: Max Kellermann <[email protected]>

/*
* Implementation of D. J. Bernstein's cdb hash function.
* http://cr.yp.to/cdb/cdb.txt
*/

#pragma once

#include <cstddef>
#include <span>

static constexpr std::size_t DJB_HASH_INIT = 5381;

[[gnu::pure]]
std::size_t
djb_hash(std::span<const std::byte> src,
std::size_t init=DJB_HASH_INIT) noexcept;

[[gnu::pure]]
std::size_t
djb_hash_string(const char *p,
std::size_t init=DJB_HASH_INIT) noexcept;
1 change: 1 addition & 0 deletions src/util/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ util = static_library(
'ByteReverse.cxx',
'format.c',
'BitReverse.cxx',
'djb_hash.cxx',
'Serial.cxx',
include_directories: inc,
)
Expand Down

0 comments on commit c391ada

Please sign in to comment.