Skip to content

Commit cd99490

Browse files
committed
feat: use inline start/stop functions
1 parent 131975b commit cd99490

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ bazel_dep(name = "instrument_hooks", version = "1.0.0")
33

44
git_override(
55
module_name = "instrument_hooks",
6-
commit = "8e872739f0d6866810edb9b4f1816a53bfdc11a7",
6+
commit = "3fdf53b869568fd2d20f11a72be574563846c356",
77
remote = "https://github.com/CodSpeedHQ/instrument-hooks",
88
)

core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ include(FetchContent)
1515
FetchContent_Declare(
1616
instrument_hooks
1717
GIT_REPOSITORY https://github.com/CodSpeedHQ/instrument-hooks/
18-
GIT_TAG 8e872739f0d6866810edb9b4f1816a53bfdc11a7
18+
GIT_TAG 3fdf53b869568fd2d20f11a72be574563846c356
1919
)
2020
FetchContent_MakeAvailable(instrument_hooks)
2121

core/include/measurement.hpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ extern "C" {
1616
#include "core.h"
1717
}
1818

19-
inline InstrumentHooks* get_hooks() {
20-
static InstrumentHooks* g_hooks = nullptr;
19+
static InstrumentHooks* g_hooks = nullptr;
20+
21+
inline void measurement_init() {
2122
if (!g_hooks) {
2223
g_hooks = instrument_hooks_init();
2324
}
24-
return g_hooks;
2525
}
2626

2727
inline std::string get_version() {
@@ -34,36 +34,40 @@ inline std::string get_version() {
3434

3535
#ifdef CODSPEED_INSTRUMENTATION
3636
inline bool measurement_is_instrumented() {
37-
return instrument_hooks_is_instrumented(get_hooks());
37+
return instrument_hooks_is_instrumented(g_hooks);
3838
}
3939

4040
inline void measurement_set_metadata() {
4141
std::string version = get_version();
42-
instrument_hooks_set_integration(get_hooks(), "codspeed-cpp",
43-
version.c_str());
42+
instrument_hooks_set_integration(g_hooks, "codspeed-cpp", version.c_str());
4443
}
4544

4645
__attribute__((always_inline)) inline void measurement_start() {
47-
instrument_hooks_start_benchmark(get_hooks());
46+
instrument_hooks_start_benchmark_inline(g_hooks);
4847
}
4948

50-
__attribute__((always_inline)) inline void measurement_stop(
51-
const std::string& name) {
52-
instrument_hooks_stop_benchmark(get_hooks());
49+
__attribute__((always_inline)) inline void measurement_stop() {
50+
instrument_hooks_stop_benchmark_inline(g_hooks);
51+
}
5352

53+
__attribute__((always_inline)) inline void measurement_executed_benchmark(
54+
const std::string& name) {
5455
#ifdef _WIN32
5556
auto current_pid = _getpid();
5657
#else
5758
auto current_pid = getpid();
5859
#endif
59-
instrument_hooks_executed_benchmark(get_hooks(), current_pid, name.c_str());
60-
};
60+
instrument_hooks_executed_benchmark(g_hooks, current_pid, name.c_str());
61+
}
6162
#else
6263
// Stub implementations for non-instrumentation builds
6364
inline bool measurement_is_instrumented() { return false; }
6465
inline void measurement_set_metadata() {}
6566
inline void measurement_start() {}
66-
inline void measurement_stop(const std::string& name) { (void)name; }
67+
inline void measurement_stop() {}
68+
inline void measurement_executed_benchmark(const std::string& name) {
69+
(void)name;
70+
}
6771
#endif
6872

6973
#endif // MEASUREMENT_H

core/src/codspeed.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ CodSpeed::CodSpeed() : is_instrumented(measurement_is_instrumented()) {
5555
"be made since it's running in an unknown environment."
5656
<< std::endl;
5757
}
58+
measurement_init();
5859
measurement_set_metadata();
5960
}
6061

@@ -85,7 +86,8 @@ void CodSpeed::start_benchmark(const std::string &name) {
8586
}
8687

8788
void CodSpeed::end_benchmark() {
88-
measurement_stop(current_benchmark);
89+
measurement_executed_benchmark(current_benchmark);
90+
8991
benchmarked.push_back(current_benchmark);
9092
std::string action_str = is_instrumented ? "Measured" : "Checked";
9193
std::string group_str =

google_benchmark/include/benchmark/benchmark.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,8 @@ struct State::StateIterator {
10651065
bool operator!=(StateIterator const&) const {
10661066
if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0, true)) return true;
10671067
#ifdef CODSPEED_INSTRUMENTATION
1068+
measurement_stop();
1069+
10681070
if (parent_->codspeed_ != NULL) {
10691071
parent_->codspeed_->end_benchmark();
10701072
}
@@ -1086,8 +1088,9 @@ inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::end() {
10861088
#ifdef CODSPEED_INSTRUMENTATION
10871089
if (this->codspeed_ != NULL) {
10881090
this->codspeed_->start_benchmark(name_);
1089-
measurement_start();
10901091
}
1092+
1093+
measurement_start();
10911094
#endif
10921095
return StateIterator();
10931096
}

0 commit comments

Comments
 (0)