13.6% speed boost from avoiding string_of_int!

This commit is contained in:
Tony Garnock-Jones 2012-05-25 18:33:19 +01:00
parent 15da3e9927
commit e2f9e30b58
1 changed files with 17 additions and 3 deletions

View File

@ -30,11 +30,27 @@ and t =
let compare a b = Pervasives.compare a b
let digit_val c = (int_of_char c) - (int_of_char '0')
let val_digit n = char_of_int (n + 48)
let intstr =
let siz = 40 in
let buf = String.make siz (* enough for 128 bits *) ' ' in
function
| 0 -> "0"
| n ->
let rec loop n i =
if n = 0
then String.sub buf (siz - i) i
else (String.unsafe_set buf (siz - i - 1) (val_digit (n mod 10));
loop (n / 10) (i + 1))
in loop n 0
let generic_output_sexp write x =
let rec walk x =
match x with
| Str s ->
lwt () = write (string_of_int (String.length s)) in
lwt () = write (intstr (String.length s)) in
lwt () = write ":" in
write s
| Hint {hint = h; body = b} ->
@ -85,8 +101,6 @@ let rec output_sexp_human ch x =
let char_numeric c = '0' <= c && c <= '9'
let char_whitespace c = c <= ' '
let digit_val c = (int_of_char c) - (int_of_char '0')
let input_bytes ch count =
let buf = String.create count in (* mutable strings?!?! *)
lwt () = read_into_exactly ch buf 0 count in