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