-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reland of remove uses of folly::hash::fnv32_buf (#40233)
Summary: Pull Request resolved: #40233 changelog: [internal] This is a reland of D49355595 and D49358327. the problem was using two different hashing functions in place where they must be the same. Reviewed By: javache Differential Revision: D50020135 fbshipit-source-id: 1cec6bc385077d371b024a0fb5d9c64ba1f6269c
- Loading branch information
1 parent
6a2c245
commit 67384cf
Showing
5 changed files
with
68 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#pragma once | ||
|
||
namespace facebook::react { | ||
|
||
/** | ||
* FNV-1a hash function implementation. | ||
* Implemented as described in http://www.isthe.com/chongo/tech/comp/fnv/. | ||
* | ||
* Please use std::hash if possible. `fnv1a` should only be used in cases | ||
* when std::hash does not provide the needed functionality. For example, | ||
* constexpr. | ||
*/ | ||
constexpr uint32_t fnv1a(std::string_view string) noexcept { | ||
constexpr uint32_t offset_basis = 2166136261; | ||
|
||
uint32_t hash = offset_basis; | ||
|
||
for (auto const& c : string) { | ||
hash ^= static_cast<int8_t>(c); | ||
// Using shifts and adds instead of multiplication with a prime number. | ||
// This is faster when compiled with optimizations. | ||
hash += | ||
(hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24); | ||
} | ||
|
||
return hash; | ||
} | ||
|
||
} // namespace facebook::react |
24 changes: 24 additions & 0 deletions
24
packages/react-native/ReactCommon/react/utils/tests/fnv1aTests.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#include <gtest/gtest.h> | ||
#include <react/utils/fnv1a.h> | ||
|
||
namespace facebook::react { | ||
|
||
TEST(fnv1aTests, testBasicHashing) { | ||
EXPECT_EQ(fnv1a("react"), fnv1a("react")); | ||
|
||
EXPECT_NE(fnv1a("react"), fnv1a("tceat")); | ||
|
||
auto string1 = "case 1"; | ||
auto string2 = "different string"; | ||
EXPECT_EQ(fnv1a(string1), fnv1a(string1)); | ||
EXPECT_NE(fnv1a(string1), fnv1a(string2)); | ||
} | ||
|
||
} // namespace facebook::react |