encoding/hex: speed up Encode
This CL manually unrolls Encode's loop in order to reduce the number of
bounds checks that the function incurs and speed up its execution. Here
are some benchmark results (no change to allocations):
goos: darwin
goarch: arm64
pkg: encoding/hex
cpu: Apple M4
│ old │ new │
│ sec/op │ sec/op vs base │
Encode/256-10 259.4n ± 0% 156.8n ± 0% -39.55% (p=0.000 n=20)
Encode/1024-10 997.0n ± 0% 626.0n ± 0% -37.21% (p=0.000 n=20)
Encode/4096-10 3.948µ ± 0% 2.478µ ± 0% -37.23% (p=0.000 n=20)
Encode/16384-10 15.750µ ± 0% 9.886µ ± 0% -37.23% (p=0.000 n=20)
geomean 2.003µ 1.245µ -37.82%
│ old │ new │
│ B/s │ B/s vs base │
Encode/256-10 941.1Mi ± 0% 1557.5Mi ± 0% +65.50% (p=0.000 n=20)
Encode/1024-10 979.5Mi ± 0% 1560.1Mi ± 0% +59.28% (p=0.000 n=20)
Encode/4096-10 989.5Mi ± 0% 1576.2Mi ± 0% +59.29% (p=0.000 n=20)
Encode/16384-10 992.1Mi ± 0% 1580.6Mi ± 0% +59.32% (p=0.000 n=20)
geomean 975.3Mi 1.532Gi +60.82%
diff --git a/src/encoding/hex/hex.go b/src/encoding/hex/hex.go
index 4499974..3a2a323 100644
--- a/src/encoding/hex/hex.go
+++ b/src/encoding/hex/hex.go
@@ -43,12 +43,38 @@
// of bytes written to dst, but this value is always [EncodedLen](len(src)).
// Encode implements hexadecimal encoding.
func Encode(dst, src []byte) int {
- j := 0
- for _, v := range src {
- dst[j] = hextable[v>>4]
- dst[j+1] = hextable[v&0x0f]
- j += 2
+ if len(src) == 0 {
+ return 0
}
+ i := 0
+ // Process the data in predictable chunks (4 src bytes, 8 dst bytes),
+ // to reduce the number of bounds checks.
+ for ; i < len(src)-3; i += 4 {
+ dst := dst[i*2 : i*2+8] // Eliminate bounds checks.
+ v0 := src[i]
+ dst[0] = hextable[v0>>4]
+ dst[1] = hextable[v0&0x0f]
+
+ v1 := src[i+1]
+ dst[2] = hextable[v1>>4]
+ dst[3] = hextable[v1&0x0f]
+
+ v2 := src[i+2]
+ dst[4] = hextable[v2>>4]
+ dst[5] = hextable[v2&0x0f]
+
+ v3 := src[i+3]
+ dst[6] = hextable[v3>>4]
+ dst[7] = hextable[v3&0x0f]
+ }
+
+ // Process the rest.
+ for ; i < len(src); i++ {
+ v := src[i]
+ dst[i*2] = hextable[v>>4]
+ dst[i*2+1] = hextable[v&0x0f]
+ }
+
return len(src) * 2
}
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
I spotted some possible problems with your PR:
1. You usually need to reference a bug number for all but trivial or cosmetic fixes. For this repo, the format is usually 'Fixes #12345' or 'Updates #12345' at the end of the commit message. Should you have a bug reference?
Please address any problems by updating the GitHub PR.
When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.
To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.
For more details, see:
(In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
I spotted some possible problems with your PR:
1. You usually need to reference a bug number for all but trivial or cosmetic fixes. For this repo, the format is usually 'Fixes #12345' or 'Updates #12345' at the end of the commit message. Should you have a bug reference?Please address any problems by updating the GitHub PR.
When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.
To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.
For more details, see:
- [how to update commit messages](https://go.dev/wiki/GerritBot/#how-does-gerritbot-determine-the-final-commit-message) for PRs imported into Gerrit.
- the Go project's [conventions for commit messages](https://go.dev/doc/contribute#commit_messages) that you should follow.
(In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)
Acknowledged
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Auto-Submit | +1 |
| Code-Review | +2 |
| Commit-Queue | +1 |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Auto-Submit | +1 |
| Code-Review | +2 |
| Commit-Queue | +1 |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
for i := 0; i < len(src); i++ {```suggestion
for i := range len(arc) {
```
dst[i*2] = hextable[v>>4]You could also add a bound check elimination line before this line.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
for i := 0; i < len(src); i++ {```suggestion
for i := range len(arc) {
```
Sorry I meant len(src) here (damn autocorrect).
for i := 0; i < len(src); i++ {Ingo Oeser```suggestion
for i := range len(arc) {
```
Sorry I meant len(src) here (damn autocorrect).
Done
dst[i*2] = hextable[v>>4]You could also add a bound check elimination line before this line.
This loops runs for at most three iterations; I'm not sure eliminating one of the two bounds check per iteration is worth it.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |