File 0198-Optimize-restart-intensity-calculation.patch of Package erlang
From 120fcee1da9146f6a198b1e46c4b09beff0be0ab Mon Sep 17 00:00:00 2001
From: Maria Scott <maria-12648430@hnc-agency.org>
Date: Mon, 14 Jun 2021 17:46:58 +0200
Subject: [PATCH] Optimize restart intensity calculation
add_restart/3 was a customized re-invention of lists:takewhile/2,
and as such even a misnomer, as it didn't add anything.
By putting the new restart on the list (in add_restart/1) before
passing it to add_restart/3 to be filtered caused an unnecessary filter
step, as the new restart would never be removed.
The inPeriod/3 function, used for checking if a restart was in or out
of period, did a calculation that could be replaced by a pre-calculated
constant value, as the Now and Period arguments were always the same
over a run of add_restart/3.
With this commit, the treshold time beyond which restarts are considered
as out of period is pre-calculated.
The filtering is done on the old list of restarts via lists:takewhile/2,
using the pre-calculated treshold.
Only then the new restart is added to the list of restarts.
This way, even the name add_restart starts to fit, and the inPeriod/3
function becomes obsolete.
---
lib/stdlib/src/supervisor.erl | 21 ++++++++-------------
1 file changed, 8 insertions(+), 13 deletions(-)
diff --git a/lib/stdlib/src/supervisor.erl b/lib/stdlib/src/supervisor.erl
index d062b949b6..387cf9647e 100644
--- a/lib/stdlib/src/supervisor.erl
+++ b/lib/stdlib/src/supervisor.erl
@@ -1500,7 +1500,7 @@ add_restart(State) ->
P = State#state.period,
R = State#state.restarts,
Now = erlang:monotonic_time(1),
- R1 = add_restart([Now|R], Now, P),
+ R1 = add_restart(R, Now, P),
State1 = State#state{restarts = R1},
case length(R1) of
CurI when CurI =< I ->
@@ -1509,18 +1509,13 @@ add_restart(State) ->
{terminate, State1}
end.
-add_restart([R|Restarts], Now, Period) ->
- case inPeriod(R, Now, Period) of
- true ->
- [R|add_restart(Restarts, Now, Period)];
- _ ->
- []
- end;
-add_restart([], _, _) ->
- [].
-
-inPeriod(Then, Now, Period) ->
- Now =< Then + Period.
+add_restart(Restarts0, Now, Period) ->
+ Treshold = Now - Period,
+ Restarts1 = lists:takewhile(
+ fun (R) -> R >= Treshold end,
+ Restarts0
+ ),
+ [Now | Restarts1].
%%% ------------------------------------------------------
%%% Error and progress reporting.
--
2.26.2