Skip to content

Commit df43711

Browse files
authored
Merge pull request #3821 from BuckleScript/micro-buffer
micro-optimize buffer
2 parents 94adcd1 + 45048ef commit df43711

File tree

7 files changed

+166
-159
lines changed

7 files changed

+166
-159
lines changed

jscomp/ext/ext_buffer.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ let resize b more =
7171
this tricky function that is slow anyway. *)
7272
Bytes.blit b.buffer 0 new_buffer 0 b.position;
7373
b.buffer <- new_buffer;
74-
b.length <- !new_len
74+
b.length <- !new_len ;
75+
assert (b.position + more <= b.length)
7576

7677
let add_char b c =
7778
let pos = b.position in
@@ -84,7 +85,7 @@ let add_substring b s offset len =
8485
then invalid_arg "Ext_buffer.add_substring/add_subbytes";
8586
let new_position = b.position + len in
8687
if new_position > b.length then resize b len;
87-
Bytes.blit_string s offset b.buffer b.position len;
88+
Ext_bytes.unsafe_blit_string s offset b.buffer b.position len;
8889
b.position <- new_position
8990

9091

@@ -95,7 +96,7 @@ let add_string b s =
9596
let len = String.length s in
9697
let new_position = b.position + len in
9798
if new_position > b.length then resize b len;
98-
Bytes.blit_string s 0 b.buffer b.position len;
99+
Ext_bytes.unsafe_blit_string s 0 b.buffer b.position len;
99100
b.position <- new_position
100101

101102
(* TODO: micro-optimzie *)
@@ -105,7 +106,7 @@ let add_string_char b s c =
105106
let new_position = b.position + len in
106107
if new_position > b.length then resize b len;
107108
let b_buffer = b.buffer in
108-
Bytes.blit_string s 0 b_buffer b.position s_len;
109+
Ext_bytes.unsafe_blit_string s 0 b_buffer b.position s_len;
109110
Bytes.unsafe_set b_buffer (new_position - 1) c;
110111
b.position <- new_position
111112

@@ -117,7 +118,7 @@ let add_char_string b c s =
117118
let b_buffer = b.buffer in
118119
let b_position = b.position in
119120
Bytes.unsafe_set b_buffer b_position c ;
120-
Bytes.blit_string s 0 b_buffer (b_position + 1) s_len;
121+
Ext_bytes.unsafe_blit_string s 0 b_buffer (b_position + 1) s_len;
121122
b.position <- new_position
122123

123124

lib/4.02.3/bsb.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6221,7 +6221,8 @@ let resize b more =
62216221
this tricky function that is slow anyway. *)
62226222
Bytes.blit b.buffer 0 new_buffer 0 b.position;
62236223
b.buffer <- new_buffer;
6224-
b.length <- !new_len
6224+
b.length <- !new_len ;
6225+
assert (b.position + more <= b.length)
62256226

62266227
let add_char b c =
62276228
let pos = b.position in
@@ -6234,7 +6235,7 @@ let add_substring b s offset len =
62346235
then invalid_arg "Ext_buffer.add_substring/add_subbytes";
62356236
let new_position = b.position + len in
62366237
if new_position > b.length then resize b len;
6237-
Bytes.blit_string s offset b.buffer b.position len;
6238+
Ext_bytes.unsafe_blit_string s offset b.buffer b.position len;
62386239
b.position <- new_position
62396240

62406241

@@ -6245,7 +6246,7 @@ let add_string b s =
62456246
let len = String.length s in
62466247
let new_position = b.position + len in
62476248
if new_position > b.length then resize b len;
6248-
Bytes.blit_string s 0 b.buffer b.position len;
6249+
Ext_bytes.unsafe_blit_string s 0 b.buffer b.position len;
62496250
b.position <- new_position
62506251

62516252
(* TODO: micro-optimzie *)
@@ -6255,7 +6256,7 @@ let add_string_char b s c =
62556256
let new_position = b.position + len in
62566257
if new_position > b.length then resize b len;
62576258
let b_buffer = b.buffer in
6258-
Bytes.blit_string s 0 b_buffer b.position s_len;
6259+
Ext_bytes.unsafe_blit_string s 0 b_buffer b.position s_len;
62596260
Bytes.unsafe_set b_buffer (new_position - 1) c;
62606261
b.position <- new_position
62616262

@@ -6267,7 +6268,7 @@ let add_char_string b c s =
62676268
let b_buffer = b.buffer in
62686269
let b_position = b.position in
62696270
Bytes.unsafe_set b_buffer b_position c ;
6270-
Bytes.blit_string s 0 b_buffer (b_position + 1) s_len;
6271+
Ext_bytes.unsafe_blit_string s 0 b_buffer (b_position + 1) s_len;
62716272
b.position <- new_position
62726273

62736274

lib/4.02.3/bsb_helper.ml

Lines changed: 130 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,130 @@ let string_of_bsb_dev_include i =
119119

120120

121121
let reset () = dir_index := 0
122+
end
123+
module Ext_bytes : sig
124+
#1 "ext_bytes.mli"
125+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
126+
*
127+
* This program is free software: you can redistribute it and/or modify
128+
* it under the terms of the GNU Lesser General Public License as published by
129+
* the Free Software Foundation, either version 3 of the License, or
130+
* (at your option) any later version.
131+
*
132+
* In addition to the permissions granted to you by the LGPL, you may combine
133+
* or link a "work that uses the Library" with a publicly distributed version
134+
* of this file to produce a combined library or application, then distribute
135+
* that combined work under the terms of your choosing, with no requirement
136+
* to comply with the obligations normally placed on you by section 4 of the
137+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
138+
* should you choose to use a later version).
139+
*
140+
* This program is distributed in the hope that it will be useful,
141+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
142+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
143+
* GNU Lesser General Public License for more details.
144+
*
145+
* You should have received a copy of the GNU Lesser General Public License
146+
* along with this program; if not, write to the Free Software
147+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
148+
149+
150+
151+
152+
153+
external unsafe_blit_string : string -> int -> bytes -> int -> int -> unit
154+
= "caml_blit_string"
155+
156+
"noalloc"
157+
158+
159+
160+
161+
(** Port the {!Bytes.escaped} from trunk to make it not locale sensitive *)
162+
163+
val escaped : bytes -> bytes
164+
165+
end = struct
166+
#1 "ext_bytes.ml"
167+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
168+
*
169+
* This program is free software: you can redistribute it and/or modify
170+
* it under the terms of the GNU Lesser General Public License as published by
171+
* the Free Software Foundation, either version 3 of the License, or
172+
* (at your option) any later version.
173+
*
174+
* In addition to the permissions granted to you by the LGPL, you may combine
175+
* or link a "work that uses the Library" with a publicly distributed version
176+
* of this file to produce a combined library or application, then distribute
177+
* that combined work under the terms of your choosing, with no requirement
178+
* to comply with the obligations normally placed on you by section 4 of the
179+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
180+
* should you choose to use a later version).
181+
*
182+
* This program is distributed in the hope that it will be useful,
183+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
184+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
185+
* GNU Lesser General Public License for more details.
186+
*
187+
* You should have received a copy of the GNU Lesser General Public License
188+
* along with this program; if not, write to the Free Software
189+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
190+
191+
192+
193+
194+
195+
196+
197+
external unsafe_blit_string : string -> int -> bytes -> int -> int -> unit
198+
= "caml_blit_string"
199+
200+
"noalloc"
201+
202+
203+
external char_code: char -> int = "%identity"
204+
external char_chr: int -> char = "%identity"
205+
206+
let escaped s =
207+
let n = Pervasives.ref 0 in
208+
for i = 0 to Bytes.length s - 1 do
209+
n := !n +
210+
(match Bytes.unsafe_get s i with
211+
| '"' | '\\' | '\n' | '\t' | '\r' | '\b' -> 2
212+
| ' ' .. '~' -> 1
213+
| _ -> 4)
214+
done;
215+
if !n = Bytes.length s then Bytes.copy s else begin
216+
let s' = Bytes.create !n in
217+
n := 0;
218+
for i = 0 to Bytes.length s - 1 do
219+
begin match Bytes.unsafe_get s i with
220+
| ('"' | '\\') as c ->
221+
Bytes.unsafe_set s' !n '\\'; incr n; Bytes.unsafe_set s' !n c
222+
| '\n' ->
223+
Bytes.unsafe_set s' !n '\\'; incr n; Bytes.unsafe_set s' !n 'n'
224+
| '\t' ->
225+
Bytes.unsafe_set s' !n '\\'; incr n; Bytes.unsafe_set s' !n 't'
226+
| '\r' ->
227+
Bytes.unsafe_set s' !n '\\'; incr n; Bytes.unsafe_set s' !n 'r'
228+
| '\b' ->
229+
Bytes.unsafe_set s' !n '\\'; incr n; Bytes.unsafe_set s' !n 'b'
230+
| (' ' .. '~') as c -> Bytes.unsafe_set s' !n c
231+
| c ->
232+
let a = char_code c in
233+
Bytes.unsafe_set s' !n '\\';
234+
incr n;
235+
Bytes.unsafe_set s' !n (char_chr (48 + a / 100));
236+
incr n;
237+
Bytes.unsafe_set s' !n (char_chr (48 + (a / 10) mod 10));
238+
incr n;
239+
Bytes.unsafe_set s' !n (char_chr (48 + a mod 10));
240+
end;
241+
incr n
242+
done;
243+
s'
244+
end
245+
122246
end
123247
module Ext_buffer : sig
124248
#1 "ext_buffer.mli"
@@ -311,7 +435,8 @@ let resize b more =
311435
this tricky function that is slow anyway. *)
312436
Bytes.blit b.buffer 0 new_buffer 0 b.position;
313437
b.buffer <- new_buffer;
314-
b.length <- !new_len
438+
b.length <- !new_len ;
439+
assert (b.position + more <= b.length)
315440

316441
let add_char b c =
317442
let pos = b.position in
@@ -324,7 +449,7 @@ let add_substring b s offset len =
324449
then invalid_arg "Ext_buffer.add_substring/add_subbytes";
325450
let new_position = b.position + len in
326451
if new_position > b.length then resize b len;
327-
Bytes.blit_string s offset b.buffer b.position len;
452+
Ext_bytes.unsafe_blit_string s offset b.buffer b.position len;
328453
b.position <- new_position
329454

330455

@@ -335,7 +460,7 @@ let add_string b s =
335460
let len = String.length s in
336461
let new_position = b.position + len in
337462
if new_position > b.length then resize b len;
338-
Bytes.blit_string s 0 b.buffer b.position len;
463+
Ext_bytes.unsafe_blit_string s 0 b.buffer b.position len;
339464
b.position <- new_position
340465

341466
(* TODO: micro-optimzie *)
@@ -345,7 +470,7 @@ let add_string_char b s c =
345470
let new_position = b.position + len in
346471
if new_position > b.length then resize b len;
347472
let b_buffer = b.buffer in
348-
Bytes.blit_string s 0 b_buffer b.position s_len;
473+
Ext_bytes.unsafe_blit_string s 0 b_buffer b.position s_len;
349474
Bytes.unsafe_set b_buffer (new_position - 1) c;
350475
b.position <- new_position
351476

@@ -357,7 +482,7 @@ let add_char_string b c s =
357482
let b_buffer = b.buffer in
358483
let b_position = b.position in
359484
Bytes.unsafe_set b_buffer b_position c ;
360-
Bytes.blit_string s 0 b_buffer (b_position + 1) s_len;
485+
Ext_bytes.unsafe_blit_string s 0 b_buffer (b_position + 1) s_len;
361486
b.position <- new_position
362487

363488

@@ -2047,130 +2172,6 @@ let write_file f content =
20472172
output_string oc content
20482173
end
20492174

2050-
end
2051-
module Ext_bytes : sig
2052-
#1 "ext_bytes.mli"
2053-
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2054-
*
2055-
* This program is free software: you can redistribute it and/or modify
2056-
* it under the terms of the GNU Lesser General Public License as published by
2057-
* the Free Software Foundation, either version 3 of the License, or
2058-
* (at your option) any later version.
2059-
*
2060-
* In addition to the permissions granted to you by the LGPL, you may combine
2061-
* or link a "work that uses the Library" with a publicly distributed version
2062-
* of this file to produce a combined library or application, then distribute
2063-
* that combined work under the terms of your choosing, with no requirement
2064-
* to comply with the obligations normally placed on you by section 4 of the
2065-
* LGPL version 3 (or the corresponding section of a later version of the LGPL
2066-
* should you choose to use a later version).
2067-
*
2068-
* This program is distributed in the hope that it will be useful,
2069-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2070-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2071-
* GNU Lesser General Public License for more details.
2072-
*
2073-
* You should have received a copy of the GNU Lesser General Public License
2074-
* along with this program; if not, write to the Free Software
2075-
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2076-
2077-
2078-
2079-
2080-
2081-
external unsafe_blit_string : string -> int -> bytes -> int -> int -> unit
2082-
= "caml_blit_string"
2083-
2084-
"noalloc"
2085-
2086-
2087-
2088-
2089-
(** Port the {!Bytes.escaped} from trunk to make it not locale sensitive *)
2090-
2091-
val escaped : bytes -> bytes
2092-
2093-
end = struct
2094-
#1 "ext_bytes.ml"
2095-
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2096-
*
2097-
* This program is free software: you can redistribute it and/or modify
2098-
* it under the terms of the GNU Lesser General Public License as published by
2099-
* the Free Software Foundation, either version 3 of the License, or
2100-
* (at your option) any later version.
2101-
*
2102-
* In addition to the permissions granted to you by the LGPL, you may combine
2103-
* or link a "work that uses the Library" with a publicly distributed version
2104-
* of this file to produce a combined library or application, then distribute
2105-
* that combined work under the terms of your choosing, with no requirement
2106-
* to comply with the obligations normally placed on you by section 4 of the
2107-
* LGPL version 3 (or the corresponding section of a later version of the LGPL
2108-
* should you choose to use a later version).
2109-
*
2110-
* This program is distributed in the hope that it will be useful,
2111-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2112-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2113-
* GNU Lesser General Public License for more details.
2114-
*
2115-
* You should have received a copy of the GNU Lesser General Public License
2116-
* along with this program; if not, write to the Free Software
2117-
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2118-
2119-
2120-
2121-
2122-
2123-
2124-
2125-
external unsafe_blit_string : string -> int -> bytes -> int -> int -> unit
2126-
= "caml_blit_string"
2127-
2128-
"noalloc"
2129-
2130-
2131-
external char_code: char -> int = "%identity"
2132-
external char_chr: int -> char = "%identity"
2133-
2134-
let escaped s =
2135-
let n = Pervasives.ref 0 in
2136-
for i = 0 to Bytes.length s - 1 do
2137-
n := !n +
2138-
(match Bytes.unsafe_get s i with
2139-
| '"' | '\\' | '\n' | '\t' | '\r' | '\b' -> 2
2140-
| ' ' .. '~' -> 1
2141-
| _ -> 4)
2142-
done;
2143-
if !n = Bytes.length s then Bytes.copy s else begin
2144-
let s' = Bytes.create !n in
2145-
n := 0;
2146-
for i = 0 to Bytes.length s - 1 do
2147-
begin match Bytes.unsafe_get s i with
2148-
| ('"' | '\\') as c ->
2149-
Bytes.unsafe_set s' !n '\\'; incr n; Bytes.unsafe_set s' !n c
2150-
| '\n' ->
2151-
Bytes.unsafe_set s' !n '\\'; incr n; Bytes.unsafe_set s' !n 'n'
2152-
| '\t' ->
2153-
Bytes.unsafe_set s' !n '\\'; incr n; Bytes.unsafe_set s' !n 't'
2154-
| '\r' ->
2155-
Bytes.unsafe_set s' !n '\\'; incr n; Bytes.unsafe_set s' !n 'r'
2156-
| '\b' ->
2157-
Bytes.unsafe_set s' !n '\\'; incr n; Bytes.unsafe_set s' !n 'b'
2158-
| (' ' .. '~') as c -> Bytes.unsafe_set s' !n c
2159-
| c ->
2160-
let a = char_code c in
2161-
Bytes.unsafe_set s' !n '\\';
2162-
incr n;
2163-
Bytes.unsafe_set s' !n (char_chr (48 + a / 100));
2164-
incr n;
2165-
Bytes.unsafe_set s' !n (char_chr (48 + (a / 10) mod 10));
2166-
incr n;
2167-
Bytes.unsafe_set s' !n (char_chr (48 + a mod 10));
2168-
end;
2169-
incr n
2170-
done;
2171-
s'
2172-
end
2173-
21742175
end
21752176
module Ext_char : sig
21762177
#1 "ext_char.mli"

0 commit comments

Comments
 (0)