chaokunyang commented on code in PR #2638:
URL: https://github.com/apache/fory/pull/2638#discussion_r2366272648


##########
go/fory/codegen/utils.go:
##########
@@ -253,27 +302,81 @@ func sortFields(fields []*FieldInfo) {
 }
 
 // computeStructHash computes a hash for struct schema compatibility
+// This implementation aligns with the reflection-based hash calculation
 func computeStructHash(s *StructInfo) int32 {
-       h := md5.New()
-
-       // Write struct name
-       h.Write([]byte(s.Name))
+       // Use the same iterative algorithm as reflection
+       var hash int32 = 17
 
-       // Write sorted field information
+       // Process fields in the same order as reflection
        for _, field := range s.Fields {
-               h.Write([]byte(field.SnakeName))
-               h.Write([]byte(field.TypeID))
-               // Add primitive size for better differentiation
-               if field.IsPrimitive {
-                       sizeBytes := make([]byte, 4)
-                       binary.LittleEndian.PutUint32(sizeBytes, 
uint32(field.PrimitiveSize))
-                       h.Write(sizeBytes)
+               id := getFieldHashID(field)
+
+               // Same algorithm as reflection: hash = hash * 31 + id
+               newHash := int64(hash)*31 + int64(id)
+
+               // Same overflow handling as reflection
+               const MaxInt32 = 2147483647
+               for newHash >= MaxInt32 {
+                       newHash /= 7
                }
+               hash = int32(newHash)
        }
 
-       hashBytes := h.Sum(nil)
-       // Take first 4 bytes as int32
-       return int32(binary.LittleEndian.Uint32(hashBytes[:4]))
+       if hash == 0 {
+               // Same panic condition as reflection
+               panic(fmt.Errorf("hash for type %v is 0", s.Name))
+       }
+
+       return hash
+}
+
+// getFieldHashID computes the field ID for hash calculation, matching 
reflection logic exactly
+func getFieldHashID(field *FieldInfo) int32 {
+       // Map Go types to Fory TypeIds (exactly matching reflection)
+       var tid int16
+
+       switch field.TypeID {
+       case "BOOL":
+               tid = 1 // BOOL

Review Comment:
   ditto, use const variable in `type.go`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to