-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_cpython_api.cpp
More file actions
89 lines (75 loc) · 2.77 KB
/
bench_cpython_api.cpp
File metadata and controls
89 lines (75 loc) · 2.77 KB
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
#include <benchmark/benchmark.h>
#include <Python.h>
// ============================================================================
// PyUnstable Code Extra API Benchmarks
// ============================================================================
// Dummy free function for code extra data
static void dummy_free([[maybe_unused]]void* ptr) {
}
// Benchmark PyUnstable_Eval_RequestCodeExtraIndex
static void BM_CodeExtra_RequestIndex(benchmark::State& state) {
for (auto _ : state) {
Py_ssize_t idx = PyUnstable_Eval_RequestCodeExtraIndex(dummy_free);
if (idx < 0) {
state.SkipWithMessage("ID limit reached!");
break;
}
benchmark::DoNotOptimize(idx);
}
state.SetItemsProcessed(state.iterations());
}
BENCHMARK(BM_CodeExtra_RequestIndex)->Iterations(250);
// Benchmark PyUnstable_Code_SetExtra
static void BM_CodeExtra_SetExtra(benchmark::State& state) {
Py_ssize_t idx = 0;
PyCodeObject* code = PyCode_NewEmpty("my_file.py","my_func", 1);
// Data to set
void* extra_data = (void*)0xDEADBEEF;
for (auto _ : state) {
int result = PyUnstable_Code_SetExtra((PyObject*)code, idx, extra_data);
if (result < 0) {
state.SkipWithError("PyUnstable_Code_SetExtra failed");
return;
}
}
state.SetItemsProcessed(state.iterations());
}
BENCHMARK(BM_CodeExtra_SetExtra);
// Benchmark PyUnstable_Code_GetExtra
static void BM_CodeExtra_GetExtra(benchmark::State& state) {
Py_ssize_t idx = 0;
PyCodeObject* code = PyCode_NewEmpty("my_file.py","my_func", 1);
// Set some data first
void* set_data = (void*)0xCAFEBABE;
PyUnstable_Code_SetExtra((PyObject*)code, idx, set_data);
for (auto _ : state) {
void* extra_data = NULL;
int result = PyUnstable_Code_GetExtra((PyObject*)code, idx, &extra_data);
if (result < 0) {
state.SkipWithError("PyUnstable_Code_GetExtra failed");
return;
}
benchmark::DoNotOptimize(extra_data);
}
state.SetItemsProcessed(state.iterations());
}
BENCHMARK(BM_CodeExtra_GetExtra);
// Benchmark SetExtra + GetExtra together (typical usage pattern)
static void BM_CodeExtra_SetGet(benchmark::State& state) {
Py_ssize_t idx = 0;
PyCodeObject* code = PyCode_NewEmpty("my_file.py","my_func", 1);
void* data = (void*)0x12345678;
for (auto _ : state) {
// Set
if (PyUnstable_Code_SetExtra((PyObject*)code, idx, data) < 0) {
state.SkipWithError("SetExtra failed");
return;
}
// Get
void* retrieved = NULL;
PyUnstable_Code_GetExtra((PyObject*)code, idx, &retrieved);
benchmark::DoNotOptimize(retrieved);
}
state.SetItemsProcessed(state.iterations());
}
BENCHMARK(BM_CodeExtra_SetGet);