File 3561-Remove-redundant-line-instructions.patch of Package erlang
From 9b230564a349964604586c4d66394f27c3133600 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= <bjorn@erlang.org>
Date: Thu, 3 Sep 2020 08:51:12 +0200
Subject: [PATCH] Remove redundant line instructions
Remove all `line` instructions having the same location (filename and
line number) as the previous `line` instruction. It turns out that
such redundant `line` instructions are quite common. Removing them
decreases the size of the BEAM files, but not size of the loaded
code since the loader already removes such redundant `line` instructions.
---
lib/compiler/src/beam_z.erl | 23 ++++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/lib/compiler/src/beam_z.erl b/lib/compiler/src/beam_z.erl
index 2744c96c2e..1252f3fb61 100644
--- a/lib/compiler/src/beam_z.erl
+++ b/lib/compiler/src/beam_z.erl
@@ -37,7 +37,8 @@ module({Mod,Exp,Attr,Fs0,Lc}, Opts) ->
function({function,Name,Arity,CLabel,Is0}, NoGetHdTl) ->
try
Is1 = undo_renames(Is0),
- Is = maybe_eliminate_get_hd_tl(Is1, NoGetHdTl),
+ Is2 = maybe_eliminate_get_hd_tl(Is1, NoGetHdTl),
+ Is = remove_redundant_lines(Is2),
{function,Name,Arity,CLabel,Is}
catch
Class:Error:Stack ->
@@ -161,3 +162,23 @@ maybe_eliminate_get_hd_tl(Is, true) ->
(I) -> I
end, Is);
maybe_eliminate_get_hd_tl(Is, false) -> Is.
+
+%% Remove all `line` instructions having the same location as the
+%% previous `line` instruction. It turns out that such redundant
+%% `line` instructions are quite common. Removing them decreases the
+%% size of the BEAM files, but not size of the loaded code since the
+%% loader already removes such redundant `line` instructions.
+
+remove_redundant_lines(Is) ->
+ remove_redundant_lines_1(Is, none).
+
+remove_redundant_lines_1([{line,Loc}=I|Is], PrevLoc) ->
+ if
+ Loc =:= PrevLoc ->
+ remove_redundant_lines_1(Is, Loc);
+ true ->
+ [I|remove_redundant_lines_1(Is, Loc)]
+ end;
+remove_redundant_lines_1([I|Is], PrevLoc) ->
+ [I|remove_redundant_lines_1(Is, PrevLoc)];
+remove_redundant_lines_1([], _) -> [].
--
2.26.2