proxygen
DigestBuilder.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018-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 #pragma once
18 
19 #include <memory>
20 
21 #include <folly/SpinLock.h>
22 
23 namespace folly {
24 namespace detail {
25 
26 /*
27  * Stat digests, such as TDigest, can be expensive to merge. It is faster to
28  * buffer writes and merge them in larger chunks. DigestBuilder buffers writes
29  * to improve performance.
30  *
31  * Values are stored in a cpu local buffer. Hot stats will merge the cpu local
32  * buffer into a cpu-local digest when the buffer size is reached.
33  *
34  * All methods in this class are thread safe, but it probably doesn't make sense
35  * for multiple threads to call build simultaneously. A typical usage is to
36  * buffer writes for a period of time, and then have one thread call build to
37  * merge the buffer into some other DigestT instance.
38  */
39 template <typename DigestT>
41  public:
42  explicit DigestBuilder(size_t bufferSize, size_t digestSize);
43 
44  /*
45  * Builds a DigestT from the buffer. All values used to build the DigestT are
46  * removed from the buffer.
47  */
48  DigestT build();
49 
50  /*
51  * Adds a value to the buffer.
52  */
53  void append(double value);
54 
55  private:
56  struct alignas(hardware_destructive_interference_size) CpuLocalBuffer {
57  public:
58  mutable SpinLock mutex;
59  std::vector<double> buffer;
60  std::unique_ptr<DigestT> digest;
61  };
62 
63  std::vector<CpuLocalBuffer> cpuLocalBuffers_;
64  size_t bufferSize_;
65  size_t digestSize_;
66 };
67 
68 } // namespace detail
69 } // namespace folly
std::vector< CpuLocalBuffer > cpuLocalBuffers_
Definition: DigestBuilder.h:63
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
DigestBuilder(size_t bufferSize, size_t digestSize)