|
| 1 | +""" |
| 2 | +Fast serialization utilities for Feature Server responses. |
| 3 | +
|
| 4 | +This module provides optimized alternatives to generic protobuf serialization |
| 5 | +for known message types, specifically GetOnlineFeaturesResponse. |
| 6 | +
|
| 7 | +Performance: 4x faster than MessageToDict for GetOnlineFeaturesResponse. |
| 8 | +See: https://github.com/feast-dev/feast/issues/6013 |
| 9 | +""" |
| 10 | + |
| 11 | +import base64 |
| 12 | +from datetime import datetime, timezone |
| 13 | +from typing import Any, Dict |
| 14 | + |
| 15 | +from feast.protos.feast.serving.ServingService_pb2 import GetOnlineFeaturesResponse |
| 16 | +from feast.protos.feast.types.Value_pb2 import Value |
| 17 | + |
| 18 | +# Status enum to string mapping (cached for performance) |
| 19 | +# Must match FieldStatus enum in protos/feast/serving/ServingService.proto |
| 20 | +_STATUS_NAMES: Dict[int, str] = { |
| 21 | + 0: "INVALID", |
| 22 | + 1: "PRESENT", |
| 23 | + 2: "NULL_VALUE", |
| 24 | + 3: "NOT_FOUND", |
| 25 | + 4: "OUTSIDE_MAX_AGE", |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +def response_to_dict_fast(response: GetOnlineFeaturesResponse) -> Dict[str, Any]: |
| 30 | + """ |
| 31 | + Fast conversion of GetOnlineFeaturesResponse to dict. |
| 32 | +
|
| 33 | + This is ~4x faster than MessageToDict for this specific message type |
| 34 | + because it avoids reflection-based field discovery and generic type handling. |
| 35 | +
|
| 36 | + Args: |
| 37 | + response: The GetOnlineFeaturesResponse protobuf message |
| 38 | +
|
| 39 | + Returns: |
| 40 | + Dictionary representation matching MessageToDict output format |
| 41 | + with preserving_proto_field_name=True |
| 42 | + """ |
| 43 | + result: Dict[str, Any] = { |
| 44 | + "results": [ |
| 45 | + { |
| 46 | + "values": [_value_to_dict(v) for v in feature_vector.values], |
| 47 | + "statuses": [ |
| 48 | + _STATUS_NAMES.get(s, "UNKNOWN") for s in feature_vector.statuses |
| 49 | + ], |
| 50 | + "event_timestamps": [ |
| 51 | + _timestamp_to_str(ts) for ts in feature_vector.event_timestamps |
| 52 | + ] |
| 53 | + if feature_vector.event_timestamps |
| 54 | + else [], |
| 55 | + } |
| 56 | + for feature_vector in response.results |
| 57 | + ] |
| 58 | + } |
| 59 | + |
| 60 | + if response.HasField("metadata"): |
| 61 | + result["metadata"] = _metadata_to_dict(response.metadata) |
| 62 | + |
| 63 | + return result |
| 64 | + |
| 65 | + |
| 66 | +def _value_to_dict(v: Value) -> Dict[str, Any]: |
| 67 | + """Convert a Value proto to dict based on which field is set.""" |
| 68 | + which = v.WhichOneof("val") |
| 69 | + if which is None: |
| 70 | + return {"null_val": 0} |
| 71 | + |
| 72 | + if which == "double_val": |
| 73 | + return {"double_val": v.double_val} |
| 74 | + elif which == "float_val": |
| 75 | + return {"float_val": v.float_val} |
| 76 | + elif which == "int64_val": |
| 77 | + # Proto3 JSON spec: int64 as string to avoid precision loss |
| 78 | + return {"int64_val": str(v.int64_val)} |
| 79 | + elif which == "int32_val": |
| 80 | + return {"int32_val": v.int32_val} |
| 81 | + elif which == "string_val": |
| 82 | + return {"string_val": v.string_val} |
| 83 | + elif which == "bool_val": |
| 84 | + return {"bool_val": v.bool_val} |
| 85 | + elif which == "bytes_val": |
| 86 | + return {"bytes_val": base64.b64encode(v.bytes_val).decode("ascii")} |
| 87 | + elif which == "double_list_val": |
| 88 | + return {"double_list_val": {"val": list(v.double_list_val.val)}} |
| 89 | + elif which == "float_list_val": |
| 90 | + return {"float_list_val": {"val": list(v.float_list_val.val)}} |
| 91 | + elif which == "int64_list_val": |
| 92 | + return {"int64_list_val": {"val": [str(x) for x in v.int64_list_val.val]}} |
| 93 | + elif which == "int32_list_val": |
| 94 | + return {"int32_list_val": {"val": list(v.int32_list_val.val)}} |
| 95 | + elif which == "string_list_val": |
| 96 | + return {"string_list_val": {"val": list(v.string_list_val.val)}} |
| 97 | + elif which == "bool_list_val": |
| 98 | + return {"bool_list_val": {"val": list(v.bool_list_val.val)}} |
| 99 | + elif which == "unix_timestamp_val": |
| 100 | + return {"unix_timestamp_val": v.unix_timestamp_val} |
| 101 | + elif which == "unix_timestamp_list_val": |
| 102 | + return {"unix_timestamp_list_val": {"val": list(v.unix_timestamp_list_val.val)}} |
| 103 | + |
| 104 | + return {"null_val": 0} |
| 105 | + |
| 106 | + |
| 107 | +def _timestamp_to_str(ts) -> str: |
| 108 | + """Convert protobuf Timestamp to ISO format string.""" |
| 109 | + if ts.seconds == 0 and ts.nanos == 0: |
| 110 | + return "" |
| 111 | + dt = datetime.fromtimestamp(ts.seconds + ts.nanos / 1e9, tz=timezone.utc) |
| 112 | + return dt.isoformat() |
| 113 | + |
| 114 | + |
| 115 | +def _metadata_to_dict(metadata) -> Dict[str, Any]: |
| 116 | + """Convert FeatureResponseMeta to dict.""" |
| 117 | + result: Dict[str, Any] = {} |
| 118 | + if metadata.HasField("feature_names"): |
| 119 | + result["feature_names"] = {"val": list(metadata.feature_names.val)} |
| 120 | + return result |
0 commit comments