Go to file
openharmony_ci 3d7b353b5e
!3 告警与遗留问题文件补充
Merge pull request !3 from peizhe/master
2023-05-09 11:13:47 +00:00
.gitignore Import from 1fda09bf0c/src/fnv.rs 2015-04-06 20:43:00 +02:00
.travis.yml Run tests with std feature disabled in CI 2020-01-23 18:54:50 +01:00
BUILD.gn Add GN Build Files and Custom Modifications 2023-04-18 18:10:01 +08:00
Cargo.toml Bump to 1.0.7 2020-05-14 09:06:23 -07:00
lib.rs Add support for no_std compilation through an "std" feature 2019-12-03 21:08:21 +01:00
LICENSE-APACHE Create Apache license 2017-10-25 00:37:40 -04:00
LICENSE-MIT Create MIT license 2017-10-25 00:36:58 -04:00
OAT.xml 告警与遗留问题文件补充 2023-05-08 15:42:19 +08:00
README.md Replace http with https 2018-11-10 16:50:39 +01:00
README.OpenSource CI OAT 告警清零与README.OpenSource 中License信息整改 2023-05-04 10:09:38 +08:00

rust-fnv

An implementation of the FowlerNollVo hash function.

Read the documentation

About

The FNV hash function is a custom Hasher implementation that is more efficient for smaller hash keys.

The Rust FAQ states that while the default Hasher implementation, SipHash, is good in many cases, it is notably slower than other algorithms with short keys, such as when you have a map of integers to other values. In cases like these, FNV is demonstrably faster.

Its disadvantages are that it performs badly on larger inputs, and provides no protection against collision attacks, where a malicious user can craft specific keys designed to slow a hasher down. Thus, it is important to profile your program to ensure that you are using small hash keys, and be certain that your program could not be exposed to malicious inputs (including being a networked server).

The Rust compiler itself uses FNV, as it is not worried about denial-of-service attacks, and can assume that its inputs are going to be small—a perfect use case for FNV.

Usage

To include this crate in your program, add the following to your Cargo.toml:

[dependencies]
fnv = "1.0.3"

Using FNV in a HashMap

The FnvHashMap type alias is the easiest way to use the standard librarys HashMap with FNV.

use fnv::FnvHashMap;

let mut map = FnvHashMap::default();
map.insert(1, "one");
map.insert(2, "two");

map = FnvHashMap::with_capacity_and_hasher(10, Default::default());
map.insert(1, "one");
map.insert(2, "two");

Note, the standard librarys HashMap::new and HashMap::with_capacity are only implemented for the RandomState hasher, so using Default to get the hasher is the next best option.

Using FNV in a HashSet

Similarly, FnvHashSet is a type alias for the standard librarys HashSet with FNV.

use fnv::FnvHashSet;

let mut set = FnvHashSet::default();
set.insert(1);
set.insert(2);

set = FnvHashSet::with_capacity_and_hasher(10, Default::default());
set.insert(1);
set.insert(2);