aboutsummaryrefslogtreecommitdiffstats
path: root/services/packages
diff options
context:
space:
mode:
authorJason Song <i@wolfogre.com>2022-11-28 19:19:18 +0800
committerGitHub <noreply@github.com>2022-11-28 11:19:18 +0000
commit9607750b5e9001ab379fa8deab0dadbb6219c66e (patch)
treedbdd22fbe52114b852d75f7f108342570bd81eeb /services/packages
parente81ccc406bf723a5a58d685e7782f281736affd4 (diff)
downloadgitea-9607750b5e9001ab379fa8deab0dadbb6219c66e.tar.gz
gitea-9607750b5e9001ab379fa8deab0dadbb6219c66e.zip
Replace fmt.Sprintf with hex.EncodeToString (#21960)
`hex.EncodeToString` has better performance than `fmt.Sprintf("%x", []byte)`, we should use it as much as possible. I'm not an extreme fan of performance, so I think there are some exceptions: - `fmt.Sprintf("%x", func(...)[N]byte())` - We can't slice the function return value directly, and it's not worth adding lines. ```diff func A()[20]byte { ... } - a := fmt.Sprintf("%x", A()) - a := hex.EncodeToString(A()[:]) // invalid + tmp := A() + a := hex.EncodeToString(tmp[:]) ``` - `fmt.Sprintf("%X", []byte)` - `strings.ToUpper(hex.EncodeToString(bytes))` has even worse performance.
Diffstat (limited to 'services/packages')
-rw-r--r--services/packages/packages.go9
1 files changed, 5 insertions, 4 deletions
diff --git a/services/packages/packages.go b/services/packages/packages.go
index f819949c96..49f5a2fac4 100644
--- a/services/packages/packages.go
+++ b/services/packages/packages.go
@@ -5,6 +5,7 @@ package packages
import (
"context"
+ "encoding/hex"
"errors"
"fmt"
"io"
@@ -229,10 +230,10 @@ func NewPackageBlob(hsr packages_module.HashedSizeReader) *packages_model.Packag
return &packages_model.PackageBlob{
Size: hsr.Size(),
- HashMD5: fmt.Sprintf("%x", hashMD5),
- HashSHA1: fmt.Sprintf("%x", hashSHA1),
- HashSHA256: fmt.Sprintf("%x", hashSHA256),
- HashSHA512: fmt.Sprintf("%x", hashSHA512),
+ HashMD5: hex.EncodeToString(hashMD5),
+ HashSHA1: hex.EncodeToString(hashSHA1),
+ HashSHA256: hex.EncodeToString(hashSHA256),
+ HashSHA512: hex.EncodeToString(hashSHA512),
}
}