Skip to content

Commit 300e9c8

Browse files
committed
Solve python call benchmark bug.
1 parent 1af4c4c commit 300e9c8

File tree

1 file changed

+49
-37
lines changed

1 file changed

+49
-37
lines changed

source/benchmarks/metacall_py_call_bench/source/metacall_py_call_bench.cpp

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,6 @@
2626
class metacall_py_call_bench : public benchmark::Fixture
2727
{
2828
public:
29-
void SetUp(benchmark::State &state)
30-
{
31-
metacall_print_info();
32-
33-
metacall_log_null();
34-
35-
if (metacall_initialize() != 0)
36-
{
37-
state.SkipWithError("Error initializing MetaCall");
38-
}
39-
40-
/* Python */
41-
#if defined(OPTION_BUILD_LOADERS_PY)
42-
{
43-
static const char tag[] = "py";
44-
45-
static const char int_mem_type[] =
46-
"#!/usr/bin/env python3\n"
47-
"def int_mem_type(left: int, right: int) -> int:\n"
48-
"\treturn 0;";
49-
50-
if (metacall_load_from_memory(tag, int_mem_type, sizeof(int_mem_type), NULL) != 0)
51-
{
52-
state.SkipWithError("Error loading int_mem_type function");
53-
}
54-
}
55-
#endif /* OPTION_BUILD_LOADERS_PY */
56-
}
57-
58-
void TearDown(benchmark::State &state)
59-
{
60-
if (metacall_destroy() != 0)
61-
{
62-
state.SkipWithError("Error destroying MetaCall");
63-
}
64-
}
6529
};
6630

6731
BENCHMARK_DEFINE_F(metacall_py_call_bench, call_va_args)
@@ -174,4 +138,52 @@ BENCHMARK_REGISTER_F(metacall_py_call_bench, call_array_args)
174138
->Iterations(1)
175139
->Repetitions(5);
176140

177-
BENCHMARK_MAIN();
141+
/* Use main for initializing MetaCall once. There's a bug in Python async which prevents reinitialization */
142+
/* https://github.com/python/cpython/issues/89425 */
143+
/* https://bugs.python.org/issue45262 */
144+
/* metacall-py-call-benchd: ./Modules/_asynciomodule.c:261: get_running_loop: Assertion `Py_IS_TYPE(rl, &PyRunningLoopHolder_Type)' failed. */
145+
int main(int argc, char *argv[])
146+
{
147+
metacall_print_info();
148+
149+
metacall_log_null();
150+
151+
if (metacall_initialize() != 0)
152+
{
153+
return 1;
154+
}
155+
156+
/* Python */
157+
#if defined(OPTION_BUILD_LOADERS_PY)
158+
{
159+
static const char tag[] = "py";
160+
161+
static const char int_mem_type[] =
162+
"#!/usr/bin/env python3\n"
163+
"def int_mem_type(left: int, right: int) -> int:\n"
164+
"\treturn 0;";
165+
166+
if (metacall_load_from_memory(tag, int_mem_type, sizeof(int_mem_type), NULL) != 0)
167+
{
168+
return 2;
169+
}
170+
}
171+
#endif /* OPTION_BUILD_LOADERS_PY */
172+
173+
::benchmark::Initialize(&argc, argv);
174+
175+
if (::benchmark::ReportUnrecognizedArguments(argc, argv))
176+
{
177+
return 3;
178+
}
179+
180+
::benchmark::RunSpecifiedBenchmarks();
181+
::benchmark::Shutdown();
182+
183+
if (metacall_destroy() != 0)
184+
{
185+
return 4;
186+
}
187+
188+
return 0;
189+
}

0 commit comments

Comments
 (0)