Skip to content

Commit

Permalink
Optimize crc32 hash calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
maksim.konovalov committed Feb 1, 2025
1 parent 8fd45e2 commit a6e669e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Unreleased

FEATURES:
* Optimized crc32 hash function.

BUG FIXES:
* ETCD v2 provider validates replicaset result count and make error if replicasets count == 0 (#40).

Expand Down
18 changes: 10 additions & 8 deletions vshard.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,17 @@ func validateCfg(cfg Config) error {
// -- Other
// --------------------------------------------------------------------------------

var h = crc.NewHash(&crc.Parameters{
Width: 32,
Polynomial: 0x1EDC6F41,
FinalXor: 0x0,
ReflectIn: true,
ReflectOut: true,
Init: 0xFFFFFFFF,
})

func BucketIDStrCRC32(shardKey string, totalBucketCount uint64) uint64 {
return crc.CalculateCRC(&crc.Parameters{
Width: 32,
Polynomial: 0x1EDC6F41,
FinalXor: 0x0,
ReflectIn: true,
ReflectOut: true,
Init: 0xFFFFFFFF,
}, []byte(shardKey))%totalBucketCount + 1
return h.CalculateCRC([]byte(shardKey))%totalBucketCount + 1
}

// BucketIDStrCRC32 return the bucket identifier from the parameter used for sharding.
Expand Down
1 change: 1 addition & 0 deletions vshard_shadow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func BenchmarkRouterBucketIDStrCRC32(b *testing.B) {
for i := 0; i < b.N; i++ {
vshardrouter.BucketIDStrCRC32("test_bench_key", uint64(256000))
}
b.ReportAllocs()
}

func TestInstanceInfo_Validate(t *testing.T) {
Expand Down

0 comments on commit a6e669e

Please sign in to comment.