1
0
mirror of https://github.com/golang/go synced 2024-11-26 02:57:57 -07:00

hash/crc32,hash/crc64: use sync.OnceFunc

Use sync.OnceFunc to simplify the code and to reduce global variables.

Change-Id: I7676339177e082c5be93dcf8121e379a6a7de912
GitHub-Last-Rev: f796c49260
GitHub-Pull-Request: golang/go#69633
Reviewed-on: https://go-review.googlesource.com/c/go/+/615920
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
apocelipes 2024-09-26 08:08:07 +00:00 committed by Gopher Robot
parent ca3b1b13de
commit 9ace960d54
2 changed files with 12 additions and 17 deletions

View File

@ -78,10 +78,9 @@ type Table [256]uint32
var castagnoliTable *Table var castagnoliTable *Table
var castagnoliTable8 *slicing8Table var castagnoliTable8 *slicing8Table
var updateCastagnoli func(crc uint32, p []byte) uint32 var updateCastagnoli func(crc uint32, p []byte) uint32
var castagnoliOnce sync.Once
var haveCastagnoli atomic.Bool var haveCastagnoli atomic.Bool
func castagnoliInit() { var castagnoliInitOnce = sync.OnceFunc(func() {
castagnoliTable = simpleMakeTable(Castagnoli) castagnoliTable = simpleMakeTable(Castagnoli)
if archAvailableCastagnoli() { if archAvailableCastagnoli() {
@ -96,7 +95,7 @@ func castagnoliInit() {
} }
haveCastagnoli.Store(true) haveCastagnoli.Store(true)
} })
// IEEETable is the table for the [IEEE] polynomial. // IEEETable is the table for the [IEEE] polynomial.
var IEEETable = simpleMakeTable(IEEE) var IEEETable = simpleMakeTable(IEEE)
@ -104,9 +103,8 @@ var IEEETable = simpleMakeTable(IEEE)
// ieeeTable8 is the slicing8Table for IEEE // ieeeTable8 is the slicing8Table for IEEE
var ieeeTable8 *slicing8Table var ieeeTable8 *slicing8Table
var updateIEEE func(crc uint32, p []byte) uint32 var updateIEEE func(crc uint32, p []byte) uint32
var ieeeOnce sync.Once
func ieeeInit() { var ieeeInitOnce = sync.OnceFunc(func() {
if archAvailableIEEE() { if archAvailableIEEE() {
archInitIEEE() archInitIEEE()
updateIEEE = archUpdateIEEE updateIEEE = archUpdateIEEE
@ -117,17 +115,17 @@ func ieeeInit() {
return slicingUpdate(crc, ieeeTable8, p) return slicingUpdate(crc, ieeeTable8, p)
} }
} }
} })
// MakeTable returns a [Table] constructed from the specified polynomial. // MakeTable returns a [Table] constructed from the specified polynomial.
// The contents of this [Table] must not be modified. // The contents of this [Table] must not be modified.
func MakeTable(poly uint32) *Table { func MakeTable(poly uint32) *Table {
switch poly { switch poly {
case IEEE: case IEEE:
ieeeOnce.Do(ieeeInit) ieeeInitOnce()
return IEEETable return IEEETable
case Castagnoli: case Castagnoli:
castagnoliOnce.Do(castagnoliInit) castagnoliInitOnce()
return castagnoliTable return castagnoliTable
default: default:
return simpleMakeTable(poly) return simpleMakeTable(poly)
@ -147,7 +145,7 @@ type digest struct {
// marshal and unmarshal the internal state of the hash. // marshal and unmarshal the internal state of the hash.
func New(tab *Table) hash.Hash32 { func New(tab *Table) hash.Hash32 {
if tab == IEEETable { if tab == IEEETable {
ieeeOnce.Do(ieeeInit) ieeeInitOnce()
} }
return &digest{0, tab} return &digest{0, tab}
} }
@ -202,7 +200,7 @@ func update(crc uint32, tab *Table, p []byte, checkInitIEEE bool) uint32 {
return updateCastagnoli(crc, p) return updateCastagnoli(crc, p)
case tab == IEEETable: case tab == IEEETable:
if checkInitIEEE { if checkInitIEEE {
ieeeOnce.Do(ieeeInit) ieeeInitOnce()
} }
return updateIEEE(crc, p) return updateIEEE(crc, p)
default: default:
@ -238,7 +236,7 @@ func Checksum(data []byte, tab *Table) uint32 { return Update(0, tab, data) }
// ChecksumIEEE returns the CRC-32 checksum of data // ChecksumIEEE returns the CRC-32 checksum of data
// using the [IEEE] polynomial. // using the [IEEE] polynomial.
func ChecksumIEEE(data []byte) uint32 { func ChecksumIEEE(data []byte) uint32 {
ieeeOnce.Do(ieeeInit) ieeeInitOnce()
return updateIEEE(0, data) return updateIEEE(0, data)
} }

View File

@ -30,14 +30,11 @@ const (
type Table [256]uint64 type Table [256]uint64
var ( var (
slicing8TablesBuildOnce sync.Once
slicing8TableISO *[8]Table slicing8TableISO *[8]Table
slicing8TableECMA *[8]Table slicing8TableECMA *[8]Table
) )
func buildSlicing8TablesOnce() { var buildSlicing8TablesOnce = sync.OnceFunc(buildSlicing8Tables)
slicing8TablesBuildOnce.Do(buildSlicing8Tables)
}
func buildSlicing8Tables() { func buildSlicing8Tables() {
slicing8TableISO = makeSlicingBy8Table(makeTable(ISO)) slicing8TableISO = makeSlicingBy8Table(makeTable(ISO))