File 2361-Collapse-strings-into-large-integers-in-core-pattern.patch of Package erlang

From 4a9ca4dbc417523bb5e97615199ceef12ae876fb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jos=C3=A9=20Valim?= <jose.valim@dashbit.co>
Date: Thu, 29 Oct 2020 18:14:18 +0100
Subject: [PATCH] Collapse strings into large integers in core patterns

Prior to this patch, v3_core would expand each
character in a string to an integer. This made
patterns with large strings to become large and
expensive ASTs. For example, this file:

https://gist.github.com/josevalim/694c1799143fcf25e43aa27e3e11e4c1

would grow its representation in more than 20x!

This commit makes it so we squeeze strings into
large integers also when building patterns,
mimicking an optimization that we already did for
strings outside of patterns.

Before this patch:

    expand_records   :  0.077 s   19988.7 kB
    core             :  2.552 s  373294.0 kB
    sys_core_fold    :  0.868 s  370212.9 kB
    sys_core_alias   :  0.237 s  370212.9 kB
    core_transforms  :  0.000 s  370212.9 kB
    sys_core_bsm     :  0.677 s  370212.9 kB
    v3_kernel        :  2.662 s  169439.0 kB

After this patch:

    core             :  0.653 s   72136.4 kB
    sys_core_fold    :  0.482 s   69055.3 kB
    sys_core_alias   :  0.146 s   69055.3 kB
    core_transforms  :  0.000 s   69055.3 kB
    sys_core_bsm     :  0.098 s   69055.3 kB
    v3_kernel        :  2.250 s  169439.0 kB

This commit also changes the maximum collapse size
from 2048 to 1024 in order to mirror the expansion
value in v3_kernel. Making sure that collapses made
in v3_core are expanded in v3_kernel.
---
 lib/compiler/src/v3_core.erl   | 21 +++++++++++++--------
 lib/compiler/src/v3_kernel.erl |  2 ++
 2 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/lib/compiler/src/v3_core.erl b/lib/compiler/src/v3_core.erl
index 74a45b7279..9d148fa592 100644
--- a/lib/compiler/src/v3_core.erl
+++ b/lib/compiler/src/v3_core.erl
@@ -91,6 +91,9 @@
 
 -include("core_parse.hrl").
 
+%% Matches expansion max segment in v3_kernel.
+-define(COLLAPSE_MAX_SIZE_SEGMENT, 1024).
+
 %% Internal core expressions and help functions.
 %% N.B. annotations fields in place as normal Core expressions.
 
@@ -1152,7 +1155,7 @@ bitstrs([], St) ->
     {[],[],St}.
 
 bitstr({bin_element,Line,{string,_,S},{integer,_,8},_}, St) ->
-    bitstrs(bin_expand_string(S, Line, 0, 0), St);
+    bitstrs(bin_expand_string(S, Line, 0, 0, []), St);
 bitstr({bin_element,Line,{string,_,[]},Sz0,Ts}, St0) ->
     %% Empty string. We must make sure that the type is correct.
     {[#c_bitstr{size=Sz}],Eps0,St1} =
@@ -1332,13 +1335,13 @@ count_bits(Int) ->
 count_bits_1(0, Bits) -> Bits;
 count_bits_1(Int, Bits) -> count_bits_1(Int bsr 64, Bits+64).
 
-bin_expand_string(S, Line, Val, Size) when Size >= 2048 ->
+bin_expand_string(S, Line, Val, Size, Last) when Size >= ?COLLAPSE_MAX_SIZE_SEGMENT ->
     Combined = make_combined(Line, Val, Size),
-    [Combined|bin_expand_string(S, Line, 0, 0)];
-bin_expand_string([H|T], Line, Val, Size) ->
-    bin_expand_string(T, Line, (Val bsl 8) bor H, Size+8);
-bin_expand_string([], Line, Val, Size) ->
-    [make_combined(Line, Val, Size)].
+    [Combined|bin_expand_string(S, Line, 0, 0, Last)];
+bin_expand_string([H|T], Line, Val, Size, Last) ->
+    bin_expand_string(T, Line, (Val bsl 8) bor H, Size+8, Last);
+bin_expand_string([], Line, Val, Size, Last) ->
+    [make_combined(Line, Val, Size) | Last].
 
 make_combined(Line, Val, Size) ->
     {bin_element,Line,{integer,Line,Val},
@@ -2117,7 +2120,9 @@ pat_bin(Ps0, St) ->
     pat_segments(Ps, St).
 
 pat_bin_expand_strings(Es0) ->
-    foldr(fun ({bin_element,Line,{string,_,S},Sz,Ts}, Es1) ->
+    foldr(fun ({bin_element,Line,{string,_,[_|_]=S},default,default}, Es1) ->
+                  bin_expand_string(S, Line, 0, 0, Es1);
+              ({bin_element,Line,{string,_,S},Sz,Ts}, Es1) ->
                   foldr(
                     fun (C, Es) ->
                             [{bin_element,Line,{char,Line,C},Sz,Ts}|Es]
diff --git a/lib/compiler/src/v3_kernel.erl b/lib/compiler/src/v3_kernel.erl
index 4e2b8926bf..be7f9824fb 100644
--- a/lib/compiler/src/v3_kernel.erl
+++ b/lib/compiler/src/v3_kernel.erl
@@ -87,6 +87,8 @@
 
 -include("core_parse.hrl").
 -include("v3_kernel.hrl").
+
+%% Matches collapse max segment in v3_core.
 -define(EXPAND_MAX_SIZE_SEGMENT, 1024).
 
 %% These are not defined in v3_kernel.hrl.
-- 
2.26.2

openSUSE Build Service is sponsored by