Skip to content

perf: Defer black import to first use#229

Merged
gvanrossum merged 4 commits intomicrosoft:mainfrom
KRRT7:perf/defer-black
Apr 10, 2026
Merged

perf: Defer black import to first use#229
gvanrossum merged 4 commits intomicrosoft:mainfrom
KRRT7:perf/defer-black

Conversation

@KRRT7
Copy link
Copy Markdown
Contributor

@KRRT7 KRRT7 commented Apr 10, 2026

Stack: 2/4 — depends on #231. Merge #231 first, then this PR.


  • Defer import black from module level to first use in answers.py and utils.py
  • black (code formatter + transitive deps: pathspec, black.nodes, etc.) loaded on every import typeagent but only used in two cold formatting paths

black.format_str() is called in two places:

  • create_context_prompt() in knowpro/answers.py — formats debug context for LLM prompts
  • format_code() in aitools/utils.py — developer pretty-print utility

Neither runs during normal library operation. Moving the import inside each function eliminates ~78ms of transitive module loading from the import chain.

Benchmark

Azure Standard_D2s_v5 — 2 vCPU, 8 GiB RAM, Python 3.13

Import Time (hyperfine, warmup 5, min-runs 30)

Benchmark Before After Speedup
import typeagent 791 ms ± 11 ms 713 ms ± 8 ms 1.11x

Offline E2E Test Suite (hyperfine, warmup 2, min-runs 10)

Benchmark Before After Speedup
69 offline tests 5.72 s ± 90 ms 5.60 s ± 98 ms 1.02x

Generated by codeflash optimization agent

KRRT7 added 3 commits April 9, 2026 22:45
uv 0.10.x is current; the <0.10.0 constraint caused build warnings.
parse_azure_endpoint returned the raw URL including ?api-version=...
which AsyncAzureOpenAI then mangled into invalid paths like
...?api-version=2024-06-01/openai/. Strip the query string before
returning — api_version is already returned as a separate value and
passed to the SDK independently.
black is only used in create_context_prompt() and format_code() -- both
cold paths. Moving the import inside the functions avoids loading black
and its transitive deps (pathspec, black.nodes, etc.) on every
import typeagent.
Copy link
Copy Markdown
Collaborator

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

Should have one this a long time ago. :-(

@gvanrossum gvanrossum merged commit 63701f0 into microsoft:main Apr 10, 2026
15 checks passed
@KRRT7
Copy link
Copy Markdown
Contributor Author

KRRT7 commented Apr 10, 2026

Thanks, wasn't sure you wanted import time optimizations, if you do, I have a few more ready to go.

@gvanrossum
Copy link
Copy Markdown
Collaborator

This one is dear to my heart because I wish we could remove black from the mandatory dependencies. Happy to review more.

@KRRT7
Copy link
Copy Markdown
Contributor Author

KRRT7 commented Apr 10, 2026

why can't you remove black? out of curiousness

@gvanrossum
Copy link
Copy Markdown
Collaborator

why can't you remove black? out of curiousness

I could, but it gives the nicest formatted expressions when doing hardcore debugging. What we could do is import it conditionally and just use repr() if black cannot be imported. Then we can remove it from the main deps. Developers have the dev tools installed (pyright, black, isort, pytest, etc.) and will see the black-formatted debug output.

@KRRT7
Copy link
Copy Markdown
Contributor Author

KRRT7 commented Apr 10, 2026

Went ahead and did this — #235 replaces black.format_str with pprint.pformat + ast.literal_eval in both call sites (stdlib only, no conditional imports). Moved black from dependencies to the dev dependency-group so make format/make check still work but library consumers don't pull it in.

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