Skip to content

docs: add memory benefit and variable-reuse note to collect() section#1173

Open
k1chik wants to merge 2 commits intoprometheus:masterfrom
k1chik:fix/yield-explanation-custom-collector
Open

docs: add memory benefit and variable-reuse note to collect() section#1173
k1chik wants to merge 2 commits intoprometheus:masterfrom
k1chik:fix/yield-explanation-custom-collector

Conversation

@k1chik
Copy link
Copy Markdown
Contributor

@k1chik k1chik commented May 8, 2026

Closes points made from comment section in #1169.

Changes

  • Added a paragraph to the `collect()` protocol section noting that memory
    is a secondary benefit of using `yield`: yielding inline lets Python
    reclaim the object as soon as the registry advances, while a named variable
    stays alive until it is rebound.
  • Added a short loop example showing that yielding inside a loop rather than
    accumulating into a list keeps at most one object alive at a time.
  • Example verified by running it against the library.

cc @calestyo @csmarchbanks

@k1chik
Copy link
Copy Markdown
Contributor Author

k1chik commented May 8, 2026

Closing to reopen with a proper description.

@k1chik k1chik closed this May 8, 2026
@k1chik k1chik reopened this May 8, 2026
@k1chik k1chik closed this May 8, 2026
@k1chik k1chik reopened this May 8, 2026
k1chik added 2 commits May 8, 2026 17:52
Addresses follow-up feedback on prometheus#1169 from @calestyo: the existing
yield explanation mentioned lazy iteration but omitted memory as a
secondary benefit and gave no guidance on variable reuse for GC
efficiency.

Added a paragraph explaining that yielding inline lets Python reclaim
the object as soon as the registry advances, while a named variable
stays alive until rebound. Also added a short loop example showing
how reusing the same variable name across iterations gives the GC
the opportunity to reclaim each object before the next is allocated.

Signed-off-by: k1chik <107162115+k1chik@users.noreply.github.com>
Signed-off-by: k1chik <107162115+k1chik@users.noreply.github.com>
@k1chik k1chik force-pushed the fix/yield-explanation-custom-collector branch from 4667a37 to 387e948 Compare May 8, 2026 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant