diff options
author | Hans Bolinder <[email protected]> | 2019-02-12 14:51:08 +0100 |
---|---|---|
committer | Hans Bolinder <[email protected]> | 2019-02-14 13:00:15 +0100 |
commit | a1c5ccd3b5df9bcf7d6388937bb4bdc4767e4a4b (patch) | |
tree | ade754bb2454735b76d62b6dea3eb6ad2df7b62f /lib/stdlib/src/io_lib.erl | |
parent | 934f9974eb6bec43cd9445ec0f5019a4d1389428 (diff) | |
download | otp-a1c5ccd3b5df9bcf7d6388937bb4bdc4767e4a4b.tar.gz otp-a1c5ccd3b5df9bcf7d6388937bb4bdc4767e4a4b.tar.bz2 otp-a1c5ccd3b5df9bcf7d6388937bb4bdc4767e4a4b.zip |
stdlib: Optimize formatted printing of terms
Try calling iolist_size() before calling string:length().
The reason is that calls to string:length/1 are slow when the argument
is not a list of integers (or contains UNICODE).
Diffstat (limited to 'lib/stdlib/src/io_lib.erl')
-rw-r--r-- | lib/stdlib/src/io_lib.erl | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/lib/stdlib/src/io_lib.erl b/lib/stdlib/src/io_lib.erl index 8223a52873..2b5a374cf2 100644 --- a/lib/stdlib/src/io_lib.erl +++ b/lib/stdlib/src/io_lib.erl @@ -1,7 +1,7 @@ %% %% %CopyrightBegin% %% -%% Copyright Ericsson AB 1996-2018. All Rights Reserved. +%% Copyright Ericsson AB 1996-2019. All Rights Reserved. %% %% Licensed under the Apache License, Version 2.0 (the "License"); %% you may not use this file except in compliance with the License. @@ -87,6 +87,8 @@ -export([limit_term/2]). +-export([chars_length/1]). + -export_type([chars/0, latin1_string/0, continuation/0, fread_error/0, fread_item/0, format_spec/0, chars_limit/0]). @@ -1131,3 +1133,17 @@ test_limit_map_assoc(K, V, D) -> test_limit(V, D - 1). test_limit_bitstring(_, _) -> ok. + +-spec chars_length(chars()) -> non_neg_integer(). +%% Optimized for deep lists S such that deep_latin1_char_list(S) is +%% true. No binaries allowed! It is assumed that $\r is never followed +%% by $\n if S is an iolist() (string:length() assigns such a +%% sub-sequence length 1). +chars_length(S) -> + try + %% true = deep_latin1_char_list(S), + iolist_size(S) + catch + _:_ -> + string:length(S) + end. |