v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
bytes.h
Go to the documentation of this file.
1// Copyright 2023 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_HEAP_BASE_BYTES_H_
6#define V8_HEAP_BASE_BYTES_H_
7
8#include <algorithm>
9#include <cstddef>
10#include <limits>
11#include <optional>
12
15
16namespace heap::base {
17
18struct BytesAndDuration final {
19 constexpr BytesAndDuration() = default;
21 : bytes(bytes), duration(duration) {}
22
23 size_t bytes = 0;
25};
26
28
29// Returns the average speed of events recorded in `buffer` including an
30// `initial` event in Bytes/ms. If provided, `selected_duration` will bound the
31// events considered (which uses the order of events in
32// `BytesAndDurationBuffer`). The bounds are in Bytes/ms and can be used to
33// bound non-zero speeds.
34inline std::optional<double> AverageSpeed(
35 const BytesAndDurationBuffer& buffer, const BytesAndDuration& initial,
36 std::optional<v8::base::TimeDelta> selected_duration,
37 size_t min_non_empty_speed = 0,
38 size_t max_speed = std::numeric_limits<size_t>::max()) {
39 const BytesAndDuration sum = buffer.Reduce(
40 [selected_duration](const BytesAndDuration& a,
41 const BytesAndDuration& b) {
42 if (selected_duration.has_value() &&
43 a.duration >= selected_duration.value()) {
44 return a;
45 }
46 return BytesAndDuration(a.bytes + b.bytes, a.duration + b.duration);
47 },
48 initial);
49 const auto duration = sum.duration;
50 if (duration.IsZero()) {
51 return std::nullopt;
52 }
53 return std::max(
54 std::min(static_cast<double>(sum.bytes) / duration.InMillisecondsF(),
55 static_cast<double>(max_speed)),
56 static_cast<double>(min_non_empty_speed));
57}
58
60 public:
62 : decay_(decay) {}
63
64 void Update(BytesAndDuration bytes_and_duration) {
65 if (bytes_and_duration.duration.IsZero()) {
66 return;
67 }
68 const double new_throughput = bytes_and_duration.bytes /
69 bytes_and_duration.duration.InMillisecondsF();
70 throughput_ = new_throughput + Decay(throughput_ - new_throughput,
71 bytes_and_duration.duration);
72 }
73 // Return throughput of memory (in bytes) over time (in millis).
74 double GetThroughput() const { return throughput_; }
75
76 // Returns throughput decayed as if `delay` passed.
77 double GetThroughput(v8::base::TimeDelta delay) const {
78 return Decay(throughput_, delay);
79 }
80
81 private:
82 double Decay(double throughput, v8::base::TimeDelta delay) const {
83 return throughput *
84 exp2(-delay.InMillisecondsF() / decay_.InMillisecondsF());
85 }
86
87 double throughput_ = 0.0;
89};
90
91} // namespace heap::base
92
93#endif // V8_HEAP_BASE_BYTES_H_
void Update(BytesAndDuration bytes_and_duration)
Definition bytes.h:64
SmoothedBytesAndDuration(v8::base::TimeDelta decay)
Definition bytes.h:61
const v8::base::TimeDelta decay_
Definition bytes.h:88
double GetThroughput(v8::base::TimeDelta delay) const
Definition bytes.h:77
double Decay(double throughput, v8::base::TimeDelta delay) const
Definition bytes.h:82
constexpr T Reduce(Callback callback, const T &initial) const
Definition ring-buffer.h:40
double InMillisecondsF() const
Definition time.cc:226
constexpr bool IsZero() const
Definition time.h:113
std::optional< TNode< JSArray > > a
std::optional< double > AverageSpeed(const BytesAndDurationBuffer &buffer, const BytesAndDuration &initial, std::optional< v8::base::TimeDelta > selected_duration, size_t min_non_empty_speed=0, size_t max_speed=std::numeric_limits< size_t >::max())
Definition bytes.h:34
constexpr BytesAndDuration(size_t bytes, v8::base::TimeDelta duration)
Definition bytes.h:20
v8::base::TimeDelta duration
Definition bytes.h:24
constexpr BytesAndDuration()=default