Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Reduce redundant object creation while calling callback function from gc #104028

Closed
Labels
performancePerformance or resource usage
@corona10

Description

@corona10

cpython/Modules/gcmodule.c

Lines 1391 to 1402 in4b27972

for (Py_ssize_ti=0;i<PyList_GET_SIZE(gcstate->callbacks);i++) {
PyObject*r,*cb=PyList_GET_ITEM(gcstate->callbacks,i);
Py_INCREF(cb);/* make sure cb doesn't go away */
r=PyObject_CallFunction(cb,"sO",phase,info);
if (r==NULL) {
PyErr_WriteUnraisable(cb);
}
else {
Py_DECREF(r);
}
Py_DECREF(cb);
}

The phase object can be created earlier with one-time creation and it can be replaced with vectorcall either.
From my microbenchmark, there is 4-5% performance improvement by doing this.

microbenchmark

importpyperfimportgcdefbenchamark_collection(loops):defcallback_foo(phase,info):passfor_inrange(100):gc.callbacks.append(callback_foo)total_time=0for_inrange(loops):t0=pyperf.perf_counter()collected=gc.collect()total_time+=pyperf.perf_counter()-t0returntotal_timeif__name__=="__main__":runner=pyperf.Runner()runner.metadata["description"]="GC callback benchmark"runner.bench_time_func("create_gc_cycles",benchamark_collection)

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usage

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp