mirror of
https://github.com/golang/go
synced 2024-11-18 10:54:40 -07:00
hash/crc32: remove redundant code
Merge the CRC32 Update and Write functions using an unexported function, to avoid duplication of code and make it more readable.
The only difference between them is the check of the initialization of the IEEE table, and a boolean value specifies that.
Throughout the crc32.go file, in the switches the default value is inserted inside the switch statement, this change uniforms the style of the MakeTable function, making it like the other pieces of code.
Change-Id: I3889f6c6671210c82f0d7250cea67907bccf3ce7
GitHub-Last-Rev: b8777ee213
GitHub-Pull-Request: golang/go#55044
Reviewed-on: https://go-review.googlesource.com/c/go/+/430456
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Jenny Rakoczy <jenny@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Jenny Rakoczy <jenny@golang.org>
Auto-Submit: Jenny Rakoczy <jenny@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
This commit is contained in:
parent
a7db14241c
commit
f6436c60e4
@ -128,9 +128,10 @@ func MakeTable(poly uint32) *Table {
|
|||||||
case Castagnoli:
|
case Castagnoli:
|
||||||
castagnoliOnce.Do(castagnoliInit)
|
castagnoliOnce.Do(castagnoliInit)
|
||||||
return castagnoliTable
|
return castagnoliTable
|
||||||
}
|
default:
|
||||||
return simpleMakeTable(poly)
|
return simpleMakeTable(poly)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// digest represents the partial evaluation of a checksum.
|
// digest represents the partial evaluation of a checksum.
|
||||||
type digest struct {
|
type digest struct {
|
||||||
@ -205,32 +206,31 @@ func readUint32(b []byte) uint32 {
|
|||||||
return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
|
return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update returns the result of adding the bytes in p to the crc.
|
func update(crc uint32, tab *Table, p []byte, checkInitIEEE bool) uint32 {
|
||||||
func Update(crc uint32, tab *Table, p []byte) uint32 {
|
|
||||||
switch {
|
switch {
|
||||||
case haveCastagnoli.Load() && tab == castagnoliTable:
|
case haveCastagnoli.Load() && tab == castagnoliTable:
|
||||||
return updateCastagnoli(crc, p)
|
return updateCastagnoli(crc, p)
|
||||||
case tab == IEEETable:
|
case tab == IEEETable:
|
||||||
// Unfortunately, because IEEETable is exported, IEEE may be used without a
|
if checkInitIEEE {
|
||||||
// call to MakeTable. We have to make sure it gets initialized in that case.
|
|
||||||
ieeeOnce.Do(ieeeInit)
|
ieeeOnce.Do(ieeeInit)
|
||||||
|
}
|
||||||
return updateIEEE(crc, p)
|
return updateIEEE(crc, p)
|
||||||
default:
|
default:
|
||||||
return simpleUpdate(crc, tab, p)
|
return simpleUpdate(crc, tab, p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update returns the result of adding the bytes in p to the crc.
|
||||||
|
func Update(crc uint32, tab *Table, p []byte) uint32 {
|
||||||
|
// Unfortunately, because IEEETable is exported, IEEE may be used without a
|
||||||
|
// call to MakeTable. We have to make sure it gets initialized in that case.
|
||||||
|
return update(crc, tab, p, true)
|
||||||
|
}
|
||||||
|
|
||||||
func (d *digest) Write(p []byte) (n int, err error) {
|
func (d *digest) Write(p []byte) (n int, err error) {
|
||||||
switch {
|
|
||||||
case haveCastagnoli.Load() && d.tab == castagnoliTable:
|
|
||||||
d.crc = updateCastagnoli(d.crc, p)
|
|
||||||
case d.tab == IEEETable:
|
|
||||||
// We only create digest objects through New() which takes care of
|
// We only create digest objects through New() which takes care of
|
||||||
// initialization in this case.
|
// initialization in this case.
|
||||||
d.crc = updateIEEE(d.crc, p)
|
d.crc = update(d.crc, d.tab, p, false)
|
||||||
default:
|
|
||||||
d.crc = simpleUpdate(d.crc, d.tab, p)
|
|
||||||
}
|
|
||||||
return len(p), nil
|
return len(p), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -329,11 +329,14 @@ func benchmark(b *testing.B, h hash.Hash32, n, alignment int64) {
|
|||||||
h.Reset()
|
h.Reset()
|
||||||
h.Write(data)
|
h.Write(data)
|
||||||
h.Sum(in)
|
h.Sum(in)
|
||||||
|
// Avoid further allocations
|
||||||
|
in = in[:0]
|
||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
h.Reset()
|
h.Reset()
|
||||||
h.Write(data)
|
h.Write(data)
|
||||||
h.Sum(in)
|
h.Sum(in)
|
||||||
|
in = in[:0]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user