Skip to content

Commit

Permalink
taking a look at what set_size_unsafe would do.
Browse files Browse the repository at this point in the history
  • Loading branch information
Romain Slootmaekers committed Jun 1, 2012
1 parent 58938b5 commit 3de9ed0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/baardskeerder_util.c
@@ -0,0 +1,23 @@
#include <stdio.h>
#include <stdint.h>
#include <caml/memory.h>
#include <caml/mlvalues.h>

value Caml_set_size_unsafe(value v_buf, value v_size){
CAMLparam2(v_buf, v_size);
char * buf = String_val(v_buf);
int * ibuf = (int*) buf;
int size = Long_val(v_size);
ibuf[0] = size;
CAMLreturn(Val_unit);
}

value Caml_size_from_unsafe(value v_buf, value v_pos){
CAMLparam2(v_buf,v_pos);
char * buf = String_val(v_buf);
int pos = Long_val(v_pos);
int * ibuf = (int*) buf;
int size = ibuf[pos];
value v_size = Val_long(size);
CAMLreturn(v_size);
}
2 changes: 2 additions & 0 deletions src/cpack.ml
@@ -0,0 +1,2 @@
external set_size_unsafe : string -> int -> unit = "Caml_set_size_unsafe"
external size_from_unsafe: string -> int -> int = "Caml_size_from_unsafe"
1 change: 1 addition & 0 deletions src/libbaardskeerder_c.clib
Expand Up @@ -2,3 +2,4 @@ posix.o
bsd_crc32c.o
arakoon_crc32c.o
cpudetect.o
baardskeerder_util.o
40 changes: 39 additions & 1 deletion src/pack_test.ml
Expand Up @@ -52,7 +52,9 @@ let size_performance() =
(fun v ->
set_size s v;
let v' = size_from s 0 in
OUnit.assert_equal ~printer:string_of_int v v';
(* OUnit.assert_equal ~printer:string_of_int v v'; *)
assert (v' = v);
()
) sample
in
loop (i-1)
Expand All @@ -62,8 +64,44 @@ let size_performance() =
let mega_da = da /. (1024.0 *. 1024.0) in
Printf.printf "\nn=%i;dt=%fs;da=%fMB\n" n dt mega_da


let size2_correctness () =
let s = "xxxxxxxx" in
let () = Array.iter
(fun v ->
Cpack.set_size_unsafe s v;
let v_ocaml = size_from s 0 in
let () = Printf.printf "%08i:%S\n%!" v_ocaml s in
let v_c = Cpack.size_from_unsafe s 0 in
OUnit.assert_equal ~printer:string_of_int v_ocaml v_c ~msg:(Printf.sprintf "%S: " s);
OUnit.assert_equal ~printer:string_of_int v v_c ~msg:(Printf.sprintf "%S: " s)
) sample
in
()
let size2_performance() =
let s = "xxxxxxxx" in
let rec loop i =
if i = 0 then ()
else
let () = Array.iter
(fun v ->
Cpack.set_size_unsafe s v;
let v' = Cpack.size_from_unsafe s 0 in
(*OUnit.assert_equal ~printer:string_of_int v v';*)
assert (v = v');
()
) sample
in
loop (i - 1)
in
let n = 10 * 1000 * 1000 in
let dt,da = measure (fun () -> loop n) in
let mega_da = da /. (1024.0 *. 1024.0) in
Printf.printf "\nn=%i;dt=%fs;da=%fMB\n" n dt mega_da
let suite =
"Pack">:::[
"vint_to_performance" >:: vint_to_performance;
"size_performance" >:: size_performance;
"size2_correctness" >:: size2_correctness;
"size2_performance" >:: size2_performance;
]

0 comments on commit 3de9ed0

Please sign in to comment.