Skip to content

Optimize Signature.clone options copying with an immutable fast path - #10163

Open
AsymptotaX wants to merge 11 commits into
celery:mainfrom
AsymptotaX:optimize-clone-options
Open

AsymptotaX wants to merge 11 commits into
celery:mainfrom
AsymptotaX:optimize-clone-options

Conversation

@AsymptotaX

Copy link
Copy Markdown

Description

This PR optimizes a hot path in Signature.clone() by avoiding deepcopy() when task options contain only immutable scalar values.

What changed

  • Added a fast path in celery/canvas.py:
    if all option values are immutable scalars (str, bytes, int, float, bool, None), use dict.copy() otherwise keep the existing deepcopy() behavior
  • Kept behavior unchanged for nested/mutable option values.

Why
Large canvas workflows (group/chord) call clone() heavily during publish. This reduces copy overhead on the common scalar-only options case while preserving safety for mutable structures.

benchmark

Baseline (main) PR Delta
clone() throughput (200k iters) 480,199.7 clones/sec 802,540.2 clones/sec +67.1%
clone() throughput (1M iters) 489,985.2 clones/sec 798,834.5 clones/sec +63.0%
group.apply_async publish rate (mean) 3,303.2 tasks/sec 3,611.3 tasks/sec +9.3%
chord.apply_async publish rate (mean) 3,312.6 tasks/sec 3,483.9 tasks/sec +5.2%

(Real publish benchmark used local Redis, same host, same parameters for A/B.)

@Nusnus Nusnus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Interesting PR.. lets see how the CI is going..

@codecov

codecov Bot commented Mar 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.22%. Comparing base (2d560f5) to head (037050f).
⚠️ Report is 484 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10163      +/-   ##
==========================================
- Coverage   88.22%   88.22%   -0.01%     
==========================================
  Files         153      153              
  Lines       19559    19566       +7     
  Branches     2246     2248       +2     
==========================================
+ Hits        17256    17262       +6     
  Misses       2005     2005              
- Partials      298      299       +1     
Flag Coverage Δ
unittests 88.19% <100.00%> (-0.01%) ⬇️

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.

@auvipy
auvipy requested review from auvipy and Copilot March 3, 2026 05:19
@auvipy auvipy added this to the 5.7.0 milestone Mar 3, 2026

Copilot AI 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.

Pull request overview

This PR optimizes Signature.clone() — a hot path in large canvas workflows (group/chord) — by avoiding deepcopy() when task options contain only immutable scalar values. A new helper _clone_options() applies a shallow copy in the scalar-only case and falls back to deepcopy otherwise, preserving correctness for mutable structures like link, link_error, chord, and stamped_headers.

Changes:

  • Added _FAST_PATH_IMMUTABLE_TYPES tuple and _clone_options() helper in celery/canvas.py.
  • Replaced deepcopy(opts) with _clone_options(opts) in Signature.clone().
  • Added two targeted unit tests covering the fast-path (scalar options) and fallback (mutable options) behaviors.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
celery/canvas.py Adds _FAST_PATH_IMMUTABLE_TYPES constant and _clone_options() helper; wires it into Signature.clone() in place of the direct deepcopy(opts) call.
t/unit/tasks/test_canvas.py Adds two tests verifying that scalar options are independently copied and that mutable options are still deep-copied on clone.

Comment thread celery/canvas.py Resolved
@Nusnus
Nusnus force-pushed the optimize-clone-options branch from 34baff5 to 49c1f43 Compare March 3, 2026 10:32
@Nusnus
Nusnus self-requested a review March 3, 2026 10:33

@auvipy auvipy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should we consider adding an Integration test? or it is already covered?

Copilot AI 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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread celery/canvas.py Resolved
@AsymptotaX

Copy link
Copy Markdown
Author

should we consider adding an Integration test? or it is already covered?

I think this is already sufficiently covered at the unit level. Since the change is internal to Signature.clone() and does not alter observable canvas behavior, an integration test would likely be redundant.

@auvipy

auvipy commented Mar 15, 2026

Copy link
Copy Markdown
Member

@ChickenBenny please feel free to try it and share your views

@auvipy

auvipy commented Apr 6, 2026

Copy link
Copy Markdown
Member

@aurangzaib048 can you please try and review it

@auvipy auvipy modified the milestones: 5.7.0, 5.8.0 Aug 29, 2026

This branch has not been deployed

No deployments
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.

4 participants