Skip to content

Swift: avoid module scan for lazy body emission - #22408

Merged
jketema merged 2 commits into
github:mainfrom
arun279:codex/avoid-lazy-module-scan-current
Aug 25, 2026
Merged

Swift: avoid module scan for lazy body emission#22408
jketema merged 2 commits into
github:mainfrom
arun279:codex/avoid-lazy-module-scan-current

Conversation

@arun279

@arun279 arun279 commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Summary

Avoid enumerating every top-level declaration in a Swift module when the extractor is processing one lazy declaration. extractLazy passes the pending declaration's own module, and shouldEmitDeclBody separately rejects every declaration except that exact currentLazyDeclaration, so populating currentTopLevelDecls cannot affect the result.

Performance

I profiled a reproducible CodeQL scan of a small Xcode project at 10 ms intervals. Of 33,602 samples in lazy extraction, 28,176 were in ModuleDecl::getTopLevelDecls while constructing SwiftBodyEmissionStrategy.

On identical source, runner, Xcode, CodeQL 2.26.3, architecture, and build flags:

Measurement Unpatched Patched
Linked workflow wall time shown by GitHub 27m37s 28m55s
Benchmark-only extractor source build skipped 9m33s
Build analyzed application step 22m03s 11m53s
Resolve Package Graph 8m46s 4m42s
Helper-to-app compile interval 10m33s 4m02s

The patched workflow is 1m18s longer overall because this diagnostic cell first builds the modified extractor from source. That setup is absent from the unpatched cell and would not exist when the extractor ships prebuilt with CodeQL. The measured claim here is a 46.1% reduction in the analyzed-application build step, not an end-to-end reduction in these two diagnostic workflows.

Correctness checks

Both runs completed analysis with:

  • the same 22 extracted project source files;
  • the same 37 sorted SARIF artifact URIs;
  • 1,892 successfully extracted project lines;
  • zero unresolved AST nodes;
  • zero findings.

The patched database was not smaller: its Swift AST-node metric was 699,967 versus 696,067 and its TRAP data was 585.74 MiB versus 583.35 MiB. Both source archives contained the same 22 files with byte-identical contents. No extraction-quality regression was observed.

The current CodeQL branch passes git diff --check and clang-format --dry-run --Werror. The patched extractor was also compiled from source and exercised by the patched benchmark run. At this PR's base commit, SwiftExtractor.cpp and SwiftBodyEmissionStrategy.{h,cpp} are unchanged from CodeQL 2.26.3.

Signed-off-by: Arun Krishnamurthy <arun.km.279@gmail.com>
@arun279
arun279 requested review from a team as code owners August 21, 2026 19:33
@github-actions github-actions Bot added the Swift label Aug 21, 2026
@jketema

jketema commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

This looks correct. Thanks for your contribution!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Optimizes Swift lazy declaration extraction by avoiding an unnecessary module-wide declaration scan.

Changes:

  • Skips populating currentTopLevelDecls when extracting one lazy declaration.
  • Preserves existing body-emission filtering behavior.
Show a summary per file
File Description
swift/extractor/infra/SwiftBodyEmissionStrategy.h Avoids the costly top-level declaration scan during lazy extraction.

Review details

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Balanced

@jketema
jketema merged commit e9caa4e into github:main Aug 25, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants