codeflash/docs/cli-reference/optimization.mdx
2026-01-16 15:20:07 +02:00

172 lines
3.9 KiB
Text

---
title: "Optimization Commands"
description: "Optimize single functions or entire codebases with Codeflash CLI"
icon: "bullseye"
sidebarTitle: "Optimization Commands"
keywords: ["optimization", "function", "file", "all", "commands"]
---
# Optimization Commands
Commands for optimizing individual functions or entire codebases.
---
## Optimize a Single Function
Target a specific function in a file for optimization.
```bash
codeflash --file <path/to/file.py> --function <function_name>
```
<Accordion title="Complete Examples">
<Tabs>
<Tab title="Linux/macOS">
```bash
# Basic optimization (creates PR)
codeflash --file src/utils.py --function calculate_metrics
# Local optimization only (no PR)
codeflash --file src/utils.py --function calculate_metrics --no-pr
# With verbose output
codeflash --file src/utils.py --function calculate_metrics --no-pr --verbose
```
</Tab>
<Tab title="Windows">
```powershell
# Basic optimization (creates PR)
codeflash --file src\utils.py --function calculate_metrics
# Local optimization only (no PR)
codeflash --file src\utils.py --function calculate_metrics --no-pr
# With verbose output
codeflash --file src\utils.py --function calculate_metrics --no-pr --verbose
```
</Tab>
</Tabs>
</Accordion>
<Warning>
**Important**: The file must be within your configured `module-root`
directory. Files outside `module-root` will be ignored with "Functions outside
module-root" message.
</Warning>
---
## Optimize All Functions
Optimize all functions in your entire codebase or a specific directory.
```bash
# Optimize entire codebase
codeflash --all
# Optimize specific directory
codeflash --all src/core/
```
<Accordion title="Complete Examples">
<Tabs>
<Tab title="Linux/macOS">
```bash
# Optimize all (creates PRs)
codeflash --all
# Optimize all locally (no PRs)
codeflash --all --no-pr
# Optimize specific directory
codeflash --all src/algorithms/ --no-pr
# Skip draft PRs in CI
codeflash --all --no-draft
```
</Tab>
<Tab title="Windows">
```powershell
# Optimize all (creates PRs)
codeflash --all
# Optimize all locally (no PRs)
codeflash --all --no-pr
# Optimize specific directory
codeflash --all src\algorithms\ --no-pr
# Skip draft PRs in CI
codeflash --all --no-draft
```
</Tab>
</Tabs>
</Accordion>
<Info>
When using `--all`, Codeflash will:
- Discover all optimizable functions in your codebase
- Create separate PRs for each function (or update locally with `--no-pr`)
- Process functions in batches to avoid overwhelming your repository
</Info>
---
## Benchmark Mode
Optimize code based on performance benchmarks using pytest-benchmark format.
```bash
codeflash --file <file.py> --benchmark --benchmarks-root <path>
```
<Accordion title="Complete Examples">
<Tabs>
<Tab title="Linux/macOS">
```bash
# With benchmarks-root flag
codeflash --file src/core.py --benchmark --benchmarks-root tests/benchmarks --no-pr
# If benchmarks-root is in pyproject.toml
codeflash --file src/core.py --benchmark --no-pr
```
</Tab>
<Tab title="Windows">
```powershell
# With benchmarks-root flag
codeflash --file src\core.py --benchmark --benchmarks-root tests\benchmarks --no-pr
# If benchmarks-root is in pyproject.toml
codeflash --file src\core.py --benchmark --no-pr
```
</Tab>
</Tabs>
</Accordion>
<Warning>
The `--benchmarks-root` directory must exist and be configured either via
`pyproject.toml` or the command-line flag.
</Warning>
---
## Next Steps
<CardGroup cols={2}>
<Card
title="Tracing & Workflows"
icon="route"
href="/cli-reference/tracing"
>
Learn about trace-based optimization
</Card>
<Card
title="Flags Reference"
icon="list"
href="/cli-reference/flags"
>
Complete flag reference
</Card>
</CardGroup>