mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-24 21:56:43 +00:00
Optimize StorageSize performance with named constants
This commit is contained in:
parent
bdf47f4557
commit
215aaab509
1 changed files with 30 additions and 18 deletions
|
|
@ -20,21 +20,32 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Storage size constants for binary byte calculations
|
||||||
|
const (
|
||||||
|
// Binary byte size constants (powers of 2)
|
||||||
|
ByteSize = 1.0
|
||||||
|
KiB = 1024 * ByteSize
|
||||||
|
MiB = 1024 * KiB
|
||||||
|
GiB = 1024 * MiB
|
||||||
|
TiB = 1024 * GiB
|
||||||
|
)
|
||||||
|
|
||||||
// StorageSize is a wrapper around a float value that supports user friendly
|
// StorageSize is a wrapper around a float value that supports user friendly
|
||||||
// formatting.
|
// formatting.
|
||||||
type StorageSize float64
|
type StorageSize float64
|
||||||
|
|
||||||
// String implements the stringer interface.
|
// String implements the stringer interface.
|
||||||
func (s StorageSize) String() string {
|
func (s StorageSize) String() string {
|
||||||
if s > 1099511627776 {
|
switch {
|
||||||
return fmt.Sprintf("%.2f TiB", s/1099511627776)
|
case s > TiB:
|
||||||
} else if s > 1073741824 {
|
return fmt.Sprintf("%.2f TiB", s/TiB)
|
||||||
return fmt.Sprintf("%.2f GiB", s/1073741824)
|
case s > GiB:
|
||||||
} else if s > 1048576 {
|
return fmt.Sprintf("%.2f GiB", s/GiB)
|
||||||
return fmt.Sprintf("%.2f MiB", s/1048576)
|
case s > MiB:
|
||||||
} else if s > 1024 {
|
return fmt.Sprintf("%.2f MiB", s/MiB)
|
||||||
return fmt.Sprintf("%.2f KiB", s/1024)
|
case s > KiB:
|
||||||
} else {
|
return fmt.Sprintf("%.2f KiB", s/KiB)
|
||||||
|
default:
|
||||||
return fmt.Sprintf("%.2f B", s)
|
return fmt.Sprintf("%.2f B", s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -42,15 +53,16 @@ func (s StorageSize) String() string {
|
||||||
// TerminalString implements log.TerminalStringer, formatting a string for console
|
// TerminalString implements log.TerminalStringer, formatting a string for console
|
||||||
// output during logging.
|
// output during logging.
|
||||||
func (s StorageSize) TerminalString() string {
|
func (s StorageSize) TerminalString() string {
|
||||||
if s > 1099511627776 {
|
switch {
|
||||||
return fmt.Sprintf("%.2fTiB", s/1099511627776)
|
case s > TiB:
|
||||||
} else if s > 1073741824 {
|
return fmt.Sprintf("%.2fTiB", s/TiB)
|
||||||
return fmt.Sprintf("%.2fGiB", s/1073741824)
|
case s > GiB:
|
||||||
} else if s > 1048576 {
|
return fmt.Sprintf("%.2fGiB", s/GiB)
|
||||||
return fmt.Sprintf("%.2fMiB", s/1048576)
|
case s > MiB:
|
||||||
} else if s > 1024 {
|
return fmt.Sprintf("%.2fMiB", s/MiB)
|
||||||
return fmt.Sprintf("%.2fKiB", s/1024)
|
case s > KiB:
|
||||||
} else {
|
return fmt.Sprintf("%.2fKiB", s/KiB)
|
||||||
|
default:
|
||||||
return fmt.Sprintf("%.2fB", s)
|
return fmt.Sprintf("%.2fB", s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue