Embed HasherCommon in each hasher type.

(Rather than having a pointer to the custom data in HasherCommon.)
This commit is contained in:
Andy Balholm 2019-03-08 14:11:00 -08:00
parent 7aac2143a1
commit 6a14da654a
16 changed files with 89 additions and 62 deletions

4
h10.go
View File

@ -30,6 +30,7 @@ func HashBytesH10(data []byte) uint32 {
}
type H10 struct {
HasherCommon
window_mask_ uint
buckets_ [1 << 17]uint32
invalid_pos_ uint32
@ -37,7 +38,7 @@ type H10 struct {
}
func SelfH10(handle HasherHandle) *H10 {
return handle.extra.(*H10)
return handle.(*H10)
}
func ForestH10(self *H10) []uint32 {
@ -45,7 +46,6 @@ func ForestH10(self *H10) []uint32 {
}
func InitializeH10(handle HasherHandle, params *BrotliEncoderParams) {
handle.extra = new(H10)
var self *H10 = SelfH10(handle)
self.window_mask_ = (1 << params.lgwin) - 1
self.invalid_pos_ = uint32(0 - self.window_mask_)

4
h2.go
View File

@ -35,15 +35,15 @@ func HashBytesH2(data []byte) uint32 {
This is a hash map of fixed size (1 << 16). Starting from the
given index, 1 buckets are used to store values of a key. */
type H2 struct {
HasherCommon
buckets_ [(1 << 16) + 1]uint32
}
func SelfH2(handle HasherHandle) *H2 {
return handle.extra.(*H2)
return handle.(*H2)
}
func InitializeH2(handle HasherHandle, params *BrotliEncoderParams) {
handle.extra = new(H2)
}
func PrepareH2(handle HasherHandle, one_shot bool, input_size uint, data []byte) {

4
h3.go
View File

@ -31,15 +31,15 @@ func HashBytesH3(data []byte) uint32 {
This is a hash map of fixed size (BUCKET_SIZE). Starting from the
given index, 2 buckets are used to store values of a key. */
type H3 struct {
HasherCommon
buckets_ [(1 << 16) + 2]uint32
}
func SelfH3(handle HasherHandle) *H3 {
return handle.extra.(*H3)
return handle.(*H3)
}
func InitializeH3(handle HasherHandle, params *BrotliEncoderParams) {
handle.extra = new(H3)
}
func PrepareH3(handle HasherHandle, one_shot bool, input_size uint, data []byte) {

12
h35.go
View File

@ -32,17 +32,17 @@ func StoreLookaheadH35() uint {
}
type H35 struct {
HasherCommon
ha HasherHandle
hb HasherHandle
params *BrotliEncoderParams
}
func SelfH35(handle HasherHandle) *H35 {
return handle.extra.(*H35)
return handle.(*H35)
}
func InitializeH35(handle HasherHandle, params *BrotliEncoderParams) {
handle.extra = new(H35)
var self *H35 = SelfH35(handle)
self.ha = nil
self.hb = nil
@ -59,16 +59,16 @@ func PrepareH35(handle HasherHandle, one_shot bool, input_size uint, data []byte
var common_a *HasherCommon
var common_b *HasherCommon
self.ha = new(HasherCommon)
common_a = (*HasherCommon)(self.ha)
self.ha = new(H3)
common_a = self.ha.Common()
common_a.params = self.params.hasher
common_a.is_prepared_ = false
common_a.dict_num_lookups = 0
common_a.dict_num_matches = 0
InitializeH3(self.ha, self.params)
self.hb = new(HasherCommon)
common_b = (*HasherCommon)(self.hb)
self.hb = new(HROLLING_FAST)
common_b = self.hb.Common()
common_b.params = self.params.hasher
common_b.is_prepared_ = false
common_b.dict_num_lookups = 0

4
h4.go
View File

@ -31,15 +31,15 @@ func HashBytesH4(data []byte) uint32 {
This is a hash map of fixed size (BUCKET_SIZE). Starting from the
given index, 4 buckets are used to store values of a key. */
type H4 struct {
HasherCommon
buckets_ [(1 << 17) + 4]uint32
}
func SelfH4(handle HasherHandle) *H4 {
return handle.extra.(*H4)
return handle.(*H4)
}
func InitializeH4(handle HasherHandle, params *BrotliEncoderParams) {
handle.extra = new(H4)
}
func PrepareH4(handle HasherHandle, one_shot bool, input_size uint, data []byte) {

4
h40.go
View File

@ -40,6 +40,7 @@ type BankH40 struct {
}
type H40 struct {
HasherCommon
addr [1 << 15]uint32
head [1 << 15]uint16
tiny_hash [65536]byte
@ -49,11 +50,10 @@ type H40 struct {
}
func SelfH40(handle HasherHandle) *H40 {
return handle.extra.(*H40)
return handle.(*H40)
}
func InitializeH40(handle HasherHandle, params *BrotliEncoderParams) {
handle.extra = new(H40)
var q uint
if params.quality > 6 {
q = 7

4
h41.go
View File

@ -40,6 +40,7 @@ type BankH41 struct {
}
type H41 struct {
HasherCommon
addr [1 << 15]uint32
head [1 << 15]uint16
tiny_hash [65536]byte
@ -49,11 +50,10 @@ type H41 struct {
}
func SelfH41(handle HasherHandle) *H41 {
return handle.extra.(*H41)
return handle.(*H41)
}
func InitializeH41(handle HasherHandle, params *BrotliEncoderParams) {
handle.extra = new(H41)
var tmp uint
if params.quality > 6 {
tmp = 7

4
h42.go
View File

@ -40,6 +40,7 @@ type BankH42 struct {
}
type H42 struct {
HasherCommon
addr [1 << 15]uint32
head [1 << 15]uint16
tiny_hash [65536]byte
@ -49,11 +50,10 @@ type H42 struct {
}
func SelfH42(handle HasherHandle) *H42 {
return handle.extra.(*H42)
return handle.(*H42)
}
func InitializeH42(handle HasherHandle, params *BrotliEncoderParams) {
handle.extra = new(H42)
var tmp uint
if params.quality > 6 {
tmp = 7

12
h5.go
View File

@ -31,6 +31,7 @@ func HashBytesH5(data []byte, shift int) uint32 {
}
type H5 struct {
HasherCommon
bucket_size_ uint
block_size_ uint
hash_shift_ int
@ -40,7 +41,7 @@ type H5 struct {
}
func SelfH5(handle HasherHandle) *H5 {
return handle.extra.(*H5)
return handle.(*H5)
}
func NumH5(self *H5) []uint16 {
@ -52,8 +53,7 @@ func BucketsH5(self *H5) []uint32 {
}
func InitializeH5(handle HasherHandle, params *BrotliEncoderParams) {
var common *HasherCommon = GetHasherCommon(handle)
handle.extra = new(H5)
var common *HasherCommon = handle.Common()
var self *H5 = SelfH5(handle)
self.hash_shift_ = 32 - common.params.bucket_bits
self.bucket_size_ = uint(1) << uint(common.params.bucket_bits)
@ -88,7 +88,7 @@ func StoreH5(handle HasherHandle, data []byte, mask uint, ix uint) {
var num []uint16 = NumH5(self)
var key uint32 = HashBytesH5(data[ix&mask:], self.hash_shift_)
var minor_ix uint = uint(num[key]) & uint(self.block_mask_)
var offset uint = minor_ix + uint(key<<uint(GetHasherCommon(handle).params.block_bits))
var offset uint = minor_ix + uint(key<<uint(handle.Common().params.block_bits))
BucketsH5(self)[offset] = uint32(ix)
num[key]++
}
@ -113,7 +113,7 @@ func StitchToPreviousBlockH5(handle HasherHandle, num_bytes uint, position uint,
}
func PrepareDistanceCacheH5(handle HasherHandle, distance_cache []int) {
PrepareDistanceCache(distance_cache, GetHasherCommon(handle).params.num_last_distances_to_check)
PrepareDistanceCache(distance_cache, handle.Common().params.num_last_distances_to_check)
}
/* Find a longest backward match of &data[cur_ix] up to the length of
@ -128,7 +128,7 @@ func PrepareDistanceCacheH5(handle HasherHandle, distance_cache []int) {
Writes the best match into |out|.
|out|->score is updated only if a better match is found. */
func FindLongestMatchH5(handle HasherHandle, dictionary *BrotliEncoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *HasherSearchResult) {
var common *HasherCommon = GetHasherCommon(handle)
var common *HasherCommon = handle.Common()
var self *H5 = SelfH5(handle)
var num []uint16 = NumH5(self)
var buckets []uint32 = BucketsH5(self)

4
h54.go
View File

@ -31,15 +31,15 @@ func HashBytesH54(data []byte) uint32 {
This is a hash map of fixed size ((1 << 20)). Starting from the
given index, 4 buckets are used to store values of a key. */
type H54 struct {
HasherCommon
buckets_ [(1 << 20) + 4]uint32
}
func SelfH54(handle HasherHandle) *H54 {
return handle.extra.(*H54)
return handle.(*H54)
}
func InitializeH54(handle HasherHandle, params *BrotliEncoderParams) {
handle.extra = new(H54)
}
func PrepareH54(handle HasherHandle, one_shot bool, input_size uint, data []byte) {

12
h55.go
View File

@ -30,17 +30,17 @@ func StoreLookaheadH55() uint {
}
type H55 struct {
HasherCommon
ha HasherHandle
hb HasherHandle
params *BrotliEncoderParams
}
func SelfH55(handle HasherHandle) *H55 {
return handle.extra.(*H55)
return handle.(*H55)
}
func InitializeH55(handle HasherHandle, params *BrotliEncoderParams) {
handle.extra = new(H55)
var self *H55 = SelfH55(handle)
self.ha = nil
self.hb = nil
@ -57,16 +57,16 @@ func PrepareH55(handle HasherHandle, one_shot bool, input_size uint, data []byte
var common_a *HasherCommon
var common_b *HasherCommon
self.ha = new(HasherCommon)
common_a = (*HasherCommon)(self.ha)
self.ha = new(H54)
common_a = self.ha.Common()
common_a.params = self.params.hasher
common_a.is_prepared_ = false
common_a.dict_num_lookups = 0
common_a.dict_num_matches = 0
InitializeH54(self.ha, self.params)
self.hb = new(HasherCommon)
common_b = (*HasherCommon)(self.hb)
self.hb = new(HROLLING_FAST)
common_b = self.hb.Common()
common_b.params = self.params.hasher
common_b.is_prepared_ = false
common_b.dict_num_lookups = 0

12
h6.go
View File

@ -31,6 +31,7 @@ func HashBytesH6(data []byte, mask uint64, shift int) uint32 {
}
type H6 struct {
HasherCommon
bucket_size_ uint
block_size_ uint
hash_shift_ int
@ -41,7 +42,7 @@ type H6 struct {
}
func SelfH6(handle HasherHandle) *H6 {
return handle.extra.(*H6)
return handle.(*H6)
}
func NumH6(self *H6) []uint16 {
@ -53,8 +54,7 @@ func BucketsH6(self *H6) []uint32 {
}
func InitializeH6(handle HasherHandle, params *BrotliEncoderParams) {
var common *HasherCommon = GetHasherCommon(handle)
handle.extra = new(H6)
var common *HasherCommon = handle.Common()
var self *H6 = SelfH6(handle)
self.hash_shift_ = 64 - common.params.bucket_bits
self.hash_mask_ = (^(uint64(0))) >> uint(64-8*common.params.hash_len)
@ -90,7 +90,7 @@ func StoreH6(handle HasherHandle, data []byte, mask uint, ix uint) {
var num []uint16 = NumH6(self)
var key uint32 = HashBytesH6(data[ix&mask:], self.hash_mask_, self.hash_shift_)
var minor_ix uint = uint(num[key]) & uint(self.block_mask_)
var offset uint = minor_ix + uint(key<<uint(GetHasherCommon(handle).params.block_bits))
var offset uint = minor_ix + uint(key<<uint(handle.Common().params.block_bits))
BucketsH6(self)[offset] = uint32(ix)
num[key]++
}
@ -115,7 +115,7 @@ func StitchToPreviousBlockH6(handle HasherHandle, num_bytes uint, position uint,
}
func PrepareDistanceCacheH6(handle HasherHandle, distance_cache []int) {
PrepareDistanceCache(distance_cache, GetHasherCommon(handle).params.num_last_distances_to_check)
PrepareDistanceCache(distance_cache, handle.Common().params.num_last_distances_to_check)
}
/* Find a longest backward match of &data[cur_ix] up to the length of
@ -130,7 +130,7 @@ func PrepareDistanceCacheH6(handle HasherHandle, distance_cache []int) {
Writes the best match into |out|.
|out|->score is updated only if a better match is found. */
func FindLongestMatchH6(handle HasherHandle, dictionary *BrotliEncoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *HasherSearchResult) {
var common *HasherCommon = GetHasherCommon(handle)
var common *HasherCommon = handle.Common()
var self *H6 = SelfH6(handle)
var num []uint16 = NumH6(self)
var buckets []uint32 = BucketsH6(self)

12
h65.go
View File

@ -30,17 +30,17 @@ func StoreLookaheadH65() uint {
}
type H65 struct {
HasherCommon
ha HasherHandle
hb HasherHandle
params *BrotliEncoderParams
}
func SelfH65(handle HasherHandle) *H65 {
return handle.extra.(*H65)
return handle.(*H65)
}
func InitializeH65(handle HasherHandle, params *BrotliEncoderParams) {
handle.extra = new(H65)
var self *H65 = SelfH65(handle)
self.ha = nil
self.hb = nil
@ -57,16 +57,16 @@ func PrepareH65(handle HasherHandle, one_shot bool, input_size uint, data []byte
var common_a *HasherCommon
var common_b *HasherCommon
self.ha = new(HasherCommon)
common_a = (*HasherCommon)(self.ha)
self.ha = new(H6)
common_a = self.ha.Common()
common_a.params = self.params.hasher
common_a.is_prepared_ = false
common_a.dict_num_lookups = 0
common_a.dict_num_matches = 0
InitializeH6(self.ha, self.params)
self.hb = new(HasherCommon)
common_b = (*HasherCommon)(self.hb)
self.hb = new(HROLLING)
common_b = self.hb.Common()
common_b.params = self.params.hasher
common_b.is_prepared_ = false
common_b.dict_num_lookups = 0

51
hash.go
View File

@ -23,13 +23,14 @@ type HasherCommon struct {
is_prepared_ bool
dict_num_lookups uint
dict_num_matches uint
extra interface{}
}
type HasherHandle *HasherCommon
func (h *HasherCommon) Common() *HasherCommon {
return h
}
func GetHasherCommon(handle HasherHandle) *HasherCommon {
return (*HasherCommon)(handle)
type HasherHandle interface {
Common() *HasherCommon
}
type score_t uint
@ -166,7 +167,7 @@ func TestStaticDictionaryItem(dictionary *BrotliEncoderDictionary, item uint, da
func SearchInStaticDictionary(dictionary *BrotliEncoderDictionary, handle HasherHandle, data []byte, max_length uint, max_backward uint, max_distance uint, out *HasherSearchResult, shallow bool) {
var key uint
var i uint
var self *HasherCommon = GetHasherCommon(handle)
var self *HasherCommon = handle.Common()
if self.dict_num_matches < self.dict_num_lookups>>7 {
return
}
@ -238,7 +239,7 @@ func HasherReset(handle HasherHandle) {
if handle == nil {
return
}
GetHasherCommon(handle).is_prepared_ = false
handle.Common().is_prepared_ = false
}
func HasherSetup(handle *HasherHandle, params *BrotliEncoderParams, data []byte, position uint, input_size uint, is_last bool) {
@ -247,9 +248,37 @@ func HasherSetup(handle *HasherHandle, params *BrotliEncoderParams, data []byte,
var one_shot bool = (position == 0 && is_last)
if *handle == nil {
ChooseHasher(params, &params.hasher)
self = new(HasherCommon)
switch params.hasher.type_ {
case 2:
self = new(H2)
case 3:
self = new(H3)
case 4:
self = new(H4)
case 5:
self = new(H5)
case 6:
self = new(H6)
case 40:
self = new(H40)
case 41:
self = new(H41)
case 42:
self = new(H42)
case 54:
self = new(H54)
case 35:
self = new(H35)
case 55:
self = new(H55)
case 65:
self = new(H65)
case 10:
self = new(H10)
}
*handle = self
common = GetHasherCommon(self)
common = self.Common()
common.params = params.hasher
switch common.params.type_ {
case 2:
@ -278,8 +307,6 @@ func HasherSetup(handle *HasherHandle, params *BrotliEncoderParams, data []byte,
InitializeH65(*handle, params)
case 10:
InitializeH10(*handle, params)
break
fallthrough
default:
break
@ -289,7 +316,7 @@ func HasherSetup(handle *HasherHandle, params *BrotliEncoderParams, data []byte,
}
self = *handle
common = GetHasherCommon(self)
common = self.Common()
if !common.is_prepared_ {
switch common.params.type_ {
case 2:
@ -336,7 +363,7 @@ func InitOrStitchToPreviousBlock(handle *HasherHandle, data []byte, mask uint, p
var self HasherHandle
HasherSetup(handle, params, data, position, input_size, is_last)
self = *handle
switch GetHasherCommon(self).params.type_ {
switch self.Common().params.type_ {
case 2:
StitchToPreviousBlockH2(self, input_size, position, data, mask)
case 3:

View File

@ -41,6 +41,7 @@ func HashRollingFunctionHROLLING(state uint32, add byte, rem byte, factor uint32
}
type HROLLING struct {
HasherCommon
state uint32
table []uint32
next_ix uint
@ -50,11 +51,10 @@ type HROLLING struct {
}
func SelfHROLLING(handle HasherHandle) *HROLLING {
return handle.extra.(*HROLLING)
return handle.(*HROLLING)
}
func InitializeHROLLING(handle HasherHandle, params *BrotliEncoderParams) {
handle.extra = new(HROLLING)
var self *HROLLING = SelfHROLLING(handle)
var i uint
self.state = 0

View File

@ -39,6 +39,7 @@ func HashRollingFunctionHROLLING_FAST(state uint32, add byte, rem byte, factor u
}
type HROLLING_FAST struct {
HasherCommon
state uint32
table []uint32
next_ix uint
@ -48,11 +49,10 @@ type HROLLING_FAST struct {
}
func SelfHROLLING_FAST(handle HasherHandle) *HROLLING_FAST {
return handle.extra.(*HROLLING_FAST)
return handle.(*HROLLING_FAST)
}
func InitializeHROLLING_FAST(handle HasherHandle, params *BrotliEncoderParams) {
handle.extra = new(HROLLING_FAST)
var self *HROLLING_FAST = SelfHROLLING_FAST(handle)
var i uint
self.state = 0