Skip to content

feat: Optimize TrimSpace#166

Merged
ReneWerner87 merged 4 commits intomasterfrom
trimSpace_optimization
Dec 4, 2025
Merged

feat: Optimize TrimSpace#166
ReneWerner87 merged 4 commits intomasterfrom
trimSpace_optimization

Conversation

@ReneWerner87
Copy link
Member

@ReneWerner87 ReneWerner87 commented Dec 4, 2025

                                            │   old.txt    │               new.txt               │
                                            │    sec/op    │    sec/op     vs base                │
_TrimSpaceBytes/fiber/empty-12                0.3391n ± 1%   0.3729n ± 2%   +9.97% (p=0.000 n=20)
_TrimSpaceBytes/fiber/spaces-12                2.754n ± 1%    3.315n ± 1%  +20.33% (p=0.000 n=20)
_TrimSpaceBytes/fiber/ascii-word-12            4.780n ± 1%    3.843n ± 2%  -19.61% (p=0.000 n=20)
_TrimSpaceBytes/fiber/auth-header-bearer-12    5.464n ± 1%    4.427n ± 2%  -18.97% (p=0.000 n=20)
_TrimSpaceBytes/fiber/auth-header-basic-12     4.139n ± 1%    3.232n ± 1%  -21.89% (p=0.000 n=20)
_TrimSpaceBytes/fiber/accept-encoding-12       4.727n ± 1%    4.010n ± 2%  -15.15% (p=0.000 n=20)
_TrimSpaceBytes/fiber/content-type-12          4.748n ± 1%    3.943n ± 1%  -16.95% (p=0.000 n=20)
_TrimSpaceBytes/fiber/x-forwarded-for-12       3.831n ± 0%    3.010n ± 1%  -21.43% (p=0.000 n=20)
_TrimSpaceBytes/fiber/query-params-12          4.702n ± 1%    3.882n ± 1%  -17.44% (p=0.000 n=20)
_TrimSpaceBytes/fiber/ascii-long-12            5.620n ± 2%    4.785n ± 1%  -14.85% (p=0.000 n=20)
_TrimSpaceBytes/fiber/no-trim-12               2.358n ± 1%    1.781n ± 1%  -24.45% (p=0.000 n=20)
_TrimSpaceBytes/fiber/mixed-whitespace-12      5.312n ± 1%    4.417n ± 1%  -16.84% (p=0.000 n=20)
geomean                                        3.453n         2.971n       -13.96%

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @ReneWerner87, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on enhancing the performance of the TrimSpace function, which is responsible for removing leading and trailing ASCII whitespace from byte sequences. The core change involves a significant refactoring of the trimming logic to use more direct character comparisons, thereby eliminating the need for a dedicated whitespace lookup table. This optimization aims to make whitespace trimming operations faster and more efficient.

Highlights

  • Optimized TrimSpace Function: The TrimSpace function in byteseq.go has been completely rewritten to improve performance, especially for ASCII strings. The new implementation uses direct character comparisons for whitespace detection, replacing the previous lookup table approach.
  • Removed Whitespace Lookup Table: The whitespaceTable variable in common.go, which was previously used by the TrimSpace function for identifying whitespace characters, has been removed as it is no longer needed by the optimized implementation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@codecov
Copy link

codecov bot commented Dec 4, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.31%. Comparing base (f6c45bc) to head (be229ff).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #166      +/-   ##
==========================================
- Coverage   83.48%   83.31%   -0.18%     
==========================================
  Files          11       11              
  Lines         763      755       -8     
==========================================
- Hits          637      629       -8     
  Misses        115      115              
  Partials       11       11              
Flag Coverage Δ
unittests 83.31% <100.00%> (-0.18%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request introduces an optimized TrimSpace function in byteseq.go that is designed to be faster for ASCII strings by directly checking character values instead of using a lookup table. This change also allows for the removal of the whitespaceTable from common.go, which is a good cleanup. The new implementation correctly handles edge cases like empty strings and strings consisting entirely of whitespace. The optimization appears sound and maintains the intended functionality for ASCII whitespace trimming.

@ReneWerner87 ReneWerner87 marked this pull request as ready for review December 4, 2025 13:16
@ReneWerner87 ReneWerner87 requested a review from a team as a code owner December 4, 2025 13:16
@ReneWerner87 ReneWerner87 requested review from Copilot, efectn, gaby and sixcolors and removed request for a team December 4, 2025 13:16
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes the TrimSpace function by simplifying its implementation while maintaining correctness and improving performance. The benchmark results show performance improvements ranging from 14-26% for most realistic use cases (ASCII strings with actual content), with minor regressions only for empty strings and strings consisting entirely of whitespace.

Key Changes:

  • Simplified the trimming logic by combining the boundary search into two compact loops
  • Removed intermediate variables and conditional checks for cleaner, more efficient code
  • Maintained compatibility with the original behavior including edge cases like all-whitespace strings

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 4, 2025

Walkthrough

The TrimSpace function in byteseq.go is refactored to use a two-index algorithm with direct in-place trimming logic. The empty input check is updated, search patterns for non-whitespace characters are modified, and the function now consistently returns a subslice constructed from both indices rather than conditionally.

Changes

Cohort / File(s) Summary
TrimSpace refactor
byteseq.go
Reimplemented to use indices i and j for direct trimming, changed empty check from length == 0 to j < 0, updated search conditions, and always return s[i : j+1]

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Verify functional equivalence between old and new implementations
  • Check boundary conditions (empty strings, whitespace-only strings, strings with leading/trailing whitespace)
  • Confirm the index logic (i <= j and i < j conditions) is correct

Possibly related PRs

Suggested reviewers

  • sixcolors
  • efectn
  • gaby

Poem

🐰 Hop, skip, and trim away,
Two indices lead the way,
Old bounds gave space, now clean and tight,
The byte sequence dances right!

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Title check ✅ Passed The title 'feat: Optimize TrimSpace' directly and concisely summarizes the main change, which is an optimization to the TrimSpace function in byteseq.go.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch trimSpace_optimization

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
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 (1)
byteseq.go (1)

95-100: Consider checking empty before computing indices to recover performance.

The benchmark shows a +15.78% regression for the empty input case. This is likely because j = len(s) - 1 is computed before checking if the string is empty.

Apply this diff to optimize the empty case:

 func TrimSpace[S byteSeq](s S) S {
+	// fast path for empty string
+	if len(s) == 0 {
+		return s
+	}
+
 	i, j := 0, len(s)-1
 
-	// fast path for empty string
-	if j < 0 {
-		return s
-	}
-
 	// Find first non-whitespace from start
 	for ; i <= j && whitespaceTable[s[i]]; i++ { //nolint:revive // we want to check for multiple whitespace chars
 	}

This avoids the subtraction for empty inputs and uses a more direct check, which should recover the performance loss.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f6c45bc and be229ff.

📒 Files selected for processing (1)
  • byteseq.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Agent
  • GitHub Check: Compare
🔇 Additional comments (1)
byteseq.go (1)

94-111: Good optimization for typical use cases.

The refactored implementation shows solid performance improvements for realistic inputs (14-26% faster for strings with content), which is where optimization matters most. The algorithm is correct, and the two-index approach maintains consistency with the Trim function, improving maintainability.

The performance regression on edge cases (empty +15.78%, spaces +23.73%) is a reasonable trade-off given the improvements elsewhere, though the empty case can be recovered with the suggested optimization above.

Copy link
Member

@gaby gaby left a comment

Choose a reason for hiding this comment

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

Lgtm

@gaby gaby changed the title optimize TrimSpace feat: Optimize TrimSpace Dec 4, 2025
@ReneWerner87 ReneWerner87 merged commit 71424ca into master Dec 4, 2025
30 checks passed
@ReneWerner87 ReneWerner87 deleted the trimSpace_optimization branch December 4, 2025 13:35
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.

3 participants