Skip to content

Commit e8a6c2e

Browse files
jvillardfacebook-github-bot
authored andcommitted
[CLOpt] replace uses of Str with Core.String
Summary: Running `infer --help` with spacetime showed that `Str` was a hog. And indeed perf is 74% better now: ``` before: bash -c 'for i in $(seq 100); do infer --help > /dev/null 2>/dev/null; done' 28.02s user 0.62s system 98% cpu 29.205 total after: bash -c 'for i in $(seq 100); do infer --help > /dev/null 2>/dev/null; done' 6.34s user 0.64s system 92% cpu 7.557 total ``` Running on many files one after the other is now 64% faster too: ``` $ cd infer/tests/codetoanalyze/c/errors $ # before $ rm -fr infer-out && time bash -c 'for src in */*.c; do infer -a capture --continue -- clang -c $src; done' bash -c 7.77s user 0.66s system 97% cpu 8.647 total $ # after $ rm -fr infer-out && time bash -c 'for src in */*.c; do infer -a capture --continue -- clang -c $src; done' bash -c 2.35s user 0.56s system 93% cpu 3.119 total $ time infer -a capture -- clang -c */*.c infer -a capture -- clang -c */*.c 0.54s user 0.20s system 99% cpu 0.737 total ``` Reviewed By: mbouaziz Differential Revision: D4875803 fbshipit-source-id: cfcfa69
1 parent 18374a3 commit e8a6c2e

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

infer/src/base/CommandLineOption.ml

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,17 @@ let xdesc {long; short; spec; doc} =
162162

163163
let wrap_line indent_string wrap_length line =
164164
let indent_length = String.length indent_string in
165-
let word_sep = " " in
166-
let words = Str.split (Str.regexp_string word_sep) line in
165+
let word_sep = ' ' in
166+
let words = String.split ~on:word_sep line in
167+
let word_sep_str = String.of_char word_sep in
167168
let add_word_to_paragraph (rev_lines, non_empty, line, line_length) word =
168169
let word_length = String.length word in
169-
let new_length = line_length + (String.length word_sep) + word_length in
170+
let new_length = line_length + (String.length word_sep_str) + word_length in
170171
let new_non_empty = non_empty || word <> "" in
171172
if new_length > wrap_length && non_empty then
172173
(line::rev_lines, true, indent_string ^ word, indent_length + word_length)
173174
else
174-
let sep = if Int.equal line_length indent_length then "" else word_sep in
175+
let sep = if Int.equal line_length indent_length then "" else word_sep_str in
175176
let new_line = line ^ sep ^ word in
176177
if new_length > wrap_length && new_non_empty then
177178
(new_line::rev_lines, false, indent_string, indent_length)
@@ -189,9 +190,11 @@ let pad_and_xform doc_width left_width desc =
189190
let indent_doc doc =
190191
(* 2 blank columns before option + 2 columns of gap between flag and doc *)
191192
let left_indent = 4 + left_width in
193+
let newline_padding = "\n" ^ String.make left_indent ' ' in
192194
(* align every line after the first one of [doc] *)
193-
let doc = Str.global_replace (Str.regexp_string "\n")
194-
("\n" ^ String.make left_indent ' ') doc in
195+
let doc = String.concat_map doc ~f:(function
196+
| '\n' -> newline_padding
197+
| c -> String.of_char c) in
195198
(* align the first line of [doc] *)
196199
let short_meta = short_meta desc in
197200
let gap = left_width - (left_length long short_meta) in
@@ -201,7 +204,7 @@ let pad_and_xform doc_width left_width desc =
201204
short_meta ^ (String.make (gap + 1) ' ') ^ doc
202205
in
203206
let wrapped_lines =
204-
let lines = Str.split (Str.regexp_string "\n") doc in
207+
let lines = String.split ~on:'\n' doc in
205208
let wrap_line s =
206209
if String.length s > doc_width then
207210
wrap_line "" doc_width s
@@ -550,7 +553,7 @@ let mk_symbol_seq ?(default=[]) ~symbols ~eq ?(deprecated=[]) ~long ?short ?pars
550553
mk ~deprecated ~long ?short ~default ?parse_mode ~meta:(",-separated sequence" ^ meta) doc
551554
~default_to_string:(fun syms -> String.concat ~sep:" " (List.map ~f:to_string syms))
552555
~mk_setter:(fun var str_seq ->
553-
var := List.map ~f:of_string (Str.split (Str.regexp_string ",") str_seq))
556+
var := List.map ~f:of_string (String.split ~on:',' str_seq))
554557
~decode_json:(fun ~inferconfig_dir:_ json ->
555558
[dashdash long;
556559
String.concat ~sep:"," (YBU.convert_each YBU.to_string json)])
@@ -620,7 +623,10 @@ let set_curr_speclist_for_parse_action ~usage ?(parse_all=false) parse_action =
620623
if len > 3 && String.sub s ~pos:0 ~len:3 = "no-"
621624
then String.sub s ~pos:3 ~len:(len - 3)
622625
else s in
623-
let remove_weird_chars = Str.global_replace (Str.regexp "[^a-z0-9-]") "" in
626+
let remove_weird_chars =
627+
String.filter ~f:(function
628+
| 'a'..'z' | '0'..'9' | '-' -> true
629+
| _ -> false) in
624630
remove_weird_chars @@ String.lowercase @@ remove_no k in
625631
let compare_specs {long = x} {long = y} =
626632
match x, y with
@@ -759,7 +765,7 @@ let encode_argv_to_env argv =
759765
) argv)
760766

761767
let decode_env_to_argv env =
762-
Str.split (Str.regexp_string (String.make 1 env_var_sep)) env
768+
String.split ~on:env_var_sep env |> List.filter ~f:(Fn.non String.is_empty)
763769

764770
(** [prefix_before_rest (prefix @ ["--" :: rest])] is [prefix] where "--" is not in [prefix]. *)
765771
let rev_prefix_before_rest args =

0 commit comments

Comments
 (0)