From 9186c8ce651a795c6192650c2bd99f013bce3ecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20G=C3=B6m=C3=B6ri?= Date: Mon, 20 Nov 2023 13:06:33 +0100 Subject: [PATCH] Optimise text_format:escape_label_value (#160) Don't create a new binary if there is no special character to escape. --- src/formats/prometheus_text_format.erl | 28 +++++++++++++++---- .../format/prometheus_text_format_tests.erl | 4 +++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/formats/prometheus_text_format.erl b/src/formats/prometheus_text_format.erl index da407d15..2d7a8193 100644 --- a/src/formats/prometheus_text_format.erl +++ b/src/formats/prometheus_text_format.erl @@ -77,15 +77,21 @@ format(Registry) -> ok = file:close(Fd), Str. --spec escape_label_value(binary() | iolist()) -> binary() - ; (undefined) -> no_return(). +-spec escape_label_value(binary() | iolist()) -> binary(). %% @doc %% Escapes the backslash (\), double-quote ("), and line feed (\n) characters %% @end -escape_label_value(LValue) when is_list(LValue); is_binary(LValue) -> - escape_string(fun escape_label_char/1, LValue); +escape_label_value(LValue) when is_binary(LValue) -> + case has_special_char(LValue) of + true -> + escape_string(fun escape_label_char/1, LValue); + false -> + LValue + end; +escape_label_value(LValue) when is_list(LValue) -> + escape_label_value(iolist_to_binary(LValue)); escape_label_value(Value) -> - erlang:error({wtf, Value}). + erlang:error({invalid_value, Value}). %%==================================================================== %% Private Parts @@ -248,6 +254,18 @@ escape_label_char($" = X) -> escape_label_char(X) -> <>. +%% @perivate +-spec has_special_char(binary()) -> boolean(). +has_special_char(<>) + when C =:= $\\; + C =:= $\n; + C =:= $" -> + true; +has_special_char(<<_:8, Rest/bitstring>>) -> + has_special_char(Rest); +has_special_char(<<>>) -> + false. + %% @private escape_string(Fun, Str) when is_binary(Str) -> << <<(Fun(X))/binary>> || <> <= Str >>; diff --git a/test/eunit/format/prometheus_text_format_tests.erl b/test/eunit/format/prometheus_text_format_tests.erl index c6247253..a62897c4 100644 --- a/test/eunit/format/prometheus_text_format_tests.erl +++ b/test/eunit/format/prometheus_text_format_tests.erl @@ -28,6 +28,10 @@ escape_label_value_test()-> ?assertEqual(<<"qwe\\\\qwe\\nq\\\"we\\\"qwe">>, prometheus_text_format:escape_label_value("qwe\\qwe\nq\"we\"qwe")). +escape_label_value_no_special_char_test() -> + LabelBin = <<"qweqweqwe">>, + ?assert(erts_debug:same(LabelBin, prometheus_text_format:escape_label_value(LabelBin))). + prometheus_format_test_() -> {foreach, fun prometheus_eunit_common:start/0,