Skip to content

Add opt-in optimizing AST backend and optimization levels - #137

Open
sharktide wants to merge 3 commits into
mainfrom
optimizer/ast-passes
Open

sharktide wants to merge 3 commits into
mainfrom
optimizer/ast-passes

Conversation

@sharktide

@sharktide sharktide commented Dec 6, 2026 •

Copy link
Copy Markdown
Owner

First optimizer foundation: conservative literal folding, constant conditional-expression simplification, immutable literal membership, pass reports, opt-in -O0 through -O3 config/API/CLI integration, and differential tests. O2/O3 currently share the same passes; no performance claims or automatic unsafe transformations. Later PRs add native runtime specialization. No release or merge.

Summary by Sourcery

Introduce an experimental, opt-in AST optimization backend while preserving unoptimized compilation as the default.

New Features:

  • Add opt-in optimization levels -O0 through -O3, with conservative literal folding and conditional-expression simplification at O1, plus immutable literal membership optimization at O2 and O3.
  • Expose optimization configuration through project TOML, compiler and runner CLI flags, Python compilation APIs, and a public optimization API that reports pass counts.

Enhancements:

  • Keep optimization disabled by default, with O2 and O3 currently enabling the same passes.

Documentation:

  • Document the experimental optimizer, its configuration, behavior, and limitations in the README and a new optimizer guide.
  • Add a release-blocking Python compatibility invariant to the contributor guide.

Tests:

  • Add optimizer tests covering semantic equivalence, optimization levels, API/config/CLI integration, pass reports, safety boundaries, and default Python compatibility.

@sourcery-ai

sourcery-ai Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Data structures

Define the optimizer’s result shape, supported levels, and bounded literal eligibility that the optimization passes depend on.

  • restructuredpython/optimizer.py L1-49

API

Expose the optimizer and its pass-count result as a public Python API.

  • restructuredpython/__init__.py L18, L27

Add optimization options to compiler, run, and CLI entry points, resolving configuration and applying optimization to generated code.

  • restructuredpython/restructuredpython.py L58-69, L121-126, L322-330 (+16 small edits)

Implementation

Implement bounded AST rewrites, preserve source when optimization is disabled or no pass applies, and validate optimized output.

  • restructuredpython/optimizer.py L50-167

Configuration

Document the default-Python-compatibility invariant and the requirement for explicit, tested opt-ins.

  • CONTRIBUTING.md L18-21

Advertise the experimental optimization option and its configuration form to users.

  • README.md L84-87

Documentation

Explain optimization levels, invocation, pass behavior, and limitations.

  • docs/optimizer.md L1-19

Tests

Cover optimizer behavior and safety, pass counts, API/config/CLI integration, include handling, and default Python compatibility.

  • tests/test_optimizer.py L1-116

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 7 issues


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread restructuredpython/optimizer.py Resolved
Comment thread restructuredpython/optimizer.py Resolved
Comment thread restructuredpython/optimizer.py Resolved
Comment thread tests/test_optimizer.py Resolved

This branch has not been deployed

No deployments
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.

1 participant