mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-25 11:22:16 +00:00
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.
This commit is contained in:
parent
e81ccc406b
commit
9607750b5e
9 changed files with 30 additions and 28 deletions
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue