• Stars
    star
    2,266
  • Rank 20,331 (Top 0.5 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created about 6 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Rust port of Google's SwissTable hash map

hashbrown

Build Status Crates.io Documentation Rust

This crate is a Rust port of Google's high-performance SwissTable hash map, adapted to make it a drop-in replacement for Rust's standard HashMap and HashSet types.

The original C++ version of SwissTable can be found here, and this CppCon talk gives an overview of how the algorithm works.

Since Rust 1.36, this is now the HashMap implementation for the Rust standard library. However you may still want to use this crate instead since it works in environments without std, such as embedded systems and kernels.

Change log

Features

  • Drop-in replacement for the standard library HashMap and HashSet types.
  • Uses AHash as the default hasher, which is much faster than SipHash. However, AHash does not provide the same level of HashDoS resistance as SipHash, so if that is important to you, you might want to consider using a different hasher.
  • Around 2x faster than the previous standard library HashMap.
  • Lower memory usage: only 1 byte of overhead per entry instead of 8.
  • Compatible with #[no_std] (but requires a global allocator with the alloc crate).
  • Empty hash maps do not allocate any memory.
  • SIMD lookups to scan multiple hash entries in parallel.

Performance

Compared to the previous implementation of std::collections::HashMap (Rust 1.35).

With the hashbrown default AHash hasher:

name oldstdhash ns/iter hashbrown ns/iter diff ns/iter diff % speedup
insert_ahash_highbits 18,865 8,020 -10,845 -57.49% x 2.35
insert_ahash_random 19,711 8,019 -11,692 -59.32% x 2.46
insert_ahash_serial 19,365 6,463 -12,902 -66.63% x 3.00
insert_erase_ahash_highbits 51,136 17,916 -33,220 -64.96% x 2.85
insert_erase_ahash_random 51,157 17,688 -33,469 -65.42% x 2.89
insert_erase_ahash_serial 45,479 14,895 -30,584 -67.25% x 3.05
iter_ahash_highbits 1,399 1,092 -307 -21.94% x 1.28
iter_ahash_random 1,586 1,059 -527 -33.23% x 1.50
iter_ahash_serial 3,168 1,079 -2,089 -65.94% x 2.94
lookup_ahash_highbits 32,351 4,792 -27,559 -85.19% x 6.75
lookup_ahash_random 17,419 4,817 -12,602 -72.35% x 3.62
lookup_ahash_serial 15,254 3,606 -11,648 -76.36% x 4.23
lookup_fail_ahash_highbits 21,187 4,369 -16,818 -79.38% x 4.85
lookup_fail_ahash_random 21,550 4,395 -17,155 -79.61% x 4.90
lookup_fail_ahash_serial 19,450 3,176 -16,274 -83.67% x 6.12

With the libstd default SipHash hasher:

name oldstdhash ns/iter hashbrown ns/iter diff ns/iter diff % speedup
insert_std_highbits 19,216 16,885 -2,331 -12.13% x 1.14
insert_std_random 19,179 17,034 -2,145 -11.18% x 1.13
insert_std_serial 19,462 17,493 -1,969 -10.12% x 1.11
insert_erase_std_highbits 50,825 35,847 -14,978 -29.47% x 1.42
insert_erase_std_random 51,448 35,392 -16,056 -31.21% x 1.45
insert_erase_std_serial 87,711 38,091 -49,620 -56.57% x 2.30
iter_std_highbits 1,378 1,159 -219 -15.89% x 1.19
iter_std_random 1,395 1,132 -263 -18.85% x 1.23
iter_std_serial 1,704 1,105 -599 -35.15% x 1.54
lookup_std_highbits 17,195 13,642 -3,553 -20.66% x 1.26
lookup_std_random 17,181 13,773 -3,408 -19.84% x 1.25
lookup_std_serial 15,483 13,651 -1,832 -11.83% x 1.13
lookup_fail_std_highbits 20,926 13,474 -7,452 -35.61% x 1.55
lookup_fail_std_random 21,766 13,505 -8,261 -37.95% x 1.61
lookup_fail_std_serial 19,336 13,519 -5,817 -30.08% x 1.43

Usage

Add this to your Cargo.toml:

[dependencies]
hashbrown = "0.14"

Then:

use hashbrown::HashMap;

let mut map = HashMap::new();
map.insert(1, "one");

Flags

This crate has the following Cargo features:

  • nightly: Enables nightly-only features including: #[may_dangle].
  • serde: Enables serde serialization support.
  • rkyv: Enables rkyv serialization support.
  • rayon: Enables rayon parallel iterator support.
  • raw: Enables access to the experimental and unsafe RawTable API.
  • inline-more: Adds inline hints to most functions, improving run-time performance at the cost of compilation time. (enabled by default)
  • ahash: Compiles with ahash as default hasher. (enabled by default)
  • allocator-api2: Enables support for allocators that support allocator-api2. (enabled by default)

License

Licensed under either of:

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

More Repositories

1

rust

Empowering everyone to build reliable and efficient software.
Rust
85,122
star
2

rustlings

🦀 Small exercises to get you used to reading and writing Rust code!
Rust
47,148
star
3

mdBook

Create book from markdown files. Like Gitbook but implemented in Rust
Rust
15,892
star
4

book

The Rust Programming Language
Rust
13,981
star
5

rust-analyzer

A Rust compiler front-end for IDEs
Rust
13,571
star
6

cargo

The Rust package manager
Rust
11,293
star
7

rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
Rust
9,860
star
8

rust-by-example

Learn Rust with examples (Live code editor included)
6,109
star
9

rustup

The Rust toolchain installer
Rust
5,558
star
10

rustfmt

Format Rust code
Rust
5,524
star
11

rfcs

RFCs for changes to Rust
Markdown
5,390
star
12

futures-rs

Zero-cost asynchronous programming in Rust
Rust
5,119
star
13

rust-bindgen

Automatically generates Rust FFI bindings to C (and some C++) libraries.
Rust
3,830
star
14

rust.vim

Vim configuration for Rust.
Vim Script
3,628
star
15

miri

An interpreter for Rust's mid-level intermediate representation
Rust
3,581
star
16

rls

Repository for the Rust Language Server (aka RLS)
Rust
3,513
star
17

regex

An implementation of regular expressions for Rust. This implementation uses finite automata and guarantees linear time matching on all inputs.
Rust
3,083
star
18

crates.io

The Rust package registry
Rust
2,554
star
19

log

Logging implementation for Rust
Rust
2,180
star
20

this-week-in-rust

Data for this-week-in-rust.org
HTML
1,826
star
21

libc

Raw bindings to platform APIs for Rust
Rust
1,786
star
22

chalk

An implementation and definition of the Rust trait system using a PROLOG-like logic solver
Rust
1,713
star
23

async-book

Asynchronous Programming in Rust
Shell
1,699
star
24

nomicon

The Dark Arts of Advanced and Unsafe Rust Programming
CSS
1,698
star
25

rustc-dev-guide

A guide to how rustc works and how to contribute to it.
HTML
1,580
star
26

rustc_codegen_cranelift

Cranelift based backend for rustc
Rust
1,538
star
27

git2-rs

libgit2 bindings for Rust
Rust
1,520
star
28

cc-rs

Rust library for build scripts to compile C/C++ code into a Rust library
Rust
1,491
star
29

vscode-rust

Rust extension for Visual Studio Code
TypeScript
1,386
star
30

polonius

Defines the Rust borrow checker.
Rust
1,255
star
31

api-guidelines

Rust API guidelines
1,202
star
32

reference

The Rust Reference
Rust
1,140
star
33

rust-playground

The Rust Playground
Rust
1,076
star
34

rust-mode

Emacs configuration for Rust
Emacs Lisp
1,009
star
35

docs.rs

crates.io documentation generator
Rust
891
star
36

rustc_codegen_gcc

libgccjit AOT codegen for rustc
Rust
875
star
37

rustfix

Automatically apply the suggestions made by rustc
Rust
853
star
38

portable-simd

The testing ground for the future of portable SIMD in Rust
Rust
816
star
39

datafrog

A lightweight Datalog engine in Rust
Rust
783
star
40

flate2-rs

DEFLATE, gzip, and zlib bindings for Rust
Rust
730
star
41

socket2

Advanced configuration options for sockets.
Rust
681
star
42

unsafe-code-guidelines

Forum for discussion about what unsafe code can and can't do
641
star
43

rust-semverver

Automatic checking for semantic versioning in library crates
Rust
639
star
44

stdarch

Rust's standard library vendor-specific APIs and run-time feature detection
Rust
597
star
45

crater

Run experiments across parts of the Rust ecosystem!
Rust
588
star
46

packed_simd

Portable Packed SIMD Vectors for Rust standard library
Rust
575
star
47

rustc-perf

Website for graphing performance of rustc
Rust
539
star
48

libm

A port of MUSL's libm to Rust.
Rust
499
star
49

cfg-if

A if/elif-like macro for Rust #[cfg] statements
Rust
479
star
50

crates.io-index

Registry index for crates.io
476
star
51

backtrace-rs

Backtraces in Rust
Rust
473
star
52

style-team

Home of the Rust style team
Rust
438
star
53

docker-rust

The official Docker images for Rust
Dockerfile
414
star
54

glob

Support for matching file paths against Unix shell style patterns.
Rust
381
star
55

rust-forge

Information useful to people contributing to Rust
Rust
380
star
56

compiler-team

A home for compiler team planning documents, meeting minutes, and other such things.
HTML
380
star
57

wg-async

Working group dedicated to improving the foundations of Async I/O in Rust
Rust
365
star
58

www.rust-lang.org

The home of the Rust website
Fluent
358
star
59

compiler-builtins

Porting `compiler-rt` intrinsics to Rust
Rust
328
star
60

measureme

Support crate for rustc's self-profiling feature
Rust
324
star
61

rustc-hash

Custom hash algorithm used by rustc (plus hashmap/set aliases): fast, deterministic, not secure
Rust
312
star
62

blog.rust-lang.org

The Rust Programming Language Blog
HTML
302
star
63

team

Rust teams structure
Rust
293
star
64

glacier

A big 'ol pile of ICE.
Rust
289
star
65

project-error-handling

Error handling project group
264
star
66

atom-ide-rust

Rust IDE support for Atom, powered by the Rust Analyzer or Rust Language Server
JavaScript
259
star
67

stacker

Manual segmented stacks for Rust
Rust
252
star
68

a-mir-formality

a model of MIR and the Rust type/trait system
Rust
245
star
69

cmake-rs

Rust build dependency for running cmake
Rust
241
star
70

edition-guide

A guide to changes between various editions of Rust
238
star
71

getopts

The getopts repo maintained by the rust-lang project
Rust
229
star
72

annotate-snippets-rs

Library for snippet annotations
Rust
223
star
73

rust-roadmap-2017

Tracking Rust's roadmap
215
star
74

areweasyncyet.rs

Are we async yet?
Rust
211
star
75

rust-playpen

A web interface for running Rust code
Rust
208
star
76

wg-allocators

Home of the Allocators working group: Paving a path for a standard set of allocator traits to be used in collections!
199
star
77

rustc-demangle

Rust symbol demangling
Rust
196
star
78

lang-team

Home of the Rust lang team
JavaScript
191
star
79

highfive

Github hooks to provide an encouraging atmosphere for new contributors
Python
185
star
80

ferris-says

A Rust flavored implementation of `cowsay`
Rust
182
star
81

rust-guidelines

This repository has moved
179
star
82

cargo-bisect-rustc

Bisects rustc, either nightlies or CI artifacts
Rust
177
star
83

rustwide

Execute your code on the Rust ecosystem.
Rust
177
star
84

homu

A bot that integrates with GitHub and your favorite continuous integration service
Python
174
star
85

triagebot

Automation/tooling for Rust spaces
Rust
162
star
86

rfcbot-rs

Coordinates asynchronous decision making on Rust repositories. Status of tracked issues and PRs can be viewed at https://rfcbot.rs.
Rust
157
star
87

prev.rust-lang.org

The previous Rust website. The current website's code is at https://github.com/rust-lang/www.rust-lang.org.
HTML
153
star
88

pkg-config-rs

Build library for invoking pkg-config for Rust
Rust
147
star
89

thanks

Celebrate Rust contributors.
Rust
146
star
90

rustc-reading-club

Rust Code Reading Clubs
141
star
91

simpleinfra

Rust Infrastructure automation
HCL
140
star
92

gll

GLL parsing framework.
Rust
137
star
93

ena

An implementation of union-find / congruence-closure in Rust. Extracted from rustc for independent experimentation.
Rust
131
star
94

wg-cargo-std-aware

Repo for working on "std aware cargo"
130
star
95

rust-memory-model

Collecting examples and information to help design a memory model for Rust.
Python
125
star
96

rust-artwork

Official artwork for the Rust project.
122
star
97

libs-team

The home of the library team
Rust
107
star
98

google-summer-of-code

Rust project ideas for Google Summer of Code
105
star
99

rustup-components-history

Rustup package status history
Rust
102
star
100

wg-grammar

Where the work of WG-grammar, aiming to provide a canonical grammar for Rust, resides
Rust
101
star