Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-144384: Improve _colorize performance by replacing dataclass with regular class#144879

Closed
hugovk wants to merge 2 commits into
python:mainpython/cpython:mainfrom
hugovk:3.15-colorize-performancehugovk/cpython:3.15-colorize-performanceCopy head branch name to clipboard
Closed

gh-144384: Improve _colorize performance by replacing dataclass with regular class#144879
hugovk wants to merge 2 commits into
python:mainpython/cpython:mainfrom
hugovk:3.15-colorize-performancehugovk/cpython:3.15-colorize-performanceCopy head branch name to clipboard

Conversation

@hugovk
Copy link
Copy Markdown
Member

@hugovk hugovk commented Feb 16, 2026

_colorize constructs six different theme classes (and I expect more in the future), which are slow to build because they use dataclasses.

This makes the module as a whole slow to import, even the importer doesn't use a theme or doesn't use colour in the end.

If we switch to regular classes, the construction time is much faster. This PR also avoid the collections.abc import.

traceback

Below are import times for a macOS build with optimisations.

before: 15 ms

image

after: 6 ms

image

_colorize

before: 13 ms

image

after: ~0 ms

image

@ambv
Copy link
Copy Markdown
Contributor

ambv commented Feb 16, 2026

Marked with DO-NOT-MERGE as this approach isn't agreed upon at this point.

@hugovk
Copy link
Copy Markdown
Member Author

hugovk commented Mar 21, 2026

Closing. We can make the diff smaller by keeping the Mapping, but the better approach is to make dataclasses faster. Let's continue in the isssue.

@hugovk hugovk closed this Mar 21, 2026
@hugovk hugovk deleted the 3.15-colorize-performance branch March 21, 2026 14:09
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.

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.