GreedyBear
GreedyBear copied to clipboard
Optimize API Performance: Remove redundant per-item serialization in `feeds_response`
Description
The feeds_response function in api/views/utils.py currently iterates over the IOC queryset and, for each item, instantiates a FeedsResponseSerializer to validate and serialize the data.
# api/views/utils.py
# ... inside the loop iterating over iocs ...
serializer_item = FeedsResponseSerializer(
data=data_,
context={"valid_feed_types": valid_feed_types},
)
serializer_item.is_valid(raise_exception=True)
json_list.append(serializer_item.data)
Problem
When the default feed_size is 5000, this loop creates 5000 individual serializer instances and performs full validation on data that was just constructed internally from our database. This adds significant overhead (CPU and time) to the response generation, making the API slower than necessary.
Proposed Solution
Since we strictly control the data_ dictionary construction immediately before this block, the data is inherently trusted. We should:
- Verify the
data_dictionary structure aligns with the API contract. - Append
data_directly tojson_list, skipping the serializer instantiation and validation entirely.