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

Add AppendInt function with positive/negative number convertion to string #90

Closed
wants to merge 5 commits into from

Conversation

ReneWerner87
Copy link
Member

@ReneWerner87 ReneWerner87 commented Jul 21, 2024

Benchmark_ItoA/fiber             (pos_num)-12  186969812        6.25 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/fiber             (pos_num)-12  193965686        6.16 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/strconv.Itoa      (pos_num)-12   80716807       14.42 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/strconv.Itoa      (pos_num)-12   80445802       14.85 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt_(pos_num)-12   81137728       14.52 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt_(pos_num)-12   81345360       14.51 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/fiber             (neg_num)-12  192902048        6.18 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/fiber             (neg_num)-12  194245189        6.14 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/strconv.Itoa      (neg_num)-12   84505304       13.55 ns/op       5 B/op       1 allocs/op
Benchmark_ItoA/strconv.Itoa      (neg_num)-12   82524801       13.54 ns/op       5 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt (neg_num)-12   84884136       13.63 ns/op       5 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt (neg_num)-12   85829492       13.63 ns/op       5 B/op       1 allocs/op

Summary by CodeRabbit

  • New Features

    • Introduced a function to append integer string representations to byte slices, supporting both positive and negative integers.
    • Added comprehensive benchmark tests for integer-to-string conversion, enhancing performance analysis capabilities.
  • Bug Fixes

    • Minor formatting adjustment in comment lines for improved clarity.
  • Tests

    • Added new test cases to validate the functionality of the new integer appending feature.
    • Introduced benchmark tests comparing the performance of the new function against standard library methods.

…ring

Benchmark_ItoA/fiber             (pos_num)-12  190428255        6.13 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/fiber             (pos_num)-12  196626811        6.08 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/strconv.Itoa      (pos_num)-12   80716807       14.33 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/strconv.Itoa      (pos_num)-12   80445802       14.30 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt_(pos_num)-12   81137728       14.30 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt_(pos_num)-12   81345360       14.49 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/fiber             (neg_num)-12  151121002        7.81 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/fiber             (neg_num)-12  153566410        7.70 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/strconv.Itoa      (neg_num)-12   84505304       13.75 ns/op       5 B/op       1 allocs/op
Benchmark_ItoA/strconv.Itoa      (neg_num)-12   82524801       13.76 ns/op       5 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt (neg_num)-12   84884136       13.88 ns/op       5 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt (neg_num)-12   85829492       13.76 ns/op       5 B/op       1 allocs/op
…ring

Benchmark_ItoA/fiber             (pos_num)-12  190428255        6.13 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/fiber             (pos_num)-12  196626811        6.08 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/strconv.Itoa      (pos_num)-12   80716807       14.33 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/strconv.Itoa      (pos_num)-12   80445802       14.30 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt_(pos_num)-12   81137728       14.30 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt_(pos_num)-12   81345360       14.49 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/fiber             (neg_num)-12  151121002        7.81 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/fiber             (neg_num)-12  153566410        7.70 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/strconv.Itoa      (neg_num)-12   84505304       13.75 ns/op       5 B/op       1 allocs/op
Benchmark_ItoA/strconv.Itoa      (neg_num)-12   82524801       13.76 ns/op       5 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt (neg_num)-12   84884136       13.88 ns/op       5 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt (neg_num)-12   85829492       13.76 ns/op       5 B/op       1 allocs/op
…ring

Benchmark_ItoA/fiber             (pos_num)-12  190428255        6.13 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/fiber             (pos_num)-12  196626811        6.08 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/strconv.Itoa      (pos_num)-12   80716807       14.33 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/strconv.Itoa      (pos_num)-12   80445802       14.30 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt_(pos_num)-12   81137728       14.30 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt_(pos_num)-12   81345360       14.49 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/fiber             (neg_num)-12  151121002        7.81 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/fiber             (neg_num)-12  153566410        7.70 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/strconv.Itoa      (neg_num)-12   84505304       13.75 ns/op       5 B/op       1 allocs/op
Benchmark_ItoA/strconv.Itoa      (neg_num)-12   82524801       13.76 ns/op       5 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt (neg_num)-12   84884136       13.88 ns/op       5 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt (neg_num)-12   85829492       13.76 ns/op       5 B/op       1 allocs/op
@ReneWerner87 ReneWerner87 requested a review from a team as a code owner July 21, 2024 16:28
@ReneWerner87 ReneWerner87 requested review from gaby, sixcolors and efectn and removed request for a team July 21, 2024 16:28
Copy link

coderabbitai bot commented Jul 21, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

The recent updates enhance integer-to-string conversion capabilities by introducing the new AppendInt function, which efficiently appends string representations of integers to byte slices for both positive and negative values. Additionally, expanded benchmark tests provide comparative performance metrics against standard library methods, improving analysis and optimization opportunities.

Changes

Files Change Summary
README.md Added a summary of changes related to new benchmarks for integer-to-string conversions.
convert.go Introduced AppendInt function for appending integer strings to byte slices.
convert_test.go Added Test_AppendInt to validate AppendInt functionality; introduced benchmarks comparing performance of AppendInt with standard library methods.

Poem

In the meadow, where rabbits play,
New functions hop in a cheerful way.
With integers bright, both big and small,
We append and test, ensuring they all stand tall!
Let's celebrate with a joyful cheer,
For performance gains are finally here! 🐇✨


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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

codecov bot commented Jul 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.48%. Comparing base (5aaa378) to head (ac59843).

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #90      +/-   ##
==========================================
+ Coverage   92.10%   92.48%   +0.37%     
==========================================
  Files           9        9              
  Lines         380      399      +19     
==========================================
+ Hits          350      369      +19     
  Misses         17       17              
  Partials       13       13              
Flag Coverage Δ
unittests 92.48% <100.00%> (+0.37%) ⬆️

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.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5aaa378 and 9c0bea3.

Files ignored due to path filters (2)
  • go.mod is excluded by !**/*.mod
  • go.sum is excluded by !**/*.sum, !**/*.sum
Files selected for processing (3)
  • README.md (3 hunks)
  • convert.go (2 hunks)
  • convert_test.go (3 hunks)
Additional comments not posted (5)
convert.go (2)

14-15: Import statement for fasthttp package.

The fasthttp package is imported to facilitate the AppendInt function.


157-171: LGTM! But verify the usage of fasthttp.AppendUint.

The code changes are approved.

However, ensure that fasthttp.AppendUint handles the conversion as expected.

README.md (1)

63-74: Comprehensive benchmark tests for Benchmark_ItoA.

The benchmark tests are well-structured and provide valuable performance metrics for integer-to-string conversion methods.

convert_test.go (2)

236-247: Comprehensive test cases for AppendInt.

The test cases cover a wide range of inputs, ensuring comprehensive validation of the AppendInt function.


313-378: Comprehensive benchmark tests for Benchmark_ItoA.

The benchmark tests are well-structured and provide valuable performance metrics for integer-to-string conversion methods.

…ring

Benchmark_ItoA/fiber             (pos_num)-12  186969812        6.25 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/fiber             (pos_num)-12  193965686        6.16 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/strconv.Itoa      (pos_num)-12   80716807       14.42 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/strconv.Itoa      (pos_num)-12   80445802       14.85 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt_(pos_num)-12   81137728       14.52 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt_(pos_num)-12   81345360       14.51 ns/op       4 B/op       1 allocs/op
Benchmark_ItoA/fiber             (neg_num)-12  192902048        6.18 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/fiber             (neg_num)-12  194245189        6.14 ns/op       0 B/op       0 allocs/op
Benchmark_ItoA/strconv.Itoa      (neg_num)-12   84505304       13.55 ns/op       5 B/op       1 allocs/op
Benchmark_ItoA/strconv.Itoa      (neg_num)-12   82524801       13.54 ns/op       5 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt (neg_num)-12   84884136       13.63 ns/op       5 B/op       1 allocs/op
Benchmark_ItoA/strconv.FormatInt (neg_num)-12   85829492       13.63 ns/op       5 B/op       1 allocs/op
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: 2

Outside diff range, codebase verification and nitpick comments (2)
convert.go (2)

163-174: Performance: Optimize buffer usage.

The buffer size of 20 bytes is sufficient for 64-bit integers, but consider documenting this assumption for clarity.

// Buffer size of 20 bytes is sufficient for 64-bit integers
var b [20]byte

176-179: Readability: Improve comment clarity.

The comment can be more descriptive to explain the purpose of adding the '-' sign.

if isNegative {
	// Prepend '-' to indicate a negative number
	dst = append(dst, '-')
}
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9c0bea3 and ac59843.

Files selected for processing (2)
  • README.md (3 hunks)
  • convert.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • README.md

Comment on lines +156 to +161
func AppendInt(dst []byte, n int) []byte {
isNegative := n < 0
if isNegative {
// Convert the number to positive
n = -n
}
Copy link

Choose a reason for hiding this comment

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

Correctness: Handle integer overflow for negative values.

When converting a negative integer to positive, be cautious of integer overflow. The edge case for n = math.MinInt should be handled.

if isNegative {
	if n == math.MinInt {
		// Handle the edge case for the smallest possible integer
		dst = append(dst, "-9223372036854775808"...)
		return dst
	}
	n = -n
}

convert.go Show resolved Hide resolved
@@ -151,3 +151,34 @@ func ToString(arg any, timeFormat ...string) string {
return fmt.Sprint(arg)
}
}

// AppendInt appends the string representation of the int n to dst and returns the extended buffer.
func AppendInt(dst []byte, n int) []byte {
Copy link
Member

Choose a reason for hiding this comment

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

I think we need a separate function for AppendUint that uses uint as param, else we won't be able to handle values bigger than 2,147,483,647

Copy link
Member

Choose a reason for hiding this comment

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

The math module in Golang defines these:

MaxUint32 = 1<<32 - 1           // 4294967295
MaxUint64 = 1<<64 - 1           // 18446744073709551615

convert_test.go Outdated
require.Equal(b, []byte(numberString), resB)
})

b.Run("default - strconv.Itoa (positiv number)", func(b *testing.B) {
Copy link
Member

Choose a reason for hiding this comment

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

positive*

convert_test.go Outdated
require.Equal(b, numberString, resS)
})

b.Run("default - strconv.FormatInt (positiv number)", func(b *testing.B) {
Copy link
Member

Choose a reason for hiding this comment

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

positive*

@ReneWerner87
Copy link
Member Author

there is already a AppendInt function in strconv
https://pkg.go.dev/strconv#AppendInt

4 digit number
image

2 digit number
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants