Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: b5f03204ae8abf0fe4649544f9e486936caa3c1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// String internment benchmarks and baselines
//
//  Copyright (C) 2014-2020 Ryan Specialty Group, LLC.
//
//  This file is part of TAME.
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Note that the baseline tests have a _suffix_ rather than a prefix so that
// they are still grouped with the associated test in the output, since it's
// sorted lexically by function name.

#![feature(test)]

extern crate tamer;
extern crate test;

use std::rc::Rc;
use tamer::sym::*;
use test::Bencher;

fn gen_strs(n: usize) -> Vec<String> {
    (0..n)
        .map(|n| n.to_string() + "foobarbazquuxlongsymbol")
        .collect()
}

mod interner {
    use super::*;
    use std::collections::hash_map::RandomState;
    use std::collections::HashSet;
    use std::hash::BuildHasher;

    pub struct HashSetSut<S = RandomState>
    where
        S: BuildHasher,
    {
        pub map: HashSet<Rc<str>, S>,
    }

    impl<S> HashSetSut<S>
    where
        S: BuildHasher + Default,
    {
        #[inline]
        fn new() -> Self {
            Self {
                map: HashSet::with_hasher(Default::default()),
            }
        }

        pub fn intern(&mut self, value: &str) -> Rc<str> {
            if !self.map.contains(value) {
                self.map.insert(value.into());
            }

            self.map.get(value).unwrap().clone()
        }
    }

    /// This is our baseline with a raw Rc<str>.
    #[bench]
    fn with_all_new_rc_str_1000_baseline(bench: &mut Bencher) {
        let strs = gen_strs(1000);

        bench.iter(|| {
            let mut sut = HashSetSut::<RandomState>::new();
            strs.iter().map(|s| sut.intern(&s)).for_each(drop);
        });
    }

    #[bench]
    fn with_all_new_1000(bench: &mut Bencher) {
        let strs = gen_strs(1000);

        bench.iter(|| {
            let sut = ArenaInterner::<RandomState>::new();
            strs.iter().map(|s| sut.intern(&s)).for_each(drop);
        });
    }

    #[bench]
    /// This is our baseline with a raw Rc<str>.
    fn with_one_new_rc_str_1000_baseline(bench: &mut Bencher) {
        bench.iter(|| {
            let mut sut = HashSetSut::<RandomState>::new();
            (0..1000).map(|_| sut.intern("first")).for_each(drop);
        });
    }

    #[bench]
    fn with_one_new_1000(bench: &mut Bencher) {
        bench.iter(|| {
            let sut = ArenaInterner::<RandomState>::new();
            (0..1000).map(|_| sut.intern("first")).for_each(drop);
        });
    }

    mod fx {
        use super::*;
        use fxhash::FxBuildHasher;

        /// This is our baseline with a raw Rc<str>.
        #[bench]
        fn with_all_new_rc_str_1000_baseline(bench: &mut Bencher) {
            let strs = gen_strs(1000);
            bench.iter(|| {
                let mut sut = HashSetSut::<FxBuildHasher>::new();
                strs.iter().map(|s| sut.intern(&s)).for_each(drop);
            });
        }

        #[bench]
        fn with_all_new_1000(bench: &mut Bencher) {
            let strs = gen_strs(1000);

            bench.iter(|| {
                let sut = ArenaInterner::<FxBuildHasher>::new();
                strs.iter().map(|s| sut.intern(&s)).for_each(drop);
            });
        }

        #[bench]
        /// This is our baseline with a raw Rc<str>.
        fn with_one_new_rc_str_1000_baseline(bench: &mut Bencher) {
            bench.iter(|| {
                let mut sut: HashSetSut<FxBuildHasher> = HashSetSut {
                    map: HashSet::with_hasher(Default::default()),
                };
                (0..1000).map(|_| sut.intern("first")).for_each(drop);
            });
        }

        #[bench]
        fn with_one_new_1000(bench: &mut Bencher) {
            bench.iter(|| {
                let sut = ArenaInterner::<FxBuildHasher>::new();
                (0..1000).map(|_| sut.intern("first")).for_each(drop);
            });
        }

        #[bench]
        fn with_one_new_1000_utf8_unchecked(bench: &mut Bencher) {
            bench.iter(|| {
                let sut = ArenaInterner::<FxBuildHasher>::new();
                (0..1000)
                    .map(|_| unsafe { sut.intern_utf8_unchecked(b"first") })
                    .for_each(drop);
            });
        }

        /// Since Fx is the best-performing, let's build upon it to demonstrate
        /// the benefits of with_capacity
        #[bench]
        fn with_all_new_1000_with_capacity(bench: &mut Bencher) {
            let n = 1000;
            let strs = gen_strs(n);

            bench.iter(|| {
                let sut = ArenaInterner::<FxBuildHasher>::with_capacity(n);
                strs.iter().map(|s| sut.intern(&s)).for_each(drop);
            });
        }
    }
}