Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flate: Cleanup & reduce casts #1050

Merged
merged 1 commit into from
Jan 30, 2025
Merged

flate: Cleanup & reduce casts #1050

merged 1 commit into from
Jan 30, 2025

Conversation

klauspost
Copy link
Owner

@klauspost klauspost commented Jan 30, 2025

Small improvement by reducing casts for matchlen.

Sanity check before/after...

file	out	level	insize	outsize	millis	mb/s
github-june-2days-2019.json	gzkp	1	6273951764	1073607045	16547	361.58
github-june-2days-2019.json	gzkp	1	6273951764	1073607045	16327	366.46

Summary by CodeRabbit

Release Notes

  • Performance Improvements

    • Enhanced type handling in compression methods to improve efficiency
    • Streamlined integer conversions in encoding processes
  • New Features

    • Added new byte manipulation functions in the le package
    • Introduced Load8, Store16, Store32, and Store64 utility methods
  • Technical Updates

    • Updated method signatures to use more consistent integer types
    • Refined type casting in compression level encoding methods

These changes optimize the compression and byte manipulation capabilities of the library, focusing on performance and type safety.

Small improvement by reducing casts for matchlen.
Copy link

coderabbitai bot commented Jan 30, 2025

📝 Walkthrough

Walkthrough

This pull request introduces type modifications across multiple files in the flate and le packages, primarily focusing on changing integer parameter types from int32 to int in various encoding methods. The changes affect the matchlen and matchlenLong methods in different compression levels, ensuring consistent type handling. Additionally, the internal/le package receives new utility functions for byte manipulation, including Load8, Store16, Store32, and Store64, which enhance byte-level operations.

Changes

File Change Summary
flate/fast_encoder.go Method signatures updated: matchlen and matchlenLong now accept int instead of int32
flate/level1.go Added t variable for offset calculations in Encode method
flate/level{2,3,4,6}.go Modified matchlenLong function calls to explicitly cast parameters to int
internal/le/unsafe_disabled.go Added new utility functions: Load8, Store16, Store32, Store64
internal/le/unsafe_enabled.go Added Load8 function for byte slice manipulation

Possibly related PRs

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

Documentation and Community

  • 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

@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 (3)
internal/le/unsafe_disabled.go (1)

29-41: LGTM! Store functions are correctly implemented.

The Store functions correctly use binary.LittleEndian for endianness handling, ensuring consistent behavior across platforms.

Consider adding examples in the documentation to show typical usage patterns:

// Store16 will store v at b.
//
// Example:
//
//	b := make([]byte, 2)
//	Store16(b, 0x1234) // b now contains [0x34, 0x12]
internal/le/unsafe_enabled.go (1)

11-16: LGTM! Efficient unsafe implementation.

The implementation correctly uses unsafe pointer arithmetic for optimal performance while maintaining the same behavior as the safe version.

Consider removing the commented-out previous implementations since they're no longer needed and might cause confusion:

-	//return binary.LittleEndian.Uint16(b[i:])
-	//return *(*uint16)(unsafe.Pointer(&b[i]))
flate/level1.go (1)

123-123: Consider using matchlen for small matches.

For small matches (< 8 bytes), using matchlen might be more efficient than matchlenLong as it avoids the function call overhead.

-			l := e.matchlenLong(int(s+4), int(t+4), src) + 4
+			// Use matchlen for small matches
+			l := e.matchlen(src[s+4:], src[t+4:]) + 4
+			if l >= 8 {
+				l = e.matchlenLong(int(s+4), int(t+4), src) + 4
+			}

Also applies to: 126-130

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e0f89a9 and e873918.

📒 Files selected for processing (9)
  • flate/fast_encoder.go (4 hunks)
  • flate/level1.go (6 hunks)
  • flate/level2.go (1 hunks)
  • flate/level3.go (1 hunks)
  • flate/level4.go (1 hunks)
  • flate/level5.go (6 hunks)
  • flate/level6.go (8 hunks)
  • internal/le/unsafe_disabled.go (1 hunks)
  • internal/le/unsafe_enabled.go (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • flate/level3.go
⏰ Context from checks skipped due to timeout of 90000ms (16)
  • GitHub Check: build (1.23.x, windows-latest)
  • GitHub Check: build (1.23.x, macos-latest)
  • GitHub Check: build (1.23.x, ubuntu-latest)
  • GitHub Check: build (1.22.x, windows-latest)
  • GitHub Check: build (1.22.x, macos-latest)
  • GitHub Check: build (1.22.x, ubuntu-latest)
  • GitHub Check: fuzz-zstd ("noasm,nounsafe")
  • GitHub Check: fuzz-s2 ("noasm,nounsafe")
  • GitHub Check: build (1.21.x, windows-latest)
  • GitHub Check: fuzz-zstd (nounsafe)
  • GitHub Check: fuzz-s2 (nounsafe)
  • GitHub Check: build (1.21.x, macos-latest)
  • GitHub Check: build (1.21.x, ubuntu-latest)
  • GitHub Check: fuzz-other ("noasm,nounsafe")
  • GitHub Check: fuzz-other (nounsafe)
  • GitHub Check: Analyze (go)
🔇 Additional comments (8)
internal/le/unsafe_disabled.go (1)

9-12: LGTM! Simple and correct implementation.

The Load8 function provides a straightforward and safe way to access bytes from a slice.

flate/level1.go (1)

80-80: LGTM! Improved readability with explicit offset calculation.

The introduction of variable t makes the offset calculation clearer and reduces repeated expressions.

Also applies to: 92-93

flate/level4.go (1)

130-130: LGTM! Consistent type casting.

The explicit casting to int aligns with the PR's goal of reducing unnecessary casts and maintains consistency with other level files.

flate/fast_encoder.go (2)

Line range hint 139-180: LGTM! Type change improves consistency.

The change from int32 to int parameters aligns better with Go's native integer type and reduces type conversions when working with slice lengths.


Line range hint 181-220: LGTM! Consistent with matchlen changes.

The parameter type change to int maintains consistency with matchlen and Go's native types.

flate/level2.go (1)

129-129: LGTM! Necessary type cast for compatibility.

The explicit casting to int is required to match the updated matchlenLong signature.

flate/level6.go (1)

127-128: LGTM! Consistent type casting throughout the file.

All calls to matchlen and matchlenLong have been updated with appropriate int casts to match the new method signatures.

Also applies to: 151-151, 165-165, 179-179, 190-190, 210-212, 230-230, 240-240

flate/level5.go (1)

122-123: LGTM! Consistent type casting throughout the file.

All calls to matchlen and matchlenLong have been updated with appropriate int casts to match the new method signatures.

Also applies to: 145-145, 157-157, 168-168, 188-190, 206-206

@klauspost klauspost merged commit 0bf3ecb into master Jan 30, 2025
22 checks passed
@klauspost klauspost deleted the flate-cast-cleanup branch January 30, 2025 15:58
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