Skip to content

Commit

Permalink
Merge pull request erlang#7933 from garazdawi/lukas/kernel/fix-slow-r…
Browse files Browse the repository at this point in the history
…ead_line/OTP-18910

kernel: Short-circuit when casting from list to list
  • Loading branch information
garazdawi authored Dec 20, 2023
2 parents 17640a0 + fc14619 commit db4796b
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/kernel/src/group.erl
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,7 @@ get_chars_loop(Pbs, M, F, Xa, Drv, Shell, Buf0, State, LineCont0, Encoding) ->
true ->
get_line(Buf0, Pbs, LineCont0, Drv, Shell, Encoding);
false ->
%% get_line_echo_off only deals with lists,
%% so convert to list before calling it.
get_line_echo_off(cast(Buf0, list), Encoding, Pbs, Drv, Shell)
get_line_echo_off(Buf0, Encoding, Pbs, Drv, Shell)
end,
case Result of
{done,LineCont1,Buf} ->
Expand Down Expand Up @@ -1076,15 +1074,24 @@ cast(eof, _, _) ->
eof;
cast(L, binary, ToEnc) ->
unicode:characters_to_binary(L, utf8, ToEnc);
cast(L, list, _ToEnc) when is_list(L) ->
L;
cast(L, list, _ToEnc) ->
unicode:characters_to_list(L, utf8).

append(eof, [], _) ->
eof;
append(eof, L, _) ->
L;
append(L, [], _) when is_list(L) ->
%% When doing ++ all of L needs to be traversed to check if it is
%% a proper list. Since we know it is a proper list we just return
%% the list without checking.
L;
append(L, A, _) when is_list(L) ->
L ++ A; %% We know L is valid unicode, so we just append the two
append(B, L, FromEnc) ->
unicode:characters_to_list(B, FromEnc) ++ L.
append(unicode:characters_to_list(B, FromEnc), L, FromEnc).

check_encoding(eof, _) ->
true;
Expand All @@ -1099,4 +1106,3 @@ is_latin1([]) ->
true;
is_latin1(_) ->
false.

0 comments on commit db4796b

Please sign in to comment.