-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
utils: ConvertToBytes #1875
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
utils: ConvertToBytes #1875
Conversation
Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
utils/common.go
Outdated
@@ -32,6 +40,8 @@ var ( | |||
uuidSeed [24]byte | |||
uuidCounter uint64 | |||
uuidSetup sync.Once | |||
unitsMap = map[string]int64{"k": kb, "m": mb, "g": gb, "t": tb, "p": pb} | |||
sizeRegex = regexp.MustCompile(`(?i)^(\d+(\.\d+)*) ?([kmgtp])?b?$`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need to use regex here?
Can you explain more about this regex? I do like to make many tests using strings instead regex.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(based on my strong experience in regexp patterns)
This regexp is simple pattern for extracting float value and unit name
So, something like:
(strict begin)(int or float value)(maybe space symbol)(maybe unit letter)(maybe b symbol)(string end)
So, any next examples are correct:
42
42m
42 m
42 Mb
42.5 Mb
Pattern is correct and many developers use it in different packages, frameworks and languages.
And, I really don't know how parse human readable value using strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i tried the string recognition
4491602
old:
Benchmark_ConvertToBytes/fiber-8 3541161 317.9 ns/op 128 B/op 2 allocs/op
Benchmark_ConvertToBytes/fiber-8 3465039 336.8 ns/op 128 B/op 2 allocs/op
new:
Benchmark_ConvertToBytes/fiber-12 32883782 33.76 ns/op 0 B/op 0 allocs/op
Benchmark_ConvertToBytes/fiber-12 36084900 33.47 ns/op 0 B/op 0 allocs/op
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@webdevium are you okay with the new code ? is now a little more, but also 10 times faster
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ReneWerner87 Awesome!
I don't have enough golang experience to do things like this without regular expressions :)
Thank you for your help.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ReneWerner87 But method has panic if human string is empty :(
Speed improvement old: Benchmark_ConvertToBytes/fiber-8 3541161 317.9 ns/op 128 B/op 2 allocs/op Benchmark_ConvertToBytes/fiber-8 3465039 336.8 ns/op 128 B/op 2 allocs/op new: Benchmark_ConvertToBytes/fiber-12 32883782 33.76 ns/op 0 B/op 0 allocs/op Benchmark_ConvertToBytes/fiber-12 36084900 33.47 ns/op 0 B/op 0 allocs/op
Congrats on merging your first pull request! 🎉 We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
* utils: ConvertToBytes * remove unneeded compare with golang function * remove unneeded compare with golang function * Update common_test.go * utils: ConvertToBytes Speed improvement old: Benchmark_ConvertToBytes/fiber-8 3541161 317.9 ns/op 128 B/op 2 allocs/op Benchmark_ConvertToBytes/fiber-8 3465039 336.8 ns/op 128 B/op 2 allocs/op new: Benchmark_ConvertToBytes/fiber-12 32883782 33.76 ns/op 0 B/op 0 allocs/op Benchmark_ConvertToBytes/fiber-12 36084900 33.47 ns/op 0 B/op 0 allocs/op Co-authored-by: RW <rene@gofiber.io>
According #1874
Utility method for converting human-readable strings into integer bytes.
Supports size syntax like docker or nginx configs.
Examples:
42m
42M
42MB
42 MB
Returns 0 if error occurred.