Skip to content

Inline bi_reverse#1955

Merged
Dead2 merged 1 commit intodevelopfrom
bi_reverse-inline
Aug 23, 2025
Merged

Inline bi_reverse#1955
Dead2 merged 1 commit intodevelopfrom
bi_reverse-inline

Conversation

@Dead2
Copy link
Copy Markdown
Member

@Dead2 Dead2 commented Aug 22, 2025

It surprised me to find that this was not already being inlines, seeing how small and simple the function is.
Inlining it reduces the size of the library by ~160 bytes on x86-64.
Performance difference is very minimal, can't really tell for certain from the benchmarks, but logic says it should be a bit faster.

Develop

 Level   Comp   Comptime min/avg/max/stddev  Decomptime min/avg/max/stddev  Compressed size
 1     54.185%  0.0773/0.0776/0.0777/0.0001    0.0300/0.0308/0.0308/0.0001        8,526,745
 2     43.871%  0.1254/0.1257/0.1260/0.0002    0.0301/0.0302/0.0302/0.0000        6,903,702
 3     42.388%  0.1504/0.1508/0.1510/0.0002    0.0290/0.0290/0.0290/0.0000        6,670,239
 4     41.647%  0.1746/0.1751/0.1754/0.0002    0.0282/0.0283/0.0283/0.0000        6,553,746
 5     41.216%  0.1910/0.1913/0.1916/0.0002    0.0281/0.0281/0.0282/0.0000        6,485,936
 6     41.038%  0.2365/0.2369/0.2372/0.0002    0.0277/0.0278/0.0278/0.0000        6,457,827
 7     40.778%  0.3328/0.3334/0.3338/0.0003    0.0280/0.0281/0.0281/0.0000        6,416,941
 8     40.704%  0.4404/0.4409/0.4413/0.0003    0.0281/0.0281/0.0281/0.0000        6,405,249
 9     40.409%  0.5238/0.5244/0.5248/0.0003    0.0273/0.0273/0.0273/0.0000        6,358,951

 avg1  42.915%                       0.2507                         0.0286
 tot                                67.6793                         7.7268       60,779,336

   text    data     bss     dec     hex filename
 147782    1344       8  149134   2468e libz-ng.so.2
 
compress_bench/compress_bench/1                 3706 ns         3706 ns       755372
compress_bench/compress_bench/8                 4011 ns         4011 ns       697152
compress_bench/compress_bench/16                4167 ns         4167 ns       672118
compress_bench/compress_bench/32                4472 ns         4472 ns       626409
compress_bench/compress_bench/64                4759 ns         4759 ns       588360
compress_bench/compress_bench/512               4824 ns         4824 ns       581876
compress_bench/compress_bench/4096              5321 ns         5321 ns       526197
compress_bench/compress_bench/32768             9500 ns         9501 ns       294534

PR

 Level   Comp   Comptime min/avg/max/stddev  Decomptime min/avg/max/stddev  Compressed size
 1     54.185%  0.0773/0.0775/0.0776/0.0001    0.0299/0.0308/0.0308/0.0002        8,526,745
 2     43.871%  0.1254/0.1258/0.1261/0.0002    0.0302/0.0302/0.0302/0.0000        6,903,702
 3     42.388%  0.1504/0.1508/0.1509/0.0001    0.0290/0.0291/0.0291/0.0000        6,670,239
 4     41.647%  0.1747/0.1751/0.1754/0.0002    0.0283/0.0283/0.0284/0.0000        6,553,746
 5     41.216%  0.1908/0.1912/0.1915/0.0002    0.0282/0.0282/0.0282/0.0000        6,485,936
 6     41.038%  0.2362/0.2366/0.2369/0.0002    0.0278/0.0278/0.0279/0.0000        6,457,827
 7     40.778%  0.3326/0.3333/0.3336/0.0002    0.0281/0.0281/0.0281/0.0000        6,416,941
 8     40.704%  0.4404/0.4408/0.4410/0.0002    0.0281/0.0282/0.0282/0.0000        6,405,249
 9     40.409%  0.5241/0.5245/0.5249/0.0002    0.0273/0.0273/0.0274/0.0000        6,358,951

 avg1  42.915%                       0.2506                         0.0287
 tot                                67.6674                         7.7416       60,779,336

   text    data     bss     dec     hex filename
 147622    1344       8  148974   245ee libz-ng.so.2

compress_bench/compress_bench/1                 3738 ns         3738 ns       749251
compress_bench/compress_bench/8                 4000 ns         4000 ns       700073
compress_bench/compress_bench/16                4152 ns         4152 ns       674635
compress_bench/compress_bench/32                4454 ns         4454 ns       627244
compress_bench/compress_bench/64                4735 ns         4735 ns       591568
compress_bench/compress_bench/512               4820 ns         4820 ns       581146
compress_bench/compress_bench/4096              5308 ns         5308 ns       528096
compress_bench/compress_bench/32768             9484 ns         9484 ns       295214

Summary by CodeRabbit

  • Refactor

    • Consolidated low-level bit handling into a shared helper, removing duplicate implementations and simplifying internal calls.
    • Streamlined compression internals for improved maintainability without changing behavior.
  • Chores

    • Updated internal includes to align with the new shared helper.
  • Notes

    • No user-facing changes or API impacts; functionality and results remain unchanged.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 22, 2025

Walkthrough

Centralizes the bit-reversal helper by introducing a static inline bi_reverse in deflate_p.h, updates callers to use bi_reverse directly, removes the old prototype from deflate.h, adds needed includes, and deletes the local implementation in trees.c. The s390 dfltcc path and maketrees tool are updated to include deflate_p.h and call bi_reverse.

Changes

Cohort / File(s) Summary
Centralize bi_reverse in deflate_p.h
deflate_p.h, trees.c, tools/maketrees.c, arch/s390/dfltcc_deflate.c
Add static inline bi_reverse and bitrev8 macro in deflate_p.h; include this header in trees.c, maketrees.c, and s390 dfltcc file; replace PREFIX(bi_reverse)(...) calls with bi_reverse(...); remove local bi_reverse implementation from trees.c.
Remove legacy prototype
deflate.h
Delete PREFIX(bi_reverse) prototype; no other declarations changed.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • nmoinvaz
  • KungFuJesus
  • phprus

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bi_reverse-inline

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
deflate_p.h (2)

100-111: Prevent macro leakage from header by scoping bitrev8 locally

Defining bitrev8 as a preprocessor macro inside a widely-included header leaks it to the entire translation unit. Even though redefinition with identical bodies is allowed, it’s easy to collide with another symbol or alter compile diagnostics. Prefer a header-local name and immediately undef it after use.

Apply this minimal, low-risk change to confine the helper:

 static inline uint16_t bi_reverse(unsigned code, int len) {
     /* code: the value to invert */
     /* len: its bit length */
     Assert(len >= 1 && len <= 15, "code length must be 1-15");
-#define bitrev8(b) \
-    (uint8_t)((((uint8_t)(b) * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32)
-    return (bitrev8(code >> 8) | (uint16_t)bitrev8(code) << 8) >> (16 - len);
+#define ZNG_BITREV8(b) \
+    (uint8_t)((((uint8_t)(b) * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32)
+    uint16_t r = (ZNG_BITREV8(code >> 8) | (uint16_t)ZNG_BITREV8(code) << 8) >> (16 - len);
+#undef ZNG_BITREV8
+    return r;
 }

100-111: Optional: leverage compiler bit-reversal builtins when available

Some compilers provide __builtin_bitreverse16, which can be faster and clearer. Keep the current path as a portable fallback.

Example (outside this hunk, for illustration):

/* Optional fast path */
#if defined(__has_builtin)
#  if __has_builtin(__builtin_bitreverse16)
    return (uint16_t)__builtin_bitreverse16((uint16_t)code) >> (16 - len);
#  endif
#endif
/* Fallback: existing implementation */
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between ba3cc63 and 59ef761.

📒 Files selected for processing (5)
  • arch/s390/dfltcc_deflate.c (2 hunks)
  • deflate.h (0 hunks)
  • deflate_p.h (1 hunks)
  • tools/maketrees.c (2 hunks)
  • trees.c (2 hunks)
💤 Files with no reviewable changes (1)
  • deflate.h
🧰 Additional context used
🧬 Code graph analysis (3)
tools/maketrees.c (1)
deflate_p.h (1)
  • bi_reverse (103-110)
arch/s390/dfltcc_deflate.c (1)
deflate_p.h (1)
  • bi_reverse (103-110)
trees.c (1)
deflate_p.h (1)
  • bi_reverse (103-110)
🔇 Additional comments (6)
tools/maketrees.c (2)

9-9: Including deflate_p.h here is reasonable

Bringing in the inline bi_reverse via deflate_p.h for the generator keeps a single source of truth. No concerns.


94-95: Direct call to inlined bi_reverse matches original semantics

Using bi_reverse((unsigned)n, 5) is correct for the static distance codes (5-bit fixed). Good change.

arch/s390/dfltcc_deflate.c (2)

18-18: Header inclusion aligns this TU with the new inline helper

Adding deflate_p.h is needed now that bi_reverse lives there. Looks good.


94-95: Validate param->eobl Range

I didn’t find any code path that initializes or checks the 4-bit eobl field before it’s passed to bi_reverse(). Since uint8_t eobl:4 can legally be 0–15, we need to ensure the hardware DFLTCC deflate always returns a non-zero length (1–15). If that guarantee isn’t documented in the hardware spec, add a defensive check or assertion immediately before the call to prevent a zero-length shift:

// arch/s390/dfltcc_deflate.c: around line 94
Z_ASSERT(param->eobl >= 1 && param->eobl <= 15);
send_bits(state,
          bi_reverse(param->eobs >> (15 - param->eobl), param->eobl),
          param->eobl,
          state->bi_buf,
          state->bi_valid);

• Declaration: arch/s390/dfltcc_common.h (uint8_t eobl : 4;)
• Usage: arch/s390/dfltcc_deflate.c (lines 94–95)

trees.c (2)

35-35: Including deflate_p.h here is appropriate

This replaces the former local implementation and keeps the helper centralized. No issues.


403-404: bi_reverse usage remains correct for Huffman code generation

Replacing PREFIX(bi_reverse) with the inlined bi_reverse preserves behavior. The code index increment (next_code[len]++) is unchanged and correct.

@codecov
Copy link
Copy Markdown

codecov Bot commented Aug 22, 2025

Codecov Report

❌ Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 81.96%. Comparing base (ba3cc63) to head (59ef761).
⚠️ Report is 4 commits behind head on develop.

Files with missing lines Patch % Lines
arch/s390/dfltcc_deflate.c 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1955      +/-   ##
===========================================
+ Coverage    81.47%   81.96%   +0.49%     
===========================================
  Files          162      162              
  Lines        13956    13960       +4     
  Branches      3120     3127       +7     
===========================================
+ Hits         11371    11443      +72     
+ Misses        1594     1553      -41     
+ Partials       991      964      -27     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nmoinvaz
Copy link
Copy Markdown
Member

Brings back memories, ... #948

@Dead2 Dead2 merged commit 4378e43 into develop Aug 23, 2025
284 of 292 checks passed
@Dead2 Dead2 mentioned this pull request Nov 5, 2025
@Dead2 Dead2 deleted the bi_reverse-inline branch November 29, 2025 22:48
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.

2 participants