Skip to content

Commit

Permalink
[enhance] compression: improved gzip compression
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederic Ye committed Feb 17, 2012
1 parent d21e7fb commit 9e47c01
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
14 changes: 7 additions & 7 deletions libbase/sgzip.ml
Expand Up @@ -33,13 +33,13 @@ type out_channel =
mutable out_pos: int;
mutable out_avail: int;
out_stream: Zlib.stream;
mutable out_string: string;
out_string: Buffer.t;
mutable out_size: int32;
mutable out_crc: int32;
only_deflate : bool }

let output_byte oz b =
oz.out_string <- Printf.sprintf "%s%c" oz.out_string (Char.unsafe_chr b)
Buffer.add_char oz.out_string (Char.unsafe_chr b)

let open_out ?(level = 6) ?(only_deflate=false) () =
if level < 1 || level > 9 then invalid_arg "Gzip.open_out: bad level";
Expand All @@ -48,7 +48,7 @@ let open_out ?(level = 6) ?(only_deflate=false) () =
out_pos = 0;
out_avail = buffer_size;
out_stream = Zlib.deflate_init level false;
out_string = "";
out_string = Buffer.create buffer_size;
out_size = Int32.zero;
out_crc = Int32.zero;
only_deflate = only_deflate } in
Expand All @@ -69,7 +69,7 @@ let rec output oz buf pos len =
invalid_arg "Gzip.output";
(* If output buffer is full, flush it *)
if oz.out_avail = 0 then begin
oz.out_string <- Printf.sprintf "%s%s" oz.out_string (String.sub oz.out_buffer 0 oz.out_pos);
Buffer.add_substring oz.out_string oz.out_buffer 0 oz.out_pos;
oz.out_pos <- 0;
oz.out_avail <- String.length oz.out_buffer
end;
Expand Down Expand Up @@ -98,15 +98,15 @@ let output_byte oz b =
let write_int32 oz n =
let r = ref n in
for i = 1 to 4 do
oz.out_string <- Printf.sprintf "%s%c" oz.out_string (Char.unsafe_chr (Int32.to_int !r));
Buffer.add_char oz.out_string (Char.unsafe_chr (Int32.to_int !r));
r := Int32.shift_right_logical !r 8
done

let flush oz =
let rec do_flush () =
(* If output buffer is full, flush it *)
if oz.out_avail = 0 then begin
oz.out_string <- Printf.sprintf "%s%s" oz.out_string (String.sub oz.out_buffer 0 oz.out_pos);
Buffer.add_substring oz.out_string oz.out_buffer 0 oz.out_pos;
oz.out_pos <- 0;
oz.out_avail <- String.length oz.out_buffer
end;
Expand All @@ -120,7 +120,7 @@ let flush oz =
do_flush();
(* Final data flush *)
if oz.out_pos > 0 then
oz.out_string <- Printf.sprintf "%s%s" oz.out_string (String.sub oz.out_buffer 0 oz.out_pos);
Buffer.add_substring oz.out_string oz.out_buffer 0 oz.out_pos;
(* Write CRC and size *)
if not oz.only_deflate then (
write_int32 oz oz.out_crc;
Expand Down
3 changes: 2 additions & 1 deletion libnet/http/compression.ml
Expand Up @@ -160,7 +160,8 @@ let compress_content sched gzip deflate compression_level cache_response content
let oc = Sgzip.open_out ~level:compression_level ~only_deflate:deflate () in
let return () =
Sgzip.close_out oc;
let c_len, f_res = (String.length oc.Sgzip.out_string), oc.Sgzip.out_string in
let s = Buffer.contents oc.Sgzip.out_string in
let c_len, f_res = (String.length s), s in
(* Logger.log (sprintf "CACHE: OLD LEN %d : NEW LEN %d" content_len c_len); *)
if content_len > c_len then (
if cache_response then
Expand Down

0 comments on commit 9e47c01

Please sign in to comment.