Skip to content

Put the repaired Cython fast paths behind an opt-in setting - #765

Closed
wbarnha wants to merge 2 commits into
claude/faust-topic-buffer-full-tpfrom
claude/faust-cython-optimizations-flag
Closed

Put the repaired Cython fast paths behind an opt-in setting#765
wbarnha wants to merge 2 commits into
claude/faust-topic-buffer-full-tpfrom
claude/faust-cython-optimizations-flag

Conversation

@wbarnha

@wbarnha wbarnha commented Aug 7, 2026

Copy link
Copy Markdown
Member

Description

Two of the Cython fast paths never ran — each guarded by a condition that could not become true — so the extensions quietly did more work than the Python they were meant to accelerate. Repairing them (in #762 and #763) activates code that has, by definition, never executed in production. This gates it.

cython_optimizations defaults to False. With it off the extensions behave exactly as the released versions do, so upgrading changes nothing. Users opt in per app:

app = faust.App('myapp', cython_optimizations=True)

or CYTHON_OPTIMIZATIONS=1 in the environment (prefixed when env_prefix is set, like every other env-backed setting).

The flag is read once per StreamIterator and once per ConductorHandler — so once per stream and once per assigned TP, not per message — into a bint, leaving a predictable branch on the hot path rather than an attribute lookup into app.conf.

What it gates, and what it does not

Gated:

  • StreamIterator._try_get_quick_value — taking values already in the channel queue instead of always awaiting.
  • ConductorHandler event reuse — decoding once and reusing the event across channels with matching key/value types, instead of deserializing once per subscribed channel.

Not gated: the on_topic_buffer_full argument fix (#764). That one was wrong in both implementations, is not Cython-specific, and produced a metric that was simply incorrect — gating a wrong metric key behind a "Cython improvements" flag would be incoherent. It applies unconditionally.

One consequence worth stating plainly

While the setting is off, the Cython and pure-Python paths genuinely differ. That is not new — it is what has shipped for years — and the flag does not introduce the divergence, only makes it selectable.

The sharpest case is the conductor: a reused event is never decoded again, so a channel whose payload would fail to deserialize raises no error when the event is reused, and raises one when it is not. That changes which channels receive a message and how many acks it takes.

So the parity suites now run with the setting on, which is the configuration in which the two implementations are supposed to agree. Each suite also gains a test pinning the default-off behaviour, so the historical path — the one most users will actually run — stays covered: 5 awaits for 5 queued values in the iterator, one decode per channel in the conductor.

Verification

configuration result
extensions built (FAUST_REQUIRE_CYTHON=1) 2274 passed
extensions absent 2208 passed
free-threaded 3.14t, PYTHON_GIL=0 2278 passed
mypy -p faust clean
extra/tools/verify_doc_defaults.py All OK

flake8 / black / isort clean. Docs build clean, with the setting rendered into the configuration reference and a new cython_optimizations section in the developer guide.

Note on docs/includes/settingref.txt: the entry was added by hand rather than by make configref. The committed file was generated by different tooling — regenerating it reformats every block in the file, which would bury this change in a tree-wide diff.

Note on the base branch

Based on claude/faust-topic-buffer-full-tp (#764), which is based on #763, which is based on #762. The flag gates changes introduced in #762 and #763, so it has to sit above them. Retarget down the stack as each lands.


Generated by Claude Code

claude added 2 commits August 7, 2026 21:47
Two of the Cython fast paths never ran, each guarded by a condition that
could not become true, so the extensions quietly did more work than the
Python they were meant to accelerate.  Repairing them (in the two PRs
below this one) activates code that has by definition never executed in
production.  Gate it.

`cython_optimizations` defaults to False.  With it off the extensions
behave exactly as the released versions do, so upgrading changes
nothing; users opt in per app:

    app = faust.App('myapp', cython_optimizations=True)

or `CYTHON_OPTIMIZATIONS=1` in the environment (prefixed when
`env_prefix` is set, like every other env-backed setting).

The flag is read once per StreamIterator and once per ConductorHandler
-- so once per stream and once per assigned TP, not per message -- into
a `bint`, leaving a predictable branch on the hot path rather than an
attribute lookup into `app.conf`.

## What it gates, and what it does not

Gated:

  * `StreamIterator._try_get_quick_value` -- taking values already in
    the channel queue instead of always awaiting.
  * `ConductorHandler` event reuse -- decoding once and reusing the
    event across channels with matching key/value types, instead of
    deserializing once per subscribed channel.

Not gated: the `on_topic_buffer_full` argument fix.  That one was wrong
in *both* implementations, is not Cython-specific, and produced a metric
that was simply incorrect -- gating a wrong metric key behind a
"Cython improvements" flag would be incoherent.  It applies always.

## Consequence worth stating plainly

While the setting is off, the Cython and pure-Python paths genuinely
differ.  That is not new -- it is what has shipped for years -- and the
flag does not introduce the divergence, only makes it selectable.  The
sharpest case is the conductor: a reused event is never decoded again,
so a channel whose payload would fail to deserialize raises no error
when the event is reused and raises one when it is not, changing which
channels receive a message and how many acks it takes.

So the parity suites now run with the setting on, which is the
configuration in which the two implementations are supposed to agree.
Each suite also gains a test pinning the default-off behaviour, so the
historical path -- the one most users will actually run -- stays
covered: 5 awaits for 5 queued values in the iterator, one decode per
channel in the conductor.

## Verification

Suite green in every configuration: extensions built (2274 passed),
absent (2208 passed), free-threaded 3.14t under PYTHON_GIL=0 (2278
passed).  `mypy -p faust` clean, `extra/tools/verify_doc_defaults.py`
clean, docs build clean with the setting rendered into the
configuration reference.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K8qT5E3rnSXvw7ibNLXrVr
The setting is transitional -- it exists so the repaired Cython fast
paths are adopted deliberately rather than arriving in an upgrade, and
it is meant to be removed, not kept.  Retiring it naively has a trap in
it, which this closes before anyone walks into it.

`Param.__get__` emits a UserWarning on *every read* of a setting once
`version_deprecated` is set, and faust reads this one itself: once per
Stream, once per assigned partition.  Setting `version_deprecated` would
therefore make faust warn at itself, at a rate that scales with the
deployment, about a setting the user most likely never set and cannot
act on.  Measured before the change: three StreamIterator constructions,
three warnings.

Both extensions now read the flag through
`faust.utils.optin.cython_optimizations_enabled`, which takes the value
the descriptor stores instead of going through the descriptor.  Internal
reads stay silent; `app.conf.cython_optimizations` still warns, which is
the entire point of deprecating a setting -- a helper that disarmed that
too would be worse than the noise, because nobody would ever be told to
stop using it.

The storage attribute is looked up through the settings registry rather
than hard-coded, so renaming the setting cannot silently turn this into
a read of a missing attribute.

Deliberately not `warnings.catch_warnings()`: it manipulates global
state and is not thread-safe, which matters on the free-threaded builds
this branch series added support for.

## Tests

tests/unit/utils/test_optin.py pins both halves of the contract -- the
internal read silent under deprecation, the public read still warning --
plus an end-to-end check that three stream iterators and three conductor
handlers produce zero warnings with the setting marked deprecated (three
and three before).  The deprecation is applied by a fixture that restores
the param afterwards, so the tests need no released deprecation to run.

## Docs

The developer guide gains the intended sequence: ships off, default
flipped once there is real-world evidence (parity passing is necessary
but not sufficient -- it only proves the two implementations agree under
test), deprecated, then removed along with the branches, the helper and
the default-off tests.  The setting's own docstring says it is
transitional, so it does not read as permanent API.

Suite green in every configuration: extensions built (2280 passed),
absent (2213 passed), free-threaded 3.14t under PYTHON_GIL=0 (2284
passed).  mypy, verify_doc_defaults and the docs build all clean.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K8qT5E3rnSXvw7ibNLXrVr

wbarnha commented Aug 7, 2026

Copy link
Copy Markdown
Member Author

Folded into #762 — the whole series is now one branch, so there is no stack to land in order. Both commits (cf10ab2, the cython_optimizations opt-in; e0ca537, making its eventual retirement a two-line change) are unchanged there.


Generated by Claude Code

@wbarnha wbarnha closed this Aug 7, 2026
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.

2 participants