-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- don't limit it to 32 bits - give it a proper name - don't over-allocate too much
- Loading branch information
1 parent
064124e
commit 1c85d43
Showing
4 changed files
with
77 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//go:build !amd64 && !arm64 && !ppc64 && !ppc64le && !s390x | ||
// +build !amd64,!arm64,!ppc64,!ppc64le,!s390x | ||
|
||
package fasthttp | ||
|
||
func roundUpForSliceCap(n int) int { | ||
if n <= 0 { | ||
return 0 | ||
} | ||
|
||
// Above 100MB, we don't round up as the overhead is too large. | ||
if n > 100*1024*1024 { | ||
return n | ||
} | ||
|
||
x := uint32(n - 1) | ||
x |= x >> 1 | ||
x |= x >> 2 | ||
x |= x >> 4 | ||
x |= x >> 8 | ||
x |= x >> 16 | ||
|
||
// Make sure we don't return 0 due to overflow, even on 32 bit systems | ||
if x >= uint32(math.MaxInt32) { | ||
return math.MaxInt32 | ||
} | ||
|
||
return int(x + 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//go:build amd64 || arm64 || ppc64 || ppc64le || s390x | ||
// +build amd64 arm64 ppc64 ppc64le s390x | ||
|
||
package fasthttp | ||
|
||
func roundUpForSliceCap(n int) int { | ||
if n <= 0 { | ||
return 0 | ||
} | ||
|
||
// Above 100MB, we don't round up as the overhead is too large. | ||
if n > 100*1024*1024 { | ||
return n | ||
} | ||
|
||
x := uint64(n - 1) | ||
x |= x >> 1 | ||
x |= x >> 2 | ||
x |= x >> 4 | ||
x |= x >> 8 | ||
x |= x >> 16 | ||
|
||
return int(x + 1) | ||
} |