Skip to content

WIP: Optimise annotation expansion for mc_amqp#15743

Draft
kjnilsson wants to merge 1 commit intomainfrom
mc-ampq-modify-annotations
Draft

WIP: Optimise annotation expansion for mc_amqp#15743
kjnilsson wants to merge 1 commit intomainfrom
mc-ampq-modify-annotations

Conversation

@kjnilsson
Copy link
Contributor

No description provided.

@kjnilsson kjnilsson requested a review from Copilot March 17, 2026 10:35
@kjnilsson kjnilsson changed the title QIP: Optimise annotation expansion for mc_amqp WIP: Optimise annotation expansion for mc_amqp Mar 17, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Optimizes how mc_amqp expands mc:annotations() into AMQP 1.0 message annotations during protocol_state/2, aiming to reduce per-annotation list scans.

Changes:

  • Prefilters existing message annotations (MA0) before folding in new annotations from Anns.
  • Replaces lists:keystore/4-based upsert with O(1) consing for newly added annotations.
  • Leaves the previous upsert helper and call sites commented out.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +476 to +488
%% filter all MA's that are going to be overwrtten by ANNS
MA = lists:filter(
fun
({{symbol, <<"x-", _/binary>> = K}, _}) ->
not maps:is_key(K, Anns);
({{symbol, <<"x-exchange">>}, _}) ->
not maps:is_key(?ANN_EXCHANGE, Anns);
({{symbol, <<"x-routing-key">>}, _}) ->
not maps:is_key(?ANN_ROUTING_KEYS, Anns);
({{symbol, <<"x-opt-rabbitmq-received-time">>}, _}) ->
not maps:is_key(<<"timestamp_in_ms">>, Anns);
({{symbol, <<"x-opt-deaths">>}, _}) ->
not maps:is_key(deaths, Anns);
amqp_annotations().
protocol_state_message_annotations(MA, Anns) ->
protocol_state_message_annotations(MA0, Anns) ->
%% filter all MA's that are going to be overwrtten by ANNS
Comment on lines +499 to +522
% maps_upsert(<<"x-routing-key">>, {utf8, RKey}, L);
(<<"x-", _/binary>> = K, V, L)
when V =/= undefined ->
%% any x-* annotations get added as message annotations
maps_upsert(K, mc_util:infer_type(V), L);
[{{symbol, K}, mc_util:infer_type(V)} | L];
% maps_upsert(K, mc_util:infer_type(V), L);
(<<"timestamp_in_ms">>, V, L) ->
maps_upsert(<<"x-opt-rabbitmq-received-time">>, {timestamp, V}, L);
[{{symbol, <<"x-opt-rabbitmq-received-time">>},
{timestamp, V}} | L];
% maps_upsert(<<"x-opt-rabbitmq-received-time">>, {timestamp, V}, L);
(deaths, Deaths, L)
when is_list(Deaths) ->
Maps = encode_deaths(Deaths),
maps_upsert(<<"x-opt-deaths">>, {array, map, Maps}, L);
[{{symbol, <<"x-opt-deaths">>},
{array, map, Maps}} | L];
% maps_upsert(<<"x-opt-deaths">>, {array, map, Maps}, L);
(_, _, Acc) ->
Acc
end, MA, Anns).

maps_upsert(Key, TaggedVal, KVList) ->
TaggedKey = {symbol, Key},
Elem = {TaggedKey, TaggedVal},
lists:keystore(TaggedKey, 1, KVList, Elem).
% maps_upsert(Key, TaggedVal, KVList) ->
% TaggedKey = {symbol, Key},
% Elem = {TaggedKey, TaggedVal},
% lists:keystore(TaggedKey, 1, KVList, Elem).
@kjnilsson kjnilsson force-pushed the mc-ampq-modify-annotations branch from 56ea0c3 to e550aeb Compare March 17, 2026 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants