henry3260 commented on code in PR #77092:
URL: https://github.com/apache/airflow/pull/77092#discussion_r4728151547


##########
go-sdk/pkg/execution/client.go:
##########
@@ -521,95 +522,32 @@ func resolveExpiry(retention *time.Duration, now 
time.Time) (*genmodels.Time, er
        }
 }
 
-// validateJSONRepresentable checks what the frame encoder actually emits, not
-// the Go value: a reflection walk has to mirror the encoder's field rules 
(tags,
-// "-", omitempty, embedding, marshalers) and misjudges values wherever it 
drifts.
-// On success it returns the encoded bytes, so the caller sends them instead of
-// encoding the value a second time, and whether the value encodes to null.
-func validateJSONRepresentable(value any) (msgpack.RawMessage, bool, error) {
-       var buf bytes.Buffer
-       if err := newFrameEncoder(&buf).Encode(value); err != nil {
-               return nil, false, fmt.Errorf("%T is not JSON representable: 
%w", value, err)
-       }
-       encoded := msgpack.RawMessage(buf.Bytes())
-       // Decoded from its own reader so encoded keeps the whole payload.
-       dec := msgpack.NewDecoder(bytes.NewReader(encoded))
-       dec.SetMapDecoder(decodeStringKeyedMap)
-       decoded, err := dec.DecodeInterface()
+// encodeJSONValue returns the msgpack form of value as encoding/json writes 
it,
+// and whether that is null. The value must take encoding/json's shape, not 
only
+// its field names: argument binding and UnmarshalJSONValue decode it with
+// encoding/json, and a ",string" field or an UnmarshalJSON method rejects a
+// value that was written any other way.
+func encodeJSONValue(value any) (encoded msgpack.RawMessage, isNull bool, err 
error) {
+       blob, err := json.Marshal(value)
        if err != nil {
                return nil, false, fmt.Errorf("%T is not JSON representable: 
%w", value, err)
        }
-       if err := validateDecodedValue(decoded); err != nil {
-               return nil, false, err
-       }
-       return encoded, decoded == nil, nil
-}
-
-// decodeStringKeyedMap replaces msgpack's default map decoder, which panics on
-// a uuid.UUID key.
-func decodeStringKeyedMap(d *msgpack.Decoder) (any, error) {
-       n, err := d.DecodeMapLen()
-       if err != nil || n == -1 {
-               return nil, err
-       }
-       m := make(map[string]any, n)
-       for range n {
-               k, err := d.DecodeInterface()
-               if err != nil {
-                       return nil, err
-               }
-               key, ok := k.(string)
-               if !ok {
-                       return nil, fmt.Errorf("map keys must be strings, got 
%T", k)
-               }
-               if m[key], err = d.DecodeInterface(); err != nil {
-                       return nil, err
-               }
+       dec := json.NewDecoder(bytes.NewReader(blob))
+       dec.UseNumber()
+       var decoded any
+       if err := dec.Decode(&decoded); err != nil {
+               return nil, false, fmt.Errorf("%T is not JSON representable: 
%w", value, err)
        }
-       return m, nil
-}
-
-func validateDecodedValue(v any) error {
-       switch v := v.(type) {
-       case nil, string, bool,
-               int, int8, int16, int32, int64,
-               uint, uint8, uint16, uint32, uint64:
-               return nil
-       case float32:
-               return checkFinite(float64(v))
-       case float64:
-               return checkFinite(v)
-       case time.Time:
-               return fmt.Errorf(
-                       "time.Time is not JSON representable; store 
value.Format(time.RFC3339) " +
-                               "and parse it back with time.Parse",
-               )
-       case []byte:
-               return fmt.Errorf(
-                       "[]byte is not JSON representable; encode it, for 
example with base64.StdEncoding.EncodeToString",
-               )
-       case []any:
-               for _, elem := range v {
-                       if err := validateDecodedValue(elem); err != nil {
-                               return err
-                       }
-               }
-               return nil
-       case map[string]any:
-               for _, elem := range v {
-                       if err := validateDecodedValue(elem); err != nil {
-                               return err
-                       }
-               }
-               return nil
-       default:
-               return fmt.Errorf("%T is not JSON representable", v)
+       if decoded, err = jsonvalue.ResolveNumbers(decoded, "value"); err != 
nil {

Review Comment:
   > Also (this applies more to the entire block), every push now goes through 
three passes. [#75601](https://github.com/apache/airflow/pull/75601) posted 
numbers for its single extra pass; this should do the same.
   
   Measured against `main`, medians of 5 alternating runs on Go 1.25's 
`encoding/json`, client side only (supervisor, HTTP and DB not included):
   
   | Value | main | this PR |
   |---|---|---|
   | small map | 0.41 µs | 1.27 µs |
   | struct, 6 fields | 0.92 µs | 2.74 µs |
   | 1000 rows of maps | 0.53 ms | 1.92 ms |
   | 100k-int list | 2.92 ms | 14.45 ms |
   
   A control that the PR does not touch moved by less than 1%.
   
   The extra cost comes from the approach itself: the value is written by 
`encoding/json` and read back so that `,string` and `MarshalJSON` apply. 
Avoiding it means a second encoding path that must agree with `encoding/json`, 
which is the kind of drift this PR fixes. wdyt?



-- 
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]

Reply via email to