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

utils: ConvertToBytes #1875

Merged
merged 5 commits into from Apr 24, 2022
Merged

Conversation

vanodevium
Copy link
Contributor

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.

@welcome
Copy link

welcome bot commented Apr 22, 2022

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?$`)
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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

ReneWerner87 and others added 4 commits April 24, 2022 11:08
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
@ReneWerner87 ReneWerner87 merged commit 63391d4 into gofiber:master Apr 24, 2022
@welcome
Copy link

welcome bot commented Apr 24, 2022

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

@ReneWerner87 ReneWerner87 linked an issue Apr 24, 2022 that may be closed by this pull request
@vanodevium vanodevium deleted the utils-convert-to-bytes branch April 24, 2022 14:54
trim21 pushed a commit to trim21/fiber that referenced this pull request Aug 15, 2022
* 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>
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.

🚀 Human readable BodyLimit
3 participants