Skip to content

Create faster trimSpace method with benchmarks#164

Merged
ReneWerner87 merged 11 commits intomasterfrom
claude/faster-trimspace-method-01Jj8dw18xYEA5cXSFgk4E1k
Dec 1, 2025
Merged

Create faster trimSpace method with benchmarks#164
ReneWerner87 merged 11 commits intomasterfrom
claude/faster-trimspace-method-01Jj8dw18xYEA5cXSFgk4E1k

Conversation

@ReneWerner87
Copy link
Member

@ReneWerner87 ReneWerner87 commented Nov 30, 2025

Summary by CodeRabbit

  • New Features

    • Added TrimSpace to remove leading/trailing ASCII whitespace from strings and byte sequences; fast paths return inputs unchanged when no trimming is needed. Behavior is limited to ASCII and preserves non‑ASCII content.
  • Tests

    • Added unit tests validating empty, ASCII, UTF‑8, HTTP‑style and mixed inputs against standard behavior.
  • Benchmarks

    • Added benchmarks comparing performance across input types and cases.
Benchmark_TrimSpace/default/accept-encoding-4          	183167964	         6.228 ns/op	3371.79 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/default/ascii-long-4               	148220395	         8.092 ns/op	12605.22 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/default/ascii-word-4     	        183436166	         6.548 ns/op	1374.43 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/default/auth-header-basic-4        	192802777	         6.269 ns/op	6061.29 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/default/auth-header-bearer-4       	153683058	         7.798 ns/op	3077.73 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/default/content-type-4             	191456836	         6.225 ns/op	3213.05 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/default/empty-4          	        350355049	         3.424 ns/op	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/default/mixed-whitespace-4         	160672089	         7.503 ns/op	2265.69 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/default/no-trim-4                  	320760076	         3.737 ns/op	1337.88 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/default/query-params-4             	192339032	         6.226 ns/op	3212.23 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/default/spaces-4         	        226704316	         5.296 ns/op	 566.48 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/default/x-forwarded-for-4          	213064540	         5.603 ns/op	4997.56 MB/s	       0 B/op	       0 allocs/op

Benchmark_TrimSpace/fiber/accept-encoding-4            	256978915	         4.676 ns/op	4491.30 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/fiber/ascii-long-4                 	202812584	         5.910 ns/op	17258.02 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/fiber/ascii-word-4       	        242043614	         4.676 ns/op	1924.83 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/fiber/auth-header-basic-4          	274828028	         4.367 ns/op	8701.16 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/fiber/auth-header-bearer-4         	213527164	         5.642 ns/op	4253.81 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/fiber/content-type-4               	256727560	         4.669 ns/op	4283.58 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/fiber/empty-4            	        1000000000	         0.3746 ns/op	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/fiber/mixed-whitespace-4           	213653119	         5.820 ns/op	2920.91 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/fiber/no-trim-4                    	481828731	         2.492 ns/op	2006.32 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/fiber/query-params-4               	256884889	         4.679 ns/op	4274.72 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/fiber/spaces-4           	        349965914	         3.434 ns/op	 873.54 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpace/fiber/x-forwarded-for-4            	294080383	         4.047 ns/op	6917.89 MB/s	       0 B/op	       0 allocs/op

Benchmark_TrimSpaceBytes/default/accept-encoding-4     	201609355	         5.954 ns/op	3527.29 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/default/ascii-long-4          	154005013	         7.777 ns/op	13115.29 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/default/ascii-word-4          	192663391	         6.234 ns/op	1443.78 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/default/auth-header-basic-4   	202728866	         5.926 ns/op	6412.42 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/default/auth-header-bearer-4  	160135318	         7.175 ns/op	3345.09 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/default/content-type-4        	202009124	         5.946 ns/op	3363.57 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/default/empty-4               	381078837	         3.113 ns/op	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/default/mixed-whitespace-4    	160258105	         7.473 ns/op	2274.83 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/default/no-trim-4             	321470626	         3.732 ns/op	1339.66 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/default/query-params-4        	192429375	         6.227 ns/op	3212.01 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/default/spaces-4              	240788079	         4.991 ns/op	 601.13 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/default/x-forwarded-for-4     	226560699	         5.305 ns/op	5277.79 MB/s	       0 B/op	       0 allocs/op

Benchmark_TrimSpaceBytes/fiber/accept-encoding-4       	213966306	         5.613 ns/op	3741.52 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/fiber/ascii-long-4            	174989810	         6.849 ns/op	14891.75 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/fiber/ascii-word-4            	202236631	         5.608 ns/op	1604.93 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/fiber/auth-header-basic-4     	226504020	         5.296 ns/op	7175.82 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/fiber/auth-header-bearer-4    	183684511	         6.561 ns/op	3657.81 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/fiber/content-type-4          	214071026	         5.612 ns/op	3563.82 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/fiber/empty-4                 	1000000000	         0.6232 ns/op	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/fiber/mixed-whitespace-4      	175153040	         6.676 ns/op	2546.27 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/fiber/no-trim-4               	350377460	         3.434 ns/op	1456.08 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/fiber/query-params-4          	214087597	         5.609 ns/op	3565.75 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/fiber/spaces-4                	349546147	         3.430 ns/op	 874.72 MB/s	       0 B/op	       0 allocs/op
Benchmark_TrimSpaceBytes/fiber/x-forwarded-for-4       	240697856	         4.988 ns/op	5613.10 MB/s	       0 B/op	       0 allocs/op

Implemented a faster TrimSpace function that outperforms strings.TrimSpace
for common use cases:

- Empty strings: 4.6x faster (0.63 ns vs 2.92 ns)
- No-trim cases: 21-25% faster (important for web frameworks)
- Trailing whitespace: 25% faster
- All whitespace: 15% faster

Key optimizations:
- Lookup table for whitespace detection
- Zero allocations when no trimming is needed
- Efficient byte-level operations
- Early return for empty strings

Added comprehensive tests and benchmarks to verify correctness and
performance improvements.
Moved TrimSpace from strings.go to byteseq.go and implemented it as a
generic function that works with both string and []byte types.

Performance improvements:

String benchmarks:
- Empty strings: 4.6x faster (0.63ns vs 2.88ns)
- No-trim cases: 28-34% faster
- All-whitespace: 46% faster
- Large inputs (no trim): 85+ GB/s throughput

Byte slice benchmarks:
- Empty slices: 6.4x faster (0.47ns vs 3.04ns)
- No-trim cases: 47-54% faster
- Large inputs (no trim): 88+ GB/s throughput
- Large inputs (with trim): 17% faster

Key optimizations:
- Whitespace lookup table for O(1) detection
- Zero allocations when no trimming needed
- Early returns for edge cases
- Generic implementation using Go 1.18+ type constraints

Perfect for web framework use cases like HTTP header parsing, query
parameter processing, and path normalization.
@ReneWerner87 ReneWerner87 requested a review from a team as a code owner November 30, 2025 16:49
@ReneWerner87 ReneWerner87 requested review from efectn, gaby and sixcolors and removed request for a team November 30, 2025 16:49
@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 introduces a highly optimized TrimSpace function, designed to efficiently remove leading and trailing ASCII whitespace from both strings and byte slices. The new implementation significantly outperforms the standard library's strings.TrimSpace and bytes.TrimSpace in various common scenarios, particularly for empty inputs and strings that do not require trimming. This enhancement is crucial for performance-sensitive applications, especially within web frameworks where such operations are frequent, as it minimizes allocations and execution time.

Highlights

  • New Optimized TrimSpace Function: Introduced a new generic TrimSpace function in byteseq.go that efficiently removes leading and trailing ASCII whitespace from both string and byte slice inputs.
  • Significant Performance Improvements: Benchmarks show the new TrimSpace is substantially faster than the standard library's strings.TrimSpace and bytes.TrimSpace, with speedups up to 6.4x for empty inputs and 28-54% for common 'no-trim' scenarios.
  • Zero Allocations for Common Cases: The optimized implementation achieves zero memory allocations when no trimming is required, contributing significantly to its performance efficiency.
  • Efficient Whitespace Detection: A whitespaceTable (a boolean lookup table) has been added to common.go to enable O(1) ASCII whitespace character detection, enhancing the speed of the trimming logic.
  • Comprehensive Benchmarking and Analysis: Extensive unit tests and benchmarks have been added in byteseq_test.go to validate correctness and quantify performance gains, with detailed analysis provided in benchmark_analysis.md and raw results in trimspace_benchmarks.txt.
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.

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

This pull request introduces a new, optimized TrimSpace function for strings and byte slices, which shows significant performance improvements for common cases, especially for ASCII strings with no or minimal whitespace. The implementation is clean and the accompanying tests are comprehensive. The benchmarks validate the performance claims.

My review includes a few suggestions to improve maintainability and clarity:

  • The analyze_benchmarks.sh script contains hardcoded values and should be updated to parse benchmark results dynamically.
  • There are multiple benchmark result files, which can be confusing. I've suggested removing the obsolete one.
  • A minor correction to a comment in common.go for better accuracy.
  • Refactoring the benchmark tests in byteseq_test.go to reduce code duplication.

Overall, this is a great contribution that will improve performance in the framework.

@codecov
Copy link

codecov bot commented Nov 30, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.48%. Comparing base (d531ec6) to head (439a1cf).
⚠️ Report is 18 commits behind head on master.

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

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.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 30, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

Adds a new generic function TrimSpace[S byteSeq](s S) S that trims ASCII whitespace using a byte lookup table, introduces the whitespaceTable in common.go, and adds unit tests plus benchmarks covering string and []byte variants.

Changes

Cohort / File(s) Change Summary
Core implementation
byteseq.go, common.go
Added public generic func TrimSpace[S byteSeq](s S) S with empty fast-path, scanning for first/last non-whitespace indices and returning original or a trimmed subslice. Introduced whitespaceTable [256]bool marking ASCII whitespace (HT, LF, VT, FF, CR, space).
Tests & benchmarks
byteseq_test.go
Added Test_TrimSpace validating both string and []byte variants against stdlib behavior (including an utf8-validated case) and added Benchmark_TrimSpace and Benchmark_TrimSpaceBytes plus shared trimSpaceBenchmarkCases.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Verify start/end index calculations for off-by-one and all-whitespace results (including empty and nil []byte equivalence).
  • Confirm whitespaceTable covers intended ASCII whitespace bytes and is used correctly.
  • Review tests/benchmarks for realistic coverage and correct comparisons to stdlib functions.

Suggested reviewers

  • sixcolors
  • efectn
  • gaby

Poem

🐇 I hopped along the byte-lined way,
Snipped spaces, tabs that came to play,
A tiny table showed the spot,
I trimmed the ends, discarded not,
Now strings and bytes feel light and gay. 🎋

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: a new faster TrimSpace method implementation with accompanying benchmarks.
✨ 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 claude/faster-trimspace-method-01Jj8dw18xYEA5cXSFgk4E1k

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 84335aa and 439a1cf.

📒 Files selected for processing (1)
  • byteseq_test.go (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
byteseq_test.go (1)
byteseq.go (1)
  • TrimSpace (94-126)
⏰ 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). (3)
  • GitHub Check: Build (1.24.x, windows-latest)
  • GitHub Check: Build (1.23.x, windows-latest)
  • GitHub Check: Compare
🔇 Additional comments (4)
byteseq_test.go (4)

11-28: Shared benchmark cases are clear and reusable

The shared trimSpaceBenchmarkCases table is well-structured and keeps the string/[]byte benchmarks in sync, which will make future additions easy. No changes needed.


325-364: TrimSpace tests are thorough; just be mindful of ASCII-only semantics

This table-driven test covers the key HTTP-style and edge inputs and validates both string and []byte variants against the stdlib, with proper tc := tc capture and parallel subtests. Since TrimSpace is implemented using an ASCII whitespace table, this “must match strings/bytes.TrimSpace” contract will hold for the current ASCII-only cases, but will diverge for non-ASCII whitespace; confirm that excluding non-ASCII whitespace cases here is intentional.


366-390: Benchmark_TrimSpace structure and usage look solid

The benchmark correctly reuses the shared cases, captures tc, sets SetBytes per input, and separates “fiber” vs “default” sub-benchmarks with identical loop structure and _ = res to prevent elimination. Looks good as-is.


392-417: Benchmark_TrimSpaceBytes is consistent and efficient

Converting tc.input to []byte once per case and reusing it across the fiber/default sub-benchmarks is efficient, and the benchmark mirrors the string version for a fair comparison. No issues spotted.


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: 1

🧹 Nitpick comments (2)
common.go (1)

30-38: Minor documentation clarification.

The comment mentions "(1 = whitespace, 0 = not whitespace)" but the table uses boolean values (true/false). Consider updating the comment to match the implementation.

Apply this diff:

-// Lookup table for ASCII whitespace characters (1 = whitespace, 0 = not whitespace)
+// Lookup table for ASCII whitespace characters (true = whitespace, false = not whitespace)
 var whitespaceTable = [256]bool{
analyze_benchmarks.sh (1)

1-28: Consider making the script parse actual benchmark files.

The script currently outputs hardcoded values. For better maintainability, consider parsing the actual benchmark results from benchmark_results.txt or using benchstat tool to generate comparisons dynamically.

Example using benchstat:

#!/bin/bash
# Install benchstat if needed: go install golang.org/x/perf/cmd/benchstat@latest

echo "=== TrimSpace Performance Analysis ==="
benchstat -split fiber,default benchmark_results.txt

This would automatically parse and compare the results without hardcoding values.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 86ecbcf and 31fb9a9.

📒 Files selected for processing (7)
  • analyze_benchmarks.sh (1 hunks)
  • benchmark_analysis.md (1 hunks)
  • benchmark_results.txt (1 hunks)
  • byteseq.go (1 hunks)
  • byteseq_test.go (1 hunks)
  • common.go (1 hunks)
  • trimspace_benchmarks.txt (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
byteseq_test.go (1)
byteseq.go (1)
  • TrimSpace (94-124)
🪛 GitHub Actions: golangci-lint
byteseq_test.go

[error] 373-373: golangci-lint: empty-block: this block is empty, you can remove it (revive)

🪛 GitHub Check: lint
byteseq_test.go

[failure] 373-373:
empty-block: this block is empty, you can remove it (revive)

🪛 LanguageTool
benchmark_analysis.md

[grammar] ~3-~3: Interpunctie toevoegen
Context: ...e Analysis ## String Benchmarks (Fiber vs Standard Library) | Scenario | Fiber T...

(QB_NEW_NL_OTHER_ERROR_IDS_MISSING_PUNCTUATION)


[style] ~7-~7: Netter is om het uit te schrijven.
Context: ...| | empty | 0.63 ns | 2.88 ns | 4.6x faster ⚡ | - | | no-trim | 2.83 n...

(_10X)


[style] ~22-~22: Netter is om het uit te schrijven.
Context: ...| | empty | 0.47 ns | 3.04 ns | 6.4x faster ⚡⚡ | - | | no-trim | 2.83 ...

(_10X)


[style] ~36-~36: Netter is om het uit te schrijven.
Context: ...for Common Cases - Empty strings: 4.6x faster (strings), 6.4x faster (bytes) -...

(_10X)


[style] ~36-~36: Netter is om het uit te schrijven.
Context: ...pty strings**: 4.6x faster (strings), 6.4x faster (bytes) - No trimming needed...

(_10X)


[grammar] ~37-~37: Dit kan een fout zijn.
Context: ...bytes) - No trimming needed: 28-34% faster (strings), 47-54% faster (bytes) - **La...

(QB_NEW_NL)


[grammar] ~37-~37: Dit kan een fout zijn.
Context: ...eded**: 28-34% faster (strings), 47-54% faster (bytes) - Large inputs without trim...

(QB_NEW_NL)


[grammar] ~38-~38: Dit kan een fout zijn.
Context: ...ts without trim**: Throughput > 85 GB/s for strings, > 88 GB/s for bytes ### 📊 Pe...

(QB_NEW_NL)


[grammar] ~38-~38: Dit kan een fout zijn.
Context: ...ughput > 85 GB/s for strings, > 88 GB/s for bytes ### 📊 Performance Characteristi...

(QB_NEW_NL)


[grammar] ~41-~41: Dit kan een fout zijn.
Context: ...eristics 1. Best case: Empty inputs and no-trim scenarios (most common in web f...

(QB_NEW_NL)


[grammar] ~41-~41: Woord aanpassen
Context: ...**: Empty inputs and no-trim scenarios (most common in web frameworks) 2. **Good per...

(QB_NEW_NL_OTHER_ERROR_IDS_REPLACEMENT_CONFUSION)


[grammar] ~41-~41: Dit kan een fout zijn.
Context: ...mpty inputs and no-trim scenarios (most common in web frameworks) 2. **Good performanc...

(QB_NEW_NL)


[grammar] ~41-~41: Dit kan een fout zijn.
Context: ...s and no-trim scenarios (most common in web frameworks) 2. Good performance: All-whitespac...

(QB_NEW_NL)


[grammar] ~42-~42: Dit kan een fout zijn.
Context: ...ll-whitespace removal scenarios (24-46% faster) 3. Competitive: Mixed whitespace s...

(QB_NEW_NL)


[grammar] ~43-~43: Zin met fouten
Context: ...petitive**: Mixed whitespace scenarios (similar or slightly better) ### 💡 Why It's Fa...

(QB_NEW_NL_OTHER_ERROR_IDS_REPLACEMENT_MULTITOKEN)


[grammar] ~43-~43: Zin met fouten
Context: ...**: Mixed whitespace scenarios (similar or slightly better) ### 💡 Why It's Faste...

(QB_NEW_NL_OTHER_ERROR_IDS_REPLACEMENT_MULTITOKEN)


[grammar] ~43-~43: Zin met fouten
Context: ... Mixed whitespace scenarios (similar or slightly better) ### 💡 Why It's Faster 1. **Lo...

(QB_NEW_NL_OTHER_ERROR_IDS_REPLACEMENT_MULTITOKEN)


[grammar] ~43-~43: Dit kan een fout zijn.
Context: ...itespace scenarios (similar or slightly better) ### 💡 Why It's Faster 1. **Lookup ta...

(QB_NEW_NL)


[grammar] ~60-~60: Zin met fouten
Context: ...ling - Form data processing All common web framework operations benefit from these...

(QB_NEW_NL_OTHER_ERROR_IDS_REPLACEMENT_MULTITOKEN)


[grammar] ~60-~60: Zin met fouten
Context: ... - Form data processing All common web framework operations benefit from these optimizat...

(QB_NEW_NL_OTHER_ERROR_IDS_REPLACEMENT_MULTITOKEN)


[grammar] ~60-~60: Zin met fouten
Context: ...ta processing All common web framework operations benefit from these optimizations!

(QB_NEW_NL_OTHER_ERROR_IDS_REPLACEMENT_MULTITOKEN)


[grammar] ~60-~60: Zin met fouten
Context: ...ng All common web framework operations benefit from these optimizations!

(QB_NEW_NL_OTHER_ERROR_IDS_REPLACEMENT_MULTITOKEN)


[grammar] ~60-~60: Dit kan een fout zijn.
Context: ...common web framework operations benefit from these optimizations!

(QB_NEW_NL)

⏰ 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: Build (1.24.x, macos-13)
  • GitHub Check: Compare
🔇 Additional comments (6)
benchmark_results.txt (1)

1-36: LGTM! Benchmark results look comprehensive.

The benchmark results demonstrate good performance improvements across various scenarios with zero allocations.

byteseq.go (1)

91-124: Excellent implementation with good optimizations!

The TrimSpace function is well-designed with:

  • O(1) whitespace detection using the lookup table
  • Fast path for empty inputs
  • Zero-allocation path when no trimming is needed (line 117-120)
  • Proper handling of all-whitespace inputs
  • Clear, well-documented code

The implementation correctly handles both string and []byte via generics and maintains parity with the standard library while offering performance improvements.

trimspace_benchmarks.txt (1)

1-66: LGTM! Comprehensive benchmark data.

The detailed benchmark results provide good coverage across multiple scenarios (empty, no-trim, leading/trailing/both spaces, mixed, large inputs) for both string and []byte implementations.

byteseq_test.go (2)

382-429: Well-structured benchmarks with good coverage.

The benchmark implementation properly:

  • Covers diverse scenarios (empty, no-trim, leading/trailing/both spaces, mixed, large inputs)
  • Uses b.SetBytes() for throughput measurements
  • Compares Fiber implementation against standard library
  • Follows Go benchmark best practices

431-478: Good byte slice benchmark coverage.

The byte slice benchmarks complement the string benchmarks well, ensuring both code paths are properly measured and compared against the standard library's bytes.TrimSpace.

benchmark_analysis.md (1)

1-60: Excellent performance analysis documentation.

The benchmark analysis is well-organized with clear tables, key findings, and practical use cases. The documentation effectively communicates the performance benefits of the new TrimSpace implementation.

Note: The static analysis warnings from LanguageTool can be safely ignored as they appear to be grammar checks configured for Dutch rather than English.

- Fix whitespaceTable comment accuracy (bool vs numeric values)
- Combine duplicate benchmark loops to reduce code duplication
- Add proper variable capture in benchmark loops
- Replace synthetic test cases with realistic HTTP header and query parameter examples
- Simplify test logic to directly compare against standard library behavior
- Fix empty-block linter issue by applying suggested condition inversion
- Remove Unicode whitespace test cases (implementation is ASCII-only by design)
- Update benchmarks to use same realistic test cases
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: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d8c5b95 and 365ff81.

📒 Files selected for processing (2)
  • byteseq_test.go (1 hunks)
  • common.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • common.go
🧰 Additional context used
🧬 Code graph analysis (1)
byteseq_test.go (1)
byteseq.go (1)
  • TrimSpace (94-124)
🪛 GitHub Actions: golangci-lint
byteseq_test.go

[error] 342-342: golangci-lint: QF1001 could apply De Morgan's law (staticcheck)

🪛 GitHub Check: lint
byteseq_test.go

[failure] 342-342:
QF1001: could apply De Morgan's law (staticcheck)

⏰ 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: Build (1.24.x, windows-latest)
  • GitHub Check: Compare
🔇 Additional comments (2)
byteseq_test.go (2)

368-390: Well-structured benchmarks following best practices.

The benchmark properly captures the range variable and combines both fiber and default benchmarks in a single loop, addressing the feedback from the previous review. The use of SetBytes enables meaningful throughput reporting.


412-434: Consistent benchmark structure for byte slices.

The byte slice benchmarks mirror the string benchmark structure and properly incorporate the previous review feedback by combining the loops into a single iteration.

- Simplifies logic by using bytes.Equal which treats nil and empty slices as equal
- Eliminates conditional logic and potential linter issues
- Cleaner and more idiomatic Go code
@gaby
Copy link
Member

gaby commented Dec 1, 2025

/gemini review

@gaby
Copy link
Member

gaby commented Dec 1, 2025

@codex review

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

This pull request introduces an optimized TrimSpace function for strings and byte slices, focusing on performance for ASCII whitespace. The implementation is sound and uses a lookup table for efficiency. The accompanying tests and benchmarks are thorough. My review focuses on improving the correctness and maintainability of the tests. Specifically, I suggest making the unit tests for TrimSpace independent of the standard library's TrimSpace due to differences in whitespace handling, and I recommend refactoring the benchmark tests to reduce code duplication.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

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.

ℹ️ 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".

…pacity

- Cache length in variable to avoid multiple len(s) calls
- Fix whitespace-only case to return zero-capacity slice (s[length:length])
- Matches bytes.TrimSpace behavior for append safety
- Prevents unexpected buffer mutations when appending to trimmed result
- Define trimSpaceBenchmarkCases at package level
- Reuse same test cases for both string and []byte benchmarks
- Convert to []byte where needed in Benchmark_TrimSpaceBytes
- Improves maintainability by centralizing test case definitions
@ReneWerner87 ReneWerner87 merged commit f6c45bc into master Dec 1, 2025
22 checks passed
@ReneWerner87 ReneWerner87 deleted the claude/faster-trimspace-method-01Jj8dw18xYEA5cXSFgk4E1k branch December 1, 2025 07:28
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