Skip to content

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

Closed
hugovk wants to merge 2 commits intopython:mainfrom
hugovk:3.15-colorize-performance
Closed

gh-144384: Improve _colorize performance by replacing dataclass with regular class#144879
hugovk wants to merge 2 commits intopython:mainfrom
hugovk:3.15-colorize-performance

Conversation

@hugovk
Copy link
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
Contributor

ambv commented Feb 16, 2026

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

@hugovk
Copy link
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