Skip to content

Commit

Permalink
Embed HasherCommon in each hasher type.
Browse files Browse the repository at this point in the history
(Rather than having a pointer to the custom data in HasherCommon.)
  • Loading branch information
andybalholm committed Mar 8, 2019
1 parent 7aac214 commit 6a14da6
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 62 deletions.
4 changes: 2 additions & 2 deletions h10.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ func HashBytesH10(data []byte) uint32 {
}

type H10 struct {
HasherCommon
window_mask_ uint
buckets_ [1 << 17]uint32
invalid_pos_ uint32
forest []uint32
}

func SelfH10(handle HasherHandle) *H10 {
return handle.extra.(*H10)
return handle.(*H10)
}

func ForestH10(self *H10) []uint32 {
return []uint32(self.forest)
}

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_)
Expand Down
4 changes: 2 additions & 2 deletions h2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions h3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions h35.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions h4.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions h40.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type BankH40 struct {
}

type H40 struct {
HasherCommon
addr [1 << 15]uint32
head [1 << 15]uint16
tiny_hash [65536]byte
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions h41.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type BankH41 struct {
}

type H41 struct {
HasherCommon
addr [1 << 15]uint32
head [1 << 15]uint16
tiny_hash [65536]byte
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions h42.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type BankH42 struct {
}

type H42 struct {
HasherCommon
addr [1 << 15]uint32
head [1 << 15]uint16
tiny_hash [65536]byte
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions h5.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func HashBytesH5(data []byte, shift int) uint32 {
}

type H5 struct {
HasherCommon
bucket_size_ uint
block_size_ uint
hash_shift_ int
Expand All @@ -40,7 +41,7 @@ type H5 struct {
}

func SelfH5(handle HasherHandle) *H5 {
return handle.extra.(*H5)
return handle.(*H5)
}

func NumH5(self *H5) []uint16 {
Expand All @@ -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)
Expand Down Expand Up @@ -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]++
}
Expand All @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions h54.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions h55.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions h6.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,7 +42,7 @@ type H6 struct {
}

func SelfH6(handle HasherHandle) *H6 {
return handle.extra.(*H6)
return handle.(*H6)
}

func NumH6(self *H6) []uint16 {
Expand All @@ -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)
Expand Down Expand Up @@ -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]++
}
Expand All @@ -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
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions h65.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit 6a14da6

Please sign in to comment.