Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C] Common hash functions. #1178

Merged
merged 7 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions aeron-client/src/main/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ endif ()
set(SOURCE
collections/aeron_array_to_ptr_hash_map.c
collections/aeron_bit_set.c
collections/aeron_hashing.c
collections/aeron_int64_counter_map.c
collections/aeron_int64_to_ptr_hash_map.c
collections/aeron_int64_to_tagged_ptr_hash_map.c
Expand Down Expand Up @@ -103,6 +104,7 @@ set(SOURCE
set(HEADERS
collections/aeron_array_to_ptr_hash_map.h
collections/aeron_bit_set.h
collections/aeron_hashing.h
collections/aeron_int64_counter_map.h
collections/aeron_int64_to_ptr_hash_map.h
collections/aeron_int64_to_tagged_ptr_hash_map.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "util/aeron_bitutil.h"
#include "util/aeron_strutil.h"
#include "util/aeron_error.h"
#include "collections/aeron_hashing.h"
#include "collections/aeron_map.h"
#include "aeron_alloc.h"

Expand Down Expand Up @@ -54,9 +55,9 @@ inline uint64_t aeron_array_hash(const uint8_t *arr, size_t length)
return aeron_fnv_64a_buf((uint8_t *)arr, length);
}

inline size_t aeron_array_to_ptr_hash_map_hash_key(uint64_t key, size_t mask)
inline size_t aeron_array_to_ptr_hash_map_hash_key(uint64_t key_hash_code, size_t mask)
{
return (size_t)((key * 31) & mask);
return aeron_hash(key_hash_code, mask);
}

inline bool aeron_array_to_ptr_hash_map_compare(
Expand Down
2 changes: 1 addition & 1 deletion aeron-client/src/main/c/collections/aeron_bit_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

#include "aeron_bit_set.h"
#include "collections/aeron_bit_set.h"

extern int aeron_bit_set_stack_alloc(
size_t bit_set_length, uint64_t *static_array, size_t static_array_len, aeron_bit_set_t *bit_set);
Expand Down
23 changes: 23 additions & 0 deletions aeron-client/src/main/c/collections/aeron_hashing.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2014-2021 Real Logic Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "collections/aeron_hashing.h"

extern uint32_t aeron_hash_code(uint64_t value);

extern size_t aeron_hash(uint64_t value, size_t mask);

extern size_t aeron_even_hash(uint64_t value, size_t mask);
47 changes: 47 additions & 0 deletions aeron-client/src/main/c/collections/aeron_hashing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2014-2021 Real Logic Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef AERON_HASHING_H
#define AERON_HASHING_H

#include <stddef.h>
#include <stdint.h>

inline uint32_t aeron_hash_code(uint64_t value)
{
uint64_t x = value;

x = (x ^ (x >> 30u)) * UINT64_C(0xbf58476d1ce4e5b9);
x = (x ^ (x >> 27u)) * UINT64_C(0x94d049bb133111eb);
x = x ^ (x >> 31u);

return (uint32_t)x ^ (uint32_t)(x >> 32u);
}

inline size_t aeron_hash(uint64_t value, size_t mask)
{
uint32_t hash = aeron_hash_code(value);
return (size_t)(hash & mask);
}

inline size_t aeron_even_hash(uint64_t value, size_t mask)
{
uint32_t hash = aeron_hash_code(value);
hash = (hash << 1u) - (hash << 8u);
return (size_t)(hash & mask);
}

#endif //AERON_HASHING_H
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

#include "aeron_int64_counter_map.h"
#include "collections/aeron_int64_counter_map.h"

extern size_t aeron_int64_counter_map_hash_key(int64_t key, size_t mask);

Expand Down
7 changes: 3 additions & 4 deletions aeron-client/src/main/c/collections/aeron_int64_counter_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "util/aeron_platform.h"
#include "util/aeron_bitutil.h"
#include "collections/aeron_hashing.h"
#include "collections/aeron_map.h"
#include "aeron_alloc.h"

Expand All @@ -38,9 +39,7 @@ aeron_int64_counter_map_t;

inline size_t aeron_int64_counter_map_hash_key(int64_t key, size_t mask)
{
uint64_t hash = ((uint64_t)key << UINT64_C(1)) - ((uint64_t)key << UINT64_C(8));

return (size_t)(hash & mask);
return aeron_even_hash((uint64_t)key, mask);
}

inline int aeron_int64_counter_map_init(
Expand Down Expand Up @@ -78,7 +77,6 @@ inline void aeron_int64_counter_map_delete(aeron_int64_counter_map_t *map)
inline int aeron_int64_counter_map_rehash(aeron_int64_counter_map_t *map, size_t new_entries_length)
{
size_t mask = new_entries_length - 1;
map->resize_threshold = (size_t)((new_entries_length / 2) * map->load_factor);

int64_t *tmp_entries;

Expand Down Expand Up @@ -114,6 +112,7 @@ inline int aeron_int64_counter_map_rehash(aeron_int64_counter_map_t *map, size_t

map->entries = tmp_entries;
map->entries_length = new_entries_length;
map->resize_threshold = (size_t)((new_entries_length / 2) * map->load_factor);

return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "util/aeron_platform.h"
#include "util/aeron_bitutil.h"
#include "util/aeron_error.h"
#include "collections/aeron_hashing.h"
#include "collections/aeron_map.h"
#include "aeron_alloc.h"

Expand All @@ -38,7 +39,7 @@ aeron_int64_to_ptr_hash_map_t;

inline size_t aeron_int64_to_ptr_hash_map_hash_key(int64_t key, size_t mask)
{
return (size_t)(((uint64_t)key * 31u) & mask);
return aeron_hash((uint64_t)key, mask);
}

inline int aeron_int64_to_ptr_hash_map_init(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "util/aeron_platform.h"
#include "util/aeron_bitutil.h"
#include "collections/aeron_hashing.h"
#include "collections/aeron_map.h"
#include "aeron_alloc.h"

Expand Down Expand Up @@ -50,7 +51,7 @@ aeron_int64_to_tagged_ptr_hash_map_t;

inline size_t aeron_int64_to_tagged_ptr_hash_map_hash_key(int64_t key, size_t mask)
{
return (size_t)(((uint64_t)key * 31u) & mask);
return aeron_hash((uint64_t)key, mask);
}

inline int aeron_int64_to_tagged_ptr_hash_map_init(
Expand Down
2 changes: 1 addition & 1 deletion aeron-client/src/main/c/collections/aeron_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#ifndef AERON_AERON_MAP_H
#define AERON_AERON_MAP_H

#define AERON_MAP_DEFAULT_LOAD_FACTOR (0.55f)
#define AERON_MAP_DEFAULT_LOAD_FACTOR (0.65f)

#include <stdint.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "util/aeron_bitutil.h"
#include "util/aeron_strutil.h"
#include "util/aeron_error.h"
#include "collections/aeron_hashing.h"
#include "collections/aeron_map.h"
#include "aeron_alloc.h"

Expand All @@ -46,9 +47,9 @@ typedef struct aeron_str_to_ptr_hash_map_stct
}
aeron_str_to_ptr_hash_map_t;

inline size_t aeron_str_to_ptr_hash_map_hash_key(uint64_t key, size_t mask)
inline size_t aeron_str_to_ptr_hash_map_hash_key(uint64_t key_hash_code, size_t mask)
{
return (size_t)((key * 31) & mask);
return aeron_hash(key_hash_code, mask);
}

inline bool aeron_str_to_ptr_hash_map_compare(
Expand Down
2 changes: 1 addition & 1 deletion aeron-client/src/main/c/util/aeron_bitutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

#include "aeron_bitutil.h"
#include "util/aeron_bitutil.h"

extern uint8_t *aeron_cache_line_align_buffer(uint8_t *buffer);
extern int aeron_number_of_trailing_zeroes(int32_t value);
Expand Down
6 changes: 3 additions & 3 deletions aeron-client/src/main/c/util/aeron_dlopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
#include <errno.h>
#include <inttypes.h>

#include "aeron_dlopen.h"
#include "aeron_error.h"
#include "aeron_strutil.h"
#include "util/aeron_dlopen.h"
#include "util/aeron_error.h"
#include "util/aeron_strutil.h"
#include "aeron_alloc.h"

#if defined(AERON_COMPILER_GCC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ TEST_F(Int64CounterMapTest, shouldGrowWhenThresholdExceeded)
for (int64_t i = 0; i < 16; i++)
{
EXPECT_EQ(aeron_int64_counter_map_put(&m_map, i, value, nullptr), 0);
EXPECT_EQ(aeron_int64_counter_map_get(&m_map, i), value);
}

EXPECT_EQ(m_map.resize_threshold, 16u);
Expand Down
2 changes: 2 additions & 0 deletions aeron-driver/src/main/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ endif ()
SET(C_CLIENT_SOURCE
${AERON_C_CLIENT_SOURCE_PATH}/collections/aeron_array_to_ptr_hash_map.c
${AERON_C_CLIENT_SOURCE_PATH}/collections/aeron_bit_set.c
${AERON_C_CLIENT_SOURCE_PATH}/collections/aeron_hashing.c
${AERON_C_CLIENT_SOURCE_PATH}/collections/aeron_int64_counter_map.c
${AERON_C_CLIENT_SOURCE_PATH}/collections/aeron_int64_to_ptr_hash_map.c
${AERON_C_CLIENT_SOURCE_PATH}/collections/aeron_int64_to_tagged_ptr_hash_map.c
Expand Down Expand Up @@ -177,6 +178,7 @@ SET(C_CLIENT_SOURCE
set(C_CLIENT_HEADERS
${AERON_C_CLIENT_SOURCE_PATH}/collections/aeron_array_to_ptr_hash_map.h
${AERON_C_CLIENT_SOURCE_PATH}/collections/aeron_bit_set.h
${AERON_C_CLIENT_SOURCE_PATH}/collections/aeron_hashing.h
${AERON_C_CLIENT_SOURCE_PATH}/collections/aeron_int64_counter_map.h
${AERON_C_CLIENT_SOURCE_PATH}/collections/aeron_int64_to_ptr_hash_map.h
${AERON_C_CLIENT_SOURCE_PATH}/collections/aeron_int64_to_tagged_ptr_hash_map.h
Expand Down