Skip to content

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

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

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.

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