Skip to content

Fix broken GlobalWindow singleton - #40910

Open
Abacn wants to merge 1 commit into
apache:masterfrom
Abacn:global-window-singleton
Open

Abacn wants to merge 1 commit into
apache:masterfrom
Abacn:global-window-singleton

Conversation

@Abacn

@Abacn Abacn commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

GlobalWindow implements the singleton pattern via __new__ by caching and returning cls._instance. However, because Python always invokes __init__ on the instance returned by __new__ (whenever __new__ returns an instance of cls), defining GlobalWindow.__init__ to call
super().__init__(GlobalWindow._getTimestampFromProto()) caused every GlobalWindow() call to re-parse the proto timestamp constant, allocate a new Timestamp object, and re-initialize BoundedWindow._end.

Move the one-time BoundedWindow.__init__ initialization into GlobalWindow.__new__ when cls._instance is None and make GlobalWindow.__init__ a no-op.

==========

Benchmark Results (source: https://gist.github.com/Abacn/93fa3168450bf36838e70d853a77d2e3)

Benchmark Baseline Median (sec / elem) Post-Fix Median (sec / elem) Speedup Time Reduction
GlobalWindowInstantiate 5.711e-07 7.120e-08 8.02x -87.5%
GlobalWindowsAssign 7.567e-07 2.399e-07 3.15x -68.3%
WindowIntoWorker_GlobalWindows 4.144e-06 2.934e-06 1.41x -29.2%
WindowIntoWorker_FixedWindows 5.970e-06 5.954e-06 1.00x -0.3%
WriteImplBoundedPipeline 1.643e-05 1.522e-05 1.08x -7.4%

@github-actions github-actions Bot added python core size/xs XS: <10 lines changed labels Oct 30, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 Review Summary

This PR resolves a performance issue in GlobalWindow where the singleton instance was repeatedly re-initialized via BoundedWindow.__init__ and _getTimestampFromProto() on every GlobalWindow() instantiation. Moving the initialization into __new__ (when _instance is None) and making __init__ a no-op properly preserves singleton semantics and provides a nice performance boost in window assignment.

🔍 General Feedback

  • The implementation is clean, Idiomatic for Python __new__-based singletons, and safely retains _end across pickle/unpickle cycles.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@Abacn

Abacn commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

context: when benchmarking a batch FileIO write pipeline (https://github.com/Abacn/apache-beam-sstable), this path alone is found to cost 8.6% of total CPU time

image

`GlobalWindow` implements the singleton pattern via `__new__` by caching and
returning `cls._instance`. However, because Python always invokes `__init__` on
the instance returned by `__new__` (whenever `__new__` returns an instance of
`cls`), defining `GlobalWindow.__init__` to call
`super().__init__(GlobalWindow._getTimestampFromProto())` caused every
`GlobalWindow()` call to re-parse the proto timestamp constant, allocate a new
`Timestamp` object, and re-initialize `BoundedWindow._end`.

Move the one-time `BoundedWindow.__init__` initialization into
`GlobalWindow.__new__` when `cls._instance is None` and make
`GlobalWindow.__init__` a no-op.
@Abacn
Abacn force-pushed the global-window-singleton branch from 9d97e3e to f06199b Compare October 30, 2026 19:26
@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @damccorm for label python.
Backup reviewer: jrmccluskey (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

This pull request likely touches a core component ("core" label). Please review with scrutiny.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@codecov

codecov Bot commented Oct 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.08%. Comparing base (12bbb17) to head (f06199b).
⚠️ Report is 12 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #40910      +/-   ##
==========================================
+ Coverage   79.83%   80.08%   +0.24%     
==========================================
  Files         494      494              
  Lines       83575    83583       +8     
==========================================
+ Hits        66722    66934     +212     
+ Misses      16853    16649     -204     
Flag Coverage Δ
python 80.08% <100.00%> (+0.24%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant