Skip to content

Commit 05648d0

Browse files
committed
stdlib: Use string:slice in flat_trunc for O(N) truncation
The latin1 list clause in flat_trunc/3 used lists:flatten + lists:split which materializes the entire input (O(L)) just to take N chars. Replace with string:slice/3 which is O(N), matching the existing unicode clause. Also merge both list clauses into one since string:slice handles both encodings.
1 parent 53080e2 commit 05648d0

1 file changed

Lines changed: 3 additions & 6 deletions

File tree

lib/stdlib/src/io_lib_format.erl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,14 +1066,11 @@ adjust(Data, Pad, right) -> [Pad|Data].
10661066

10671067
%% Flatten and truncate a deep list to at most N elements.
10681068

1069-
flat_trunc(List, N, latin1) when is_list(List), is_integer(N), N >= 0 ->
1070-
{S, _} = lists:split(N, lists:flatten(List)),
1071-
S;
1072-
flat_trunc(Str, N, unicode) when is_integer(N), N >= 0 ->
1073-
string:slice(Str, 0, N);
10741069
flat_trunc(Bin, N, latin1) when is_binary(Bin), is_integer(N), N >= 0 ->
10751070
{B, _} = split_binary(Bin, N),
1076-
B.
1071+
B;
1072+
flat_trunc(Str, N, _) when is_integer(N), N >= 0 ->
1073+
string:slice(Str, 0, N).
10771074

10781075
%% A deep version of lists:duplicate/2
10791076

0 commit comments

Comments
 (0)