Skip to content

refactor: optimize cognitive complexity across workspace with llm-cc - #14

Merged
pawelchcki merged 7 commits into
mainfrom
optimize_code
Sep 7, 2026
Merged

pawelchcki merged 7 commits into
mainfrom
optimize_code

Conversation

@pawelchcki

@pawelchcki pawelchcki commented Sep 5, 2026 •

Copy link
Copy Markdown
Owner

Summary

This PR optimizes cognitive complexity across the Rust codebase in razemify using llm-cc evaluated on AMD ROCm GPU (--backend rocm --gpu-layers -1) with default analysis settings.

All public API contracts, behavioral guarantees, and unit tests are preserved (cargo test --workspace passing 100%).

Key Results

  • Cognitive Score (lmcc_per_token): 0.086455 -> 0.081603 (-5.61%)
  • Total Cognitive Complexity (llm_cc): 4167.4 -> 3927.4 (-240.0 points / -5.76%)
  • AST Nesting Depth (comp_level): 10,105 -> 9,133 (-972 levels / -9.62%)
  • Total AST Branches: 2,683 -> 2,626 (-57 branches)
  • Total Tokens: 48,203 -> 48,128 (-75 tokens)

Per-File Comparison

File Base Score Opt Score Score Change Base LLM-CC Opt LLM-CC CC Change
razemify-core/src/comic_pipeline.rs 0.094779 0.072559 -23.44% 94.4 70.6 -23.8
razemify-cli/src/main.rs 0.104016 0.086731 -16.62% 585.4 531.4 -54.0
razemify-core/src/rembg.rs 0.094888 0.080172 -15.51% 310.0 261.6 -48.4
xtask/src/cache.rs 0.119737 0.106606 -10.97% 54.6 46.8 -7.8
xtask/src/verify.rs 0.098174 0.087571 -10.80% 43.0 31.0 -12.0
razemify-core/src/pipeline.rs 0.086337 0.078575 -8.99% 190.2 152.2 -38.0
razemify-core/src/ffi.rs 0.096801 0.088669 -8.40% 69.6 62.6 -7.0
xtask/src/models.rs 0.085337 0.082485 -3.34% 303.8 293.4 -10.4
xtask/src/main.rs 0.144954 0.141969 -2.06% 31.6 27.4 -4.2
razemify-core/src/flutter_ffi.rs 0.075272 0.074042 -1.63% 110.8 102.4 -8.4
xtask/src/wasm.rs 0.086445 0.085055 -1.61% 1352.6 1326.6 -26.0

Improvements & Refactoring Patterns

  1. Guard clauses & early returns: Replaced deeply nested match and if/else structures with early returns (let Ok(...) = ... else { return ... }), significantly reducing AST tree depth.
  2. Helper decomposition: Extracted batch processing, candidate model search paths, ONNX tensor normalizations, and HTTP response handling into focused private functions.
  3. Idiomatic iterators: Replaced manual index coordinate loops with .pixels().map(...), .enumerate_pixels(), and .zip().
  4. Explicit struct syntax preserved: Preserved standard named field initialization ModelInfo { ... } in MODELS.

Verification

  • cargo test --workspace passed all 19 unit tests and doc-tests
  • Full repository scan verified with llm-cc --backend rocm --gpu-layers -1

@pawelchcki
pawelchcki changed the base branch from main to fix/ci-buildbuddy September 5, 2026 23:28
@pawelchcki
pawelchcki force-pushed the optimize_code branch 2 times, most recently from 9df72bd to 154b53a Compare September 5, 2026 23:46
@pawelchcki

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-07T09:40:40.099337Z e94c8b2 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 154b53a15d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@pawelchcki

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5eb2a9bfa8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread MODULE.bazel Resolved
@pawelchcki

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: c83e9ce420

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Base automatically changed from fix/ci-buildbuddy to main September 7, 2026 09:32
Reduce overall repository cognitive complexity score by -5.52% (lmcc_per_token: 0.086455 -> 0.081680)
and total cognitive complexity points by -5.60% (llm_cc: 4167.4 -> 3934.2) as measured by
llm-cc on ROCm GPU. AST nesting levels reduced by 962 levels (-9.52%).

Key improvements:
- Use guard clauses and early returns to flatten nested match and if/else hierarchies
- Extract inner loops into private helper functions (e.g. batch image processing, ONNX tensor normalization)
- Replace manual coordinate indexing loops with idiomatic iterators (.pixels(), .enumerate_pixels())
- Deduplicate struct initialization in presets and model tables with const constructors
- Retain exact public API contracts and 100% test pass rate
@pawelchcki

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: e94c8b2d06

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@pawelchcki
pawelchcki merged commit 077d220 into main Sep 7, 2026
2 checks passed
@pawelchcki
pawelchcki deleted the optimize_code branch September 7, 2026 10:57
pawelchcki added a commit that referenced this pull request Oct 8, 2026
Since main's #14, CI builds and tests only in -c opt, so there is no
separate fastbuild output tree.
pawelchcki added a commit that referenced this pull request Oct 8, 2026
…#17)

* Make builds deterministic across output bases and tighten hermeticity

Two clean builds of this commit in different checkouts and output bases now
produce identical action keys and identical outputs for every spawn of
`//...` and of the CI `-c opt` APK and WASM build, and the APKs and WASM are
bit-for-bit identical. Before this, rav1e and av-scenechange were never
cache hits on a fresh output base: their build scripts echo
CARGO_ENCODED_RUSTFLAGS back as `cargo:rustc-env`, and that value carries
`--remap-path-prefix=<exec_root>=.`. The rules_rust build-script runner
shipped by rules_rs only redacts the exec root when a `/` or `\` follows it,
so the per-action sandbox path leaked into `_bs.env`. A local patch also
redacts it before `=`.

The response-file bootstrap patch is dropped: the pinned rules_rs already
includes the upstream fix (hermeticbuild/rules_rust#53), and without the
patch process_wrapper is byte-identical across output bases and embeds no
absolute paths.

.bazelrc:
- Disable sandbox networking. Every toolchain is downloaded and
  checksummed; the full `//...` and `-c opt` APK/WASM builds pass offline.
- Never autodetect a host C/C++ toolchain; @llvm is the only one.
- Sandbox persistent workers (Javac, Kotlin, Android tools), which otherwise
  run in the unsandboxed execroot.
- Share a disk cache across checkouts and worktrees, garbage collected at
  60G. Action keys are path-independent, so a fresh worktree reuses every
  other worktree's outputs. CI keeps using its output base and BuildBuddy.
- CI: fail on a stale MODULE.bazel.lock and download only what each step
  inspects.

Pipelined Rust compilation was measured and left off: on clean 24-core
builds it was slower for both `//...` (110s vs 97s) and the `-c opt`
APK/WASM build (184s vs 168s), since the builds are CPU-bound rather than
critical-path-bound.

* ci: request a larger BuildBuddy workflow VM

The default Linux workflow VM (3 CPU, 8GB RAM, 20GB disk) ran out of disk
in the -c opt APK step: the Android SDK/NDK, LLVM, Flutter and JDK
repositories plus the fastbuild and opt output trees on a recycled runner
exceed 20GB. More CPUs also shorten every remote-cache miss.

Drop --remote_download_toplevel from the buildbuddy config: it is Bazel's
default, and with --config=ci adding --remote_download_minimal it only
produced a conflicting-expansion warning.

* ci: describe the VM sizing for the single opt build step

Since main's #14, CI builds and tests only in -c opt, so there is no
separate fastbuild output tree.
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.

1 participant