proxygen
F14SmallOverheads.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <functional>
18 #include <iostream>
19 #include <limits>
20 #include <memory>
21 #include <string>
22 #include <unordered_map>
23 #include <utility>
24 
25 #include <folly/container/F14Map.h>
26 
27 using namespace std;
28 using namespace folly;
29 
30 template <typename T>
31 struct LoggingAlloc {
32  using value_type = T;
33 
35 
36  template <typename A>
37  explicit LoggingAlloc(A&&) {}
38 
39  T* allocate(std::size_t n) {
40  cout << "allocate " << n << " values, " << n * sizeof(T) << " bytes\n";
41  return std::allocator<T>{}.allocate(n);
42  }
43 
44  void deallocate(T* ptr, std::size_t n) {
45  cout << "deallocate " << n << " values, " << n * sizeof(T) << " bytes\n";
46  std::allocator<T>{}.deallocate(ptr, n);
47  }
48 
49  bool operator==(LoggingAlloc<T> const&) const {
50  return true;
51  }
52  bool operator!=(LoggingAlloc<T> const&) const {
53  return false;
54  }
55 
56  // Everything below here is optional when properly using
57  // allocator_traits, but dense_hash_map doesn't use allocator_traits yet
58 
59  using pointer = T*;
60  using const_pointer = T const*;
61  using reference = T&;
62  using const_reference = T const&;
63  using size_type = std::size_t;
64  using difference_type = std::ptrdiff_t;
65 
66  template <typename U>
67  struct rebind {
69  };
70 
71  T* address(T& v) const {
72  return &v;
73  }
74  T const* address(T const& v) const {
75  return &v;
76  }
77  std::size_t max_size() const {
79  }
80 };
81 
82 template <typename K, typename V, template <typename> class A>
83 using StdUnorderedMapTable = std::unordered_map<
84  K,
85  V,
86  std::hash<K>,
87  std::equal_to<K>,
88  A<std::pair<K const, V>>>;
89 
90 template <typename K, typename V, template <typename> class A>
91 using F14ValueMapTable =
92  F14ValueMap<K, V, std::hash<K>, std::equal_to<K>, A<std::pair<K const, V>>>;
93 
94 template <typename K, typename V, template <typename> class A>
95 using F14NodeMapTable =
96  F14NodeMap<K, V, std::hash<K>, std::equal_to<K>, A<std::pair<K const, V>>>;
97 
98 template <typename K, typename V, template <typename> class A>
100  K,
101  V,
102  std::hash<K>,
103  std::equal_to<K>,
104  A<std::pair<K const, V>>>;
105 
106 template <typename M>
108  cout << "----------------------\n";
109  cout << name << "\n";
110  cout << "SIZE = " << sizeof(M) << "\n";
111  cout << "CONSTRUCTING\n";
112  {
113  M map;
114  cout << "INSERTING 1 VALUE\n";
115  typename M::key_type k{};
116  map[k];
117  cout << "DESTROYING\n";
118  }
119  cout << "\n";
120 }
121 
122 template <template <typename, typename, template <typename> class> class T>
124  runSingleInsert<T<uint64_t, array<char, 8>, LoggingAlloc>>(
125  name + " uint64_t 8");
126  runSingleInsert<T<string, array<char, 8>, LoggingAlloc>>(name + " string 8");
127  runSingleInsert<T<uint64_t, array<char, 128>, LoggingAlloc>>(
128  name + " uint64_t 128");
129  runSingleInsert<T<string, array<char, 128>, LoggingAlloc>>(
130  name + " string 128");
131 }
132 
134  std::unordered_map<int16_t, float>& m,
135  int16_t k) {
136  auto i = m.find(k);
137  return i != m.end() ? 1 : 0;
138 }
139 
142  int16_t k) {
143  auto i = m.find(k);
144  return i != m.end() ? 1 : 0;
145 }
146 
149  int16_t k) {
150  auto i = m.find(k);
151  return i != m.end() ? 1 : 0;
152 }
153 
156  int16_t k) {
157  auto i = m.find(k);
158  return i != m.end() ? 1 : 0;
159 }
160 
162  std::unordered_map<int16_t, uint32_t>& m,
163  int16_t k,
164  uint32_t v) {
165  m[k] = v;
166 }
167 
170  int16_t k,
171  uint32_t v) {
172  m[k] = v;
173 }
174 
177  int16_t k,
178  uint32_t v) {
179  m[k] = v;
180 }
181 
184  int16_t k,
185  uint32_t v) {
186  m[k] = v;
187 }
188 
190  std::unordered_map<int16_t, uint32_t>& m,
191  std::unordered_map<int16_t, uint32_t>::iterator iter) {
192  m.erase(iter);
193 }
194 
198  m.erase(iter);
199 }
200 
204  m.erase(iter);
205 }
206 
210  m.erase(iter);
211 }
212 
213 int main(int, char**) {
214  (void)codeSize_find_Std;
216  (void)codeSize_find_F14Node;
218 
219  (void)codeSize_bracket_Std;
223 
224  (void)codeSize_erase_Std;
228 
229  runSingleInserts<StdUnorderedMapTable>("std");
230  runSingleInserts<F14ValueMapTable>("f14value");
231  runSingleInserts<F14NodeMapTable>("f14node");
232  runSingleInserts<F14VectorMapTable>("f14vector");
233 
234  return 0;
235 }
T const * const_pointer
void * ptr
FOLLY_NOINLINE int codeSize_find_Std(std::unordered_map< int16_t, float > &m, int16_t k)
#define T(v)
Definition: http_parser.c:233
auto v
std::unique_ptr< int > A
FOLLY_NOINLINE void codeSize_bracket_F14Node(F14NodeMap< int16_t, uint32_t > &m, int16_t k, uint32_t v)
T * allocate(std::size_t n)
LogLevel max
Definition: LogLevel.cpp:31
std::size_t size_type
std::unordered_map< K, V, std::hash< K >, std::equal_to< K >, A< std::pair< K const, V >>> StdUnorderedMapTable
void deallocate(T *ptr, std::size_t n)
T const * address(T const &v) const
T * address(T &v) const
STL namespace.
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
FOLLY_NOINLINE int codeSize_find_F14Node(F14NodeMap< int16_t, float > &m, int16_t k)
FOLLY_NOINLINE void codeSize_erase_F14Value(F14ValueMap< int16_t, uint32_t > &m, F14ValueMap< int16_t, uint32_t >::iterator iter)
int main(int, char **)
bool operator==(LoggingAlloc< T > const &) const
std::size_t max_size() const
FOLLY_NOINLINE void codeSize_erase_F14Node(F14NodeMap< int16_t, uint32_t > &m, F14NodeMap< int16_t, uint32_t >::iterator iter)
const char * name
Definition: http_parser.c:437
#define FOLLY_NOINLINE
Definition: CPortability.h:142
static Map map(mapCap)
static map< string, int > m
std::ptrdiff_t difference_type
FOLLY_NOINLINE int codeSize_find_F14Value(F14ValueMap< int16_t, float > &m, int16_t k)
FOLLY_NOINLINE void codeSize_bracket_F14Vector(F14VectorMap< int16_t, uint32_t > &m, int16_t k, uint32_t v)
FOLLY_NOINLINE void codeSize_erase_Std(std::unordered_map< int16_t, uint32_t > &m, std::unordered_map< int16_t, uint32_t >::iterator iter)
**Optimized Holders **The template hazptr_array< M > provides most of the functionality *of M hazptr_holder s but with faster construction destruction *for M
Definition: Hazptr.h:104
FOLLY_NOINLINE void codeSize_erase_F14Vector(F14VectorMap< int16_t, uint32_t > &m, F14VectorMap< int16_t, uint32_t >::iterator iter)
FOLLY_NOINLINE void codeSize_bracket_Std(std::unordered_map< int16_t, uint32_t > &m, int16_t k, uint32_t v)
void runSingleInsert(std::string const &name)
bool operator!=(LoggingAlloc< T > const &) const
const char * string
Definition: Conv.cpp:212
FOLLY_NOINLINE int codeSize_find_F14Vector(F14VectorMap< int16_t, float > &m, int16_t k)
const
Definition: upload.py:398
FOLLY_NOINLINE void codeSize_bracket_F14Value(F14ValueMap< int16_t, uint32_t > &m, int16_t k, uint32_t v)
KeyT k
T const & const_reference
void runSingleInserts(std::string const &name)