Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
Actor Framework
Commits
67f273ac
Commit
67f273ac
authored
Apr 07, 2020
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make FNV hash available as inspector
parent
4d53aa0d
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
220 additions
and
26 deletions
+220
-26
libcaf_core/CMakeLists.txt
libcaf_core/CMakeLists.txt
+1
-1
libcaf_core/caf/all.hpp
libcaf_core/caf/all.hpp
+1
-0
libcaf_core/caf/fwd.hpp
libcaf_core/caf/fwd.hpp
+9
-0
libcaf_core/caf/hash/fnv.hpp
libcaf_core/caf/hash/fnv.hpp
+122
-0
libcaf_core/caf/read_inspector.hpp
libcaf_core/caf/read_inspector.hpp
+9
-3
libcaf_core/src/detail/type_id_list_builder.cpp
libcaf_core/src/detail/type_id_list_builder.cpp
+4
-3
libcaf_core/src/ipv4_endpoint.cpp
libcaf_core/src/ipv4_endpoint.cpp
+2
-3
libcaf_core/src/ipv6_endpoint.cpp
libcaf_core/src/ipv6_endpoint.cpp
+2
-3
libcaf_core/src/uri.cpp
libcaf_core/src/uri.cpp
+2
-2
libcaf_core/test/hash/fnv.cpp
libcaf_core/test/hash/fnv.cpp
+64
-0
libcaf_io/src/io/network/ip_endpoint.cpp
libcaf_io/src/io/network/ip_endpoint.cpp
+4
-11
No files found.
libcaf_core/CMakeLists.txt
View file @
67f273ac
...
...
@@ -59,7 +59,6 @@ set(CAF_CORE_SOURCES
src/detail/behavior_impl.cpp
src/detail/behavior_stack.cpp
src/detail/blocking_behavior.cpp
src/detail/fnv_hash.cpp
src/detail/get_mac_addresses.cpp
src/detail/get_process_id.cpp
src/detail/get_root_uuid.cpp
...
...
@@ -215,6 +214,7 @@ set(CAF_CORE_TEST_SOURCES
test/function_view.cpp
test/fused_downstream_manager.cpp
test/handles.cpp
test/hash/fnv.cpp
test/inspector.cpp
test/intrusive/drr_cached_queue.cpp
test/intrusive/drr_queue.cpp
...
...
libcaf_core/caf/all.hpp
View file @
67f273ac
...
...
@@ -69,6 +69,7 @@
#include "caf/function_view.hpp"
#include "caf/fused_downstream_manager.hpp"
#include "caf/group.hpp"
#include "caf/hash/fnv.hpp"
#include "caf/index_mapping.hpp"
#include "caf/init_global_meta_objects.hpp"
#include "caf/local_actor.hpp"
...
...
libcaf_core/caf/fwd.hpp
View file @
67f273ac
...
...
@@ -203,6 +203,15 @@ using type_id_t = uint16_t;
/// @relates actor_system_config
CAF_CORE_EXPORT
const
settings
&
content
(
const
actor_system_config
&
);
// -- hash inspectors ----------------------------------------------------------
namespace
hash
{
template
<
class
>
class
fnv
;
}
// namespace hash
// -- intrusive containers -----------------------------------------------------
namespace
intrusive
{
...
...
libcaf_core/caf/hash/fnv.hpp
0 → 100644
View file @
67f273ac
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2020 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#pragma once
#include <cstdint>
#include <type_traits>
#include "caf/detail/ieee_754.hpp"
#include "caf/read_inspector.hpp"
#include "caf/span.hpp"
#include "caf/string_view.hpp"
namespace
caf
::
hash
{
/// Non-cryptographic hash algorithm (variant 1a) named after Glenn Fowler,
/// Landon Curt Noll, and Kiem-Phong Vo.
///
/// For more details regarding the public domain algorithm, see:
/// - https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// - http://www.isthe.com/chongo/tech/comp/fnv/index.html
///
/// @tparam T One of `uint32_t`, `uint64_t`, or `size_t`.
template
<
class
T
>
class
fnv
:
public
read_inspector
<
fnv
<
T
>>
{
public:
static_assert
(
sizeof
(
T
)
==
4
||
sizeof
(
T
)
==
8
);
using
result_type
=
void
;
constexpr
fnv
()
noexcept
:
value
(
init
())
{
// nop
}
template
<
class
Integral
>
std
::
enable_if_t
<
std
::
is_integral
<
Integral
>::
value
>
apply
(
Integral
x
)
noexcept
{
auto
begin
=
reinterpret_cast
<
const
uint8_t
*>
(
&
x
);
append
(
begin
,
begin
+
sizeof
(
Integral
));
}
void
apply
(
bool
x
)
noexcept
{
auto
tmp
=
static_cast
<
uint8_t
>
(
x
);
apply
(
tmp
);
}
void
apply
(
float
x
)
noexcept
{
apply
(
detail
::
pack754
(
x
));
}
void
apply
(
double
x
)
noexcept
{
apply
(
detail
::
pack754
(
x
));
}
void
apply
(
string_view
x
)
noexcept
{
auto
begin
=
reinterpret_cast
<
const
uint8_t
*>
(
x
.
data
());
append
(
begin
,
begin
+
x
.
size
());
}
void
apply
(
span
<
const
byte
>
x
)
noexcept
{
auto
begin
=
reinterpret_cast
<
const
uint8_t
*>
(
x
.
data
());
append
(
begin
,
begin
+
x
.
size
());
}
template
<
class
Enum
>
std
::
enable_if_t
<
std
::
is_enum
<
Enum
>::
value
>
apply
(
Enum
x
)
noexcept
{
return
apply
(
static_cast
<
std
::
underlying_type_t
<
Enum
>>
(
x
));
}
void
begin_sequence
(
size_t
)
{
// nop
}
void
end_sequence
()
{
// nop
}
/// Convenience function for computing an FNV1a hash value for given
/// arguments in one shot.
template
<
class
...
Ts
>
static
T
compute
(
Ts
&&
...
xs
)
{
fnv
f
;
f
(
std
::
forward
<
Ts
>
(
xs
)...);
return
f
.
value
;
}
T
value
;
private:
static
constexpr
T
init
()
{
if
constexpr
(
sizeof
(
T
)
==
4
)
return
0x811C9DC5u
;
else
return
0xCBF29CE484222325ull
;
}
void
append
(
const
uint8_t
*
begin
,
const
uint8_t
*
end
)
{
if
constexpr
(
sizeof
(
T
)
==
4
)
while
(
begin
!=
end
)
value
=
(
*
begin
++
^
value
)
*
0x01000193u
;
else
while
(
begin
!=
end
)
value
=
(
*
begin
++
^
value
)
*
1099511628211ull
;
}
};
}
// namespace caf::hash
libcaf_core/caf/read_inspector.hpp
View file @
67f273ac
...
...
@@ -51,9 +51,15 @@ public:
template
<
class
...
Ts
>
[[
nodiscard
]]
auto
operator
()(
Ts
&&
...
xs
)
{
typename
Subtype
::
result_type
result
;
static_cast
<
void
>
((
try_apply
(
result
,
xs
)
&&
...));
return
result
;
using
result_type
=
typename
Subtype
::
result_type
;
if
constexpr
(
std
::
is_same
<
result_type
,
void
>::
value
)
{
auto
dummy
=
unit
;
static_cast
<
void
>
((
try_apply
(
dummy
,
xs
)
&&
...));
}
else
{
typename
Subtype
::
result_type
result
;
static_cast
<
void
>
((
try_apply
(
result
,
xs
)
&&
...));
return
result
;
}
}
private:
...
...
libcaf_core/src/detail/type_id_list_builder.cpp
View file @
67f273ac
...
...
@@ -18,11 +18,12 @@
#include "caf/detail/type_id_list_builder.hpp"
#include <cstdint>
#include <mutex>
#include <unordered_set>
#include "caf/config.hpp"
#include "caf/
detail/fnv_hash
.hpp"
#include "caf/
hash/fnv
.hpp"
#include "caf/type_id_list.hpp"
namespace
caf
::
detail
{
...
...
@@ -30,9 +31,9 @@ namespace {
struct
dyn_type_id_list
{
dyn_type_id_list
(
type_id_t
*
storage
)
:
storage
(
storage
)
{
auto
first
=
reinterpret_cast
<
unsigned
char
*>
(
storage
);
auto
first
=
reinterpret_cast
<
const
uint8_t
*>
(
storage
);
auto
last
=
first
+
((
storage
[
0
]
+
1
)
*
sizeof
(
type_id_t
));
hash
=
fnv_hash
(
first
,
last
);
hash
=
caf
::
hash
::
fnv
<
size_t
>::
compute
(
make_span
(
first
,
last
)
);
}
dyn_type_id_list
(
dyn_type_id_list
&&
other
)
...
...
libcaf_core/src/ipv4_endpoint.cpp
View file @
67f273ac
...
...
@@ -18,7 +18,7 @@
#include "caf/ipv4_endpoint.hpp"
#include "caf/
detail/fnv_hash
.hpp"
#include "caf/
hash/fnv
.hpp"
namespace
caf
{
...
...
@@ -28,8 +28,7 @@ ipv4_endpoint::ipv4_endpoint(ipv4_address address, uint16_t port)
}
size_t
ipv4_endpoint
::
hash_code
()
const
noexcept
{
auto
result
=
detail
::
fnv_hash
(
address_
.
data
());
return
detail
::
fnv_hash_append
(
result
,
port_
);
return
hash
::
fnv
<
size_t
>::
compute
(
address_
,
port_
);
}
long
ipv4_endpoint
::
compare
(
ipv4_endpoint
x
)
const
noexcept
{
...
...
libcaf_core/src/ipv6_endpoint.cpp
View file @
67f273ac
...
...
@@ -18,7 +18,7 @@
#include "caf/ipv6_endpoint.hpp"
#include "caf/
detail/fnv_hash
.hpp"
#include "caf/
hash/fnv
.hpp"
#include "caf/ipv4_address.hpp"
#include "caf/ipv4_endpoint.hpp"
...
...
@@ -35,8 +35,7 @@ ipv6_endpoint::ipv6_endpoint(ipv4_address address, uint16_t port)
}
size_t
ipv6_endpoint
::
hash_code
()
const
noexcept
{
auto
result
=
detail
::
fnv_hash
(
address_
.
data
());
return
detail
::
fnv_hash_append
(
result
,
port_
);
return
hash
::
fnv
<
size_t
>::
compute
(
address_
,
port_
);
}
long
ipv6_endpoint
::
compare
(
ipv6_endpoint
x
)
const
noexcept
{
...
...
libcaf_core/src/uri.cpp
View file @
67f273ac
...
...
@@ -22,13 +22,13 @@
#include "caf/binary_serializer.hpp"
#include "caf/deserializer.hpp"
#include "caf/detail/append_percent_encoded.hpp"
#include "caf/detail/fnv_hash.hpp"
#include "caf/detail/overload.hpp"
#include "caf/detail/parse.hpp"
#include "caf/detail/parser/read_uri.hpp"
#include "caf/detail/uri_impl.hpp"
#include "caf/error.hpp"
#include "caf/expected.hpp"
#include "caf/hash/fnv.hpp"
#include "caf/make_counted.hpp"
#include "caf/optional.hpp"
#include "caf/serializer.hpp"
...
...
@@ -72,7 +72,7 @@ string_view uri::fragment() const noexcept {
}
size_t
uri
::
hash_code
()
const
noexcept
{
return
detail
::
fnv_hash
(
str
());
return
hash
::
fnv
<
size_t
>::
compute
(
str
());
}
optional
<
uri
>
uri
::
authority_only
()
const
{
...
...
libcaf_core/
caf/detail/fnv_hash.h
pp
→
libcaf_core/
test/hash/fnv.c
pp
View file @
67f273ac
...
...
@@ -5,7 +5,7 @@
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-20
19
Dominik Charousset *
* Copyright 2011-20
20
Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
...
...
@@ -16,57 +16,49 @@
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#
pragma once
#
define CAF_SUITE hash.fnv
#include <cstdint>
#include <type_traits>
#include "caf/hash/fnv.hpp"
#include "caf/detail/core_export.hpp"
#include "caf/detail/type_traits.hpp"
#include "caf/test/dsl.hpp"
namespace
caf
::
detail
{
#include <string>
/// Non-cryptographic hash function named after Glenn Fowler, Landon Curt Noll,
/// and Kiem-Phong Vo.
///
/// See:
/// - https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
/// - http://www.isthe.com/chongo/tech/comp/fnv/index.html
CAF_CORE_EXPORT
size_t
fnv_hash
(
const
unsigned
char
*
first
,
const
unsigned
char
*
last
);
using
namespace
caf
;
CAF_CORE_EXPORT
size_t
fnv_hash_append
(
size_t
intermediate
,
const
unsigned
char
*
first
,
const
unsigned
char
*
last
);
using
namespace
std
::
string_literals
;
template
<
class
T
>
enable_if_t
<
has_data_member
<
T
>::
value
,
size_t
>
fnv_hash
(
const
T
&
x
)
{
auto
ptr
=
x
.
data
();
auto
first
=
reinterpret_cast
<
const
uint8_t
*>
(
ptr
);
auto
last
=
first
+
(
sizeof
(
decay_t
<
decltype
(
*
ptr
)
>
)
*
x
.
size
());
return
fnv_hash
(
first
,
last
);
template
<
class
...
Ts
>
auto
fnv32_hash
(
Ts
&&
...
xs
)
{
return
hash
::
fnv
<
uint32_t
>::
compute
(
std
::
forward
<
Ts
>
(
xs
)...);
}
template
<
class
T
>
enable_if_t
<
has_data_member
<
T
>::
value
,
size_t
>
fnv_hash_append
(
size_t
intermediate
,
const
T
&
x
)
{
auto
ptr
=
x
.
data
();
auto
first
=
reinterpret_cast
<
const
uint8_t
*>
(
ptr
);
auto
last
=
first
+
(
sizeof
(
decay_t
<
decltype
(
*
ptr
)
>
)
*
x
.
size
());
return
fnv_hash_append
(
intermediate
,
first
,
last
);
template
<
class
...
Ts
>
auto
fnv64_hash
(
Ts
&&
...
xs
)
{
return
hash
::
fnv
<
uint64_t
>::
compute
(
std
::
forward
<
Ts
>
(
xs
)...);
}
template
<
class
T
>
enable_if_t
<
std
::
is_integral
<
T
>::
value
,
size_t
>
fnv_hash
(
const
T
&
x
)
{
auto
first
=
reinterpret_cast
<
const
uint8_t
*>
(
&
x
);
return
fnv_hash
(
first
,
first
+
sizeof
(
T
));
CAF_TEST
(
FNV
hashes
build
incrementally
)
{
hash
::
fnv
<
uint32_t
>
f
;
CAF_CHECK_EQUAL
(
f
.
value
,
0x811C9DC5u
);
f
(
'a'
);
CAF_CHECK_EQUAL
(
f
.
value
,
0xE40C292Cu
);
f
(
'b'
);
CAF_CHECK_EQUAL
(
f
.
value
,
0x4D2505CAu
);
f
(
'c'
);
CAF_CHECK_EQUAL
(
f
.
value
,
0x1A47E90Bu
);
f
(
'd'
);
CAF_CHECK_EQUAL
(
f
.
value
,
0xCE3479BDu
);
}
template
<
class
T
>
enable_if_t
<
std
::
is_integral
<
T
>::
value
,
size_t
>
fnv_hash_append
(
size_t
interim
,
const
T
&
x
)
{
auto
first
=
reinterpret_cast
<
const
uint8_t
*>
(
&
x
);
return
fnv_hash_append
(
interim
,
first
,
first
+
sizeof
(
T
));
CAF_TEST
(
FNV
supports
uint32
hashing
)
{
CAF_CHECK_EQUAL
(
fnv32_hash
(),
0x811C9DC5u
);
CAF_CHECK_EQUAL
(
fnv32_hash
(
"abcd"
s
),
0xCE3479BDu
);
CAF_CHECK_EQUAL
(
fnv32_hash
(
"C++ Actor Framework"
s
),
0x2FF91FE5u
);
}
}
// namespace caf::detail
CAF_TEST
(
FNV
supports
uint64
hashing
)
{
CAF_CHECK_EQUAL
(
fnv64_hash
(),
0xCBF29CE484222325ull
);
CAF_CHECK_EQUAL
(
fnv64_hash
(
"abcd"
s
),
0xFC179F83EE0724DDull
);
CAF_CHECK_EQUAL
(
fnv64_hash
(
"C++ Actor Framework"
s
),
0xA229A760C3AF69C5ull
);
}
libcaf_io/src/io/network/ip_endpoint.cpp
View file @
67f273ac
...
...
@@ -18,7 +18,7 @@
#include "caf/io/network/ip_endpoint.hpp"
#include "caf/
detail/fnv_hash
.hpp"
#include "caf/
hash/fnv
.hpp"
#include "caf/io/network/native_socket.hpp"
#include "caf/logger.hpp"
#include "caf/sec.hpp"
...
...
@@ -44,9 +44,6 @@
using
sa_family_t
=
short
;
#endif
using
caf
::
detail
::
fnv_hash
;
using
caf
::
detail
::
fnv_hash_append
;
namespace
caf
::
io
::
network
{
struct
ip_endpoint
::
impl
{
...
...
@@ -113,16 +110,12 @@ size_t ep_hash::operator()(const sockaddr& sa) const noexcept {
}
size_t
ep_hash
::
hash
(
const
sockaddr_in
*
sa
)
const
noexcept
{
auto
result
=
fnv_hash
(
sa
->
sin_addr
.
s_addr
);
result
=
fnv_hash_append
(
result
,
sa
->
sin_port
);
return
result
;
return
hash
::
fnv
<
size_t
>::
compute
(
sa
->
sin_addr
.
s_addr
,
sa
->
sin_port
);
}
size_t
ep_hash
::
hash
(
const
sockaddr_in6
*
sa
)
const
noexcept
{
auto
&
addr
=
sa
->
sin6_addr
.
s6_addr
;
auto
result
=
fnv_hash
(
addr
,
addr
+
16
);
result
=
fnv_hash_append
(
result
,
sa
->
sin6_port
);
return
result
;
return
hash
::
fnv
<
size_t
>::
compute
((
make_span
(
sa
->
sin6_addr
.
s6_addr
)),
sa
->
sin6_port
);
}
bool
operator
==
(
const
ip_endpoint
&
lhs
,
const
ip_endpoint
&
rhs
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment