Skip to content

semantic: two quadratics made the build slower with every module, 22 min down to about 10 - #3684

Merged
BaseMax merged 5 commits into
mainfrom
perf/parallel-build
Nov 19, 2026
Merged

BaseMax merged 5 commits into
mainfrom
perf/parallel-build

Conversation

@BaseMax

@BaseMax BaseMax commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Why a big build is slow, measured with callgrind and per stage timestamps on the compiler building itself (88 modules, --backend=c):

Stage Before (s) After (s)
lex and parse 0.5 0.8
semantic analysis 788 225
dead code elimination (DceFinish, a silent gap before the first generating C) 188 1
C generation and writing (already parallel, one process per CPU) 121 121 to 158
object compile (already a parallel pool) 228 228 to 331
link 1.2 1.1
total 1334 724 under load average 18 to 30

Lexing and parsing are 0.5 s of a 22 minute build, so threading them gains nothing. Semantic analysis got slower with every loaded module (the same 5 definition package took 4 s early and 9 s late), which is the up to 10 s per package that was reported.

Causes and fixes:

  • copy_program_deep_copies scanned every AST node on every sema_collect (36 percent of the run): now a watermark scan once per node.
  • DceFinish and DceReachable used linear Vector membership: roots and reach are hash sets and DceFinish uses a per caller edge list.
  • su_has_ctx_word ran four strstr calls per non ASCII identifier compare (13 M calls): lead byte check first.

Output is unchanged: all generated .c and .h files of the old and the new compiler are byte identical except the BUILD_DATE strings. semantic_test.salam gives 491 passed and 2 failed with the new compiler, the same 2 fail on main.

docs/design/BUILD_PARALLEL.md records the breakdown and what is left: caching analysed std packages between builds (the only way to make incremental rebuilds fast, high risk because sema tables are index based), pipelining codegen behind analysis, threaded per package sema (blocked by the g_sm_ globals). Wall times were taken on a machine under heavy load from other jobs, the front end and DCE numbers are the cleanest.

Summary by CodeRabbit

  • Performance

    • Improved compiler dead-code analysis by avoiding repeated scans when tracking reachable code.
    • Reduced repeated AST scanning during deep-copy processing.
  • Bug Fixes

    • Persian-context detection now checks for specific Persian characters instead of treating any non-ASCII text as Persian.
  • Documentation

    • Added build timing results and notes on current build stages and optimization opportunities.

@BaseMax
BaseMax requested a review from a team as a code owner November 19, 2026 17:45
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

Walkthrough

The semantic analysis changes add hash-set tracking for DCE, cache scans for deep_copy members, and refine context-word detection. A design note documents build timings, current build stages, and remaining work.

Changes: Semantic analysis

Layer / File(s) Summary
DCE membership and traversal
compiler/semantic/semantic.salam, compiler/semantic/sema_api.salam, compiler/semantic/sema_dce.salam
DCE root and reachable-function membership now use hash sets. DceFinish indexes call edges by caller and traverses only outgoing edges. DceReachable checks the reachable set.
Deep-copy scan cache
compiler/semantic/semantic.salam, compiler/semantic/sema_copy.salam
Sema stores scan progress and whether a deep_copy member was found. copy_program_deep_copies resumes from its cached position and resets the cache if the AST count shrinks.
Context-word detection and build note
compiler/semantic/sema_util.salam, docs/design/BUILD_PARALLEL.md
su_has_ctx_word checks for byte pairs corresponding to ا, ژ, or خ. The design note reports build timings and describes build stages, documented performance changes, and remaining work.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Possibly related PRs

  • SalamLang/Salam#2459: This PR also changes su_has_ctx_word and targets the context-word search in the semantic pass.

Suggested reviewers: bdadmehr0, erfangnu

Merge Risk: Minimal · up to 8b211

This change speeds up semantic analysis and dead-code elimination in the compiler build without changing generated output. No concrete defect was found in the reviewed changes, so merge risk is minimal.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor
sha=96a469647f3e7a04f778c040848aafa89d2d91c1

🤖 Salam AI review

Verdict: ✅ Good to merge

commit 96a4696 · 6 files · +120 −28 · model salam

Summary

This pull request optimizes semantic analysis and dead-code elimination by replacing several quadratic scans with watermarking, hash-set reachability, and a cheaper Persian-context prefilter, and adds a design note documenting build timings. The changes appear behavior-preserving, with the author reporting byte-identical generated C/H output (except build date) and unchanged semantic test results versus main.

What changed

The PR reduces front-end build time by removing repeated work in semantic analysis and DCE: compiler/semantic/sema_copy.salam now scans the AST once per new node for deep_copy using Sema watermark fields added in compiler/semantic/semantic.salam; compiler/semantic/sema_api.salam and compiler/semantic/sema_dce.salam replace linear root/reach membership checks and repeated edge scans with hash sets and a per-caller adjacency list built in DceFinish; and compiler/semantic/sema_util.salam adds a UTF-8 lead-byte prefilter before the Persian context-word substring checks. docs/design/BUILD_PARALLEL.md documents measured stage timings and remaining opportunities. The provided diff for docs/design/BUILD_PARALLEL.md was truncated at the end.

Review

  • compiler/semantic/sema_copy.salam (around copy_program_deep_copies): the watermark/reset logic correctly limits scans to newly added AST nodes and short-circuits once a deep_copy member is seen, matching the stated assumption that the AST only grows.
  • compiler/semantic/sema_api.salam and compiler/semantic/sema_dce.salam: root/reach tracking via g_sm_dce_root_set/g_sm_dce_reach_set and traversal over head/nxt preserves reachability while avoiding quadratic membership/edge scans; the worklist loop and DceReachable use the same reach set consistently.
  • compiler/semantic/sema_util.salam (su_has_ctx_word): checking the lead-byte pairs for the existing words before str.Contains is a behavior-preserving fast path for those specific context markers.

Tests

The PR description reports all generated .c/.h files are byte-identical except BUILD_DATE, and semantic_test.salam yields 491 passed and 2 failed, the same as main. No new regression tests are included, but the reported equivalence supports this optimization-focused change.

Risks

Low. Future additions to the recognized Persian context words in su_has_ctx_word would also require updating the lead-byte prefilter so detection remains complete.


⚠️ This review was written automatically by an AI and may contain mistakes. It is a helper for maintainers, not a replacement for a human review: check the code yourself before merging.

@BaseMax
BaseMax merged commit 98973a4 into main Nov 19, 2026
3 checks passed
@BaseMax
BaseMax deleted the perf/parallel-build branch November 19, 2026 17:51
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.

1 participant