GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Reduce redundant object creation while calling callback function from gc · Issue #104028 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
Reduce redundant object creation while calling callback function from gc#104028
Copy link
Labels
performancePerformance or resource usagePerformance or resource usage

Description

@corona10
corona10
opened on May 1, 2023
Issue body actions

cpython/Modules/gcmodule.c

Lines 1391 to 1402 in 4b27972

for (Py_ssize_t i=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

import pyperf
import gc


def benchamark_collection(loops):
    def callback_foo(phase, info):
        pass
    for _ in range(100):
        gc.callbacks.append(callback_foo)

    total_time = 0
    for _ in range(loops):
        t0 = pyperf.perf_counter()
        collected = gc.collect()
        total_time += pyperf.perf_counter() - t0
    return total_time


if __name__ == "__main__":
    runner = pyperf.Runner()
    runner.metadata["description"] = "GC callback benchmark"
    runner.bench_time_func("create_gc_cycles", benchamark_collection)

Linked PRs

  • gh-104028: Reduce object creation while calling callback function from gc #104030
Reacted by Ken Jin
Reacted by Ken Jin

Activity

  1. corona10
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on May 1, 2023
  2. corona10
    added a commit that references this issue on May 1, 2023

    pythongh-104028: Reduce objects creation while calling callback funct…

    6977273
  3. corona10
    changed the title [-]Reduce redundant phase objects while calling callback function on gc[/-] [+]Reduce redundant object creation while calling callback function on gc[/+] on May 1, 2023
  4. corona10
    added
    performancePerformance or resource usagePerformance or resource usage
    and removed
    type-featureA feature request or enhancementA feature request or enhancement
    on May 1, 2023
  5. corona10
    added a commit that references this issue on May 1, 2023

    pythongh-104028: Reduce objects creation while calling callback funct…

    47534f3
  6. corona10
    changed the title [-]Reduce redundant object creation while calling callback function on gc[/-] [+]Reduce redundant object creation while calling callback function from gc[/+] on May 1, 2023
  7. corona10
    added 2 commits that reference this issue on May 1, 2023

    pythongh-104028: Reduce objects creation while calling callback funct…

    d55c1a1

    gh-104028: Reduce object creation while calling callback function fro…

    e147694
  8. corona10
    closed this as completedon May 1, 2023
  9. carljm
    added 2 commits that reference this issue on May 1, 2023

    Merge branch 'main' into supermon

    159b2aa

    Merge branch 'main' into inlinecomp2

    1274e2b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usagePerformance or resource usage

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.