You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by ti...@apache.org on 2023/05/31 06:53:51 UTC

[pulsar-client-go] branch master updated: fix: use maphash instead of crypto/sha256 for hash function of hashmap in Schema.hash() (#1022)

This is an automated email from the ASF dual-hosted git repository.

tison pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-client-go.git


The following commit(s) were added to refs/heads/master by this push:
     new 2eb3405  fix: use maphash instead of crypto/sha256 for hash function of hashmap in Schema.hash() (#1022)
2eb3405 is described below

commit 2eb340511dbf0602f7c561a1d86e179098f4662b
Author: Benjamin Pereto <de...@sandchaschte.ch>
AuthorDate: Wed May 31 08:53:45 2023 +0200

    fix: use maphash instead of crypto/sha256 for hash function of hashmap in Schema.hash() (#1022)
    
    - replace sha256 hash function with hash/maphash
    - use uint64 (expected from maphash) as schema cache hashmap key
    - init static seed as it is random generated
---
 pulsar/producer_partition.go |  8 ++++----
 pulsar/schema.go             | 12 +++++++-----
 2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/pulsar/producer_partition.go b/pulsar/producer_partition.go
index fc67f51..6c2547b 100644
--- a/pulsar/producer_partition.go
+++ b/pulsar/producer_partition.go
@@ -101,12 +101,12 @@ type partitionProducer struct {
 
 type schemaCache struct {
 	lock    sync.RWMutex
-	schemas map[string][]byte
+	schemas map[uint64][]byte
 }
 
 func newSchemaCache() *schemaCache {
 	return &schemaCache{
-		schemas: make(map[string][]byte),
+		schemas: make(map[uint64][]byte),
 	}
 }
 
@@ -122,9 +122,9 @@ func (s *schemaCache) Get(schema *SchemaInfo) (schemaVersion []byte) {
 	s.lock.RLock()
 	defer s.lock.RUnlock()
 
-	key := schema.hash()
-	return s.schemas[key]
+	return s.schemas[schema.hash()]
 }
+
 func newPartitionProducer(client *client, topic string, options *ProducerOptions, partitionIdx int,
 	metrics *internal.LeveledMetrics) (
 	*partitionProducer, error) {
diff --git a/pulsar/schema.go b/pulsar/schema.go
index 405049d..0b413d4 100644
--- a/pulsar/schema.go
+++ b/pulsar/schema.go
@@ -19,10 +19,9 @@ package pulsar
 
 import (
 	"bytes"
-	"crypto/sha256"
-	"encoding/hex"
 	"encoding/json"
 	"fmt"
+	"hash/maphash"
 	"reflect"
 	"unsafe"
 
@@ -69,10 +68,11 @@ type SchemaInfo struct {
 	Properties map[string]string
 }
 
-func (s SchemaInfo) hash() string {
-	h := sha256.New()
+func (s SchemaInfo) hash() uint64 {
+	h := maphash.Hash{}
+	h.SetSeed(seed)
 	h.Write([]byte(s.Schema))
-	return hex.EncodeToString(h.Sum(nil))
+	return h.Sum64()
 }
 
 type Schema interface {
@@ -183,6 +183,8 @@ type ProtoSchema struct {
 	SchemaInfo
 }
 
+var seed = maphash.MakeSeed()
+
 // NewProtoSchema creates a new ProtoSchema
 // Note: the function will panic if creation of codec fails
 func NewProtoSchema(protoAvroSchemaDef string, properties map[string]string) *ProtoSchema {