Skip to content

Commit 697d738

Browse files
committed
inline unsafeMutateTail
1 parent a69826a commit 697d738

File tree

3 files changed

+91
-101
lines changed

3 files changed

+91
-101
lines changed

Diff for: jscomp/others/belt_List.ml

+25-29
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,15 @@ external mutableCell :
6969
'a -> 'a t -> 'a t = "#makemutablelist"
7070

7171

72-
type poly = { unsafeMutateTail : 'a . 'a t -> 'a t -> unit } [@@unboxed]
73-
74-
7572
(*
7673
[mutableCell x []] == [x]
7774
but tell the compiler that is a mutable cell, so it wont
7875
be mis-inlined in the future
7976
dont inline a binding to mutable cell, it is mutable
8077
*)
81-
(* relies on list internal representation *)
82-
let m : poly = {unsafeMutateTail = [%raw{|function(xs,ys){
83-
xs.tl = ys
84-
}|}]}
78+
(* INVARIANT: relies on Literals.tl (internal representation) *)
79+
external unsafeMutateTail : 'a t -> 'a t -> unit = "tl" [@@bs.set]
80+
8581

8682

8783

@@ -137,12 +133,12 @@ let rec partitionAux p cell precX precY =
137133
let next = mutableCell h [] in
138134
if p h [@bs] then
139135
begin
140-
m.unsafeMutateTail precX next ;
136+
unsafeMutateTail precX next ;
141137
partitionAux p t next precY
142138
end
143139
else
144140
begin
145-
m.unsafeMutateTail precY next ;
141+
unsafeMutateTail precY next ;
146142
partitionAux p t precX next
147143
end
148144

@@ -152,8 +148,8 @@ let rec splitAux cell precX precY =
152148
| (a,b)::t ->
153149
let nextA = mutableCell a [] in
154150
let nextB = mutableCell b [] in
155-
m.unsafeMutateTail precX nextA;
156-
m.unsafeMutateTail precY nextB;
151+
unsafeMutateTail precX nextA;
152+
unsafeMutateTail precY nextB;
157153
splitAux t nextA nextB
158154

159155
(* return the tail pointer so it can continue copy other
@@ -164,7 +160,7 @@ let rec copyAuxCont cellX prec =
164160
| [] -> prec
165161
| h::t ->
166162
let next = mutableCell h [] in
167-
m.unsafeMutateTail prec next ;
163+
unsafeMutateTail prec next ;
168164
copyAuxCont t next
169165

170166
let rec copyAuxWitFilter f cellX prec =
@@ -175,7 +171,7 @@ let rec copyAuxWitFilter f cellX prec =
175171
if f h [@bs] then
176172
begin
177173
let next = mutableCell h [] in
178-
m.unsafeMutateTail prec next ;
174+
unsafeMutateTail prec next ;
179175
copyAuxWitFilter f t next
180176
end
181177
else copyAuxWitFilter f t prec
@@ -188,7 +184,7 @@ let rec copyAuxWithFilterIndex f cellX prec i =
188184
if f h i [@bs] then
189185
begin
190186
let next = mutableCell h [] in
191-
m.unsafeMutateTail prec next ;
187+
unsafeMutateTail prec next ;
192188
copyAuxWithFilterIndex f t next (i + 1)
193189
end
194190
else copyAuxWithFilterIndex f t prec (i + 1)
@@ -202,7 +198,7 @@ let rec copyAuxWitFilterMap f cellX prec =
202198
| Some h ->
203199
begin
204200
let next = mutableCell h [] in
205-
m.unsafeMutateTail prec next ;
201+
unsafeMutateTail prec next ;
206202
copyAuxWitFilterMap f t next
207203
end
208204
| None -> copyAuxWitFilterMap f t prec
@@ -212,21 +208,21 @@ let rec removeAssocAuxWithMap cellX x prec f =
212208
| [] -> false
213209
| ((a,_) as h):: t ->
214210
if f a x [@bs] then
215-
(m.unsafeMutateTail prec t ; true)
211+
(unsafeMutateTail prec t ; true)
216212
else
217213
let next = mutableCell h [] in
218-
m.unsafeMutateTail prec next ;
214+
unsafeMutateTail prec next ;
219215
removeAssocAuxWithMap t x next f
220216

221217
let rec setAssocAuxWithMap cellX x k prec eq =
222218
match cellX with
223219
| [] -> false
224220
| ((a,_) as h) :: t ->
225221
if eq a x [@bs] then
226-
(m.unsafeMutateTail prec ( (x,k)::t); true)
222+
(unsafeMutateTail prec ( (x,k)::t); true)
227223
else
228224
let next = mutableCell h [] in
229-
m.unsafeMutateTail prec next ;
225+
unsafeMutateTail prec next ;
230226
setAssocAuxWithMap t x k next eq
231227

232228

@@ -236,15 +232,15 @@ let rec copyAuxWithMap cellX prec f =
236232
()
237233
| h::t ->
238234
let next = mutableCell (f h [@bs]) [] in
239-
m.unsafeMutateTail prec next ;
235+
unsafeMutateTail prec next ;
240236
copyAuxWithMap t next f
241237

242238

243239
let rec zipAux cellX cellY prec =
244240
match cellX, cellY with
245241
| h1::t1, h2::t2 ->
246242
let next = mutableCell ( h1, h2) [] in
247-
m.unsafeMutateTail prec next ;
243+
unsafeMutateTail prec next ;
248244
zipAux t1 t2 next
249245
| [],_ | _,[] ->
250246
()
@@ -253,7 +249,7 @@ let rec copyAuxWithMap2 f cellX cellY prec =
253249
match cellX, cellY with
254250
| h1::t1, h2::t2 ->
255251
let next = mutableCell (f h1 h2 [@bs]) [] in
256-
m.unsafeMutateTail prec next ;
252+
unsafeMutateTail prec next ;
257253
copyAuxWithMap2 f t1 t2 next
258254
| [],_ | _,[] ->
259255
()
@@ -262,7 +258,7 @@ let rec copyAuxWithMapI f i cellX prec =
262258
match cellX with
263259
| h::t ->
264260
let next = mutableCell (f i h [@bs]) [] in
265-
m.unsafeMutateTail prec next ;
261+
unsafeMutateTail prec next ;
266262
copyAuxWithMapI f (i + 1) t next
267263
| [] ->
268264
()
@@ -274,7 +270,7 @@ let rec takeAux n cell prec =
274270
| [] -> false
275271
| x::xs ->
276272
let cell = mutableCell x [] in
277-
m.unsafeMutateTail prec cell;
273+
unsafeMutateTail prec cell;
278274
takeAux (n - 1) xs cell
279275

280276
let rec splitAtAux n cell prec =
@@ -284,7 +280,7 @@ let rec splitAtAux n cell prec =
284280
| [] -> None
285281
| x::xs ->
286282
let cell = mutableCell x [] in
287-
m.unsafeMutateTail prec cell;
283+
unsafeMutateTail prec cell;
288284
splitAtAux (n - 1) xs cell
289285

290286
(* invarint [n >= 0] *)
@@ -332,7 +328,7 @@ let concat xs ys =
332328
| [] -> ys
333329
| h::t ->
334330
let cell = mutableCell h [] in
335-
m.unsafeMutateTail (copyAuxCont t cell) ys;
331+
unsafeMutateTail (copyAuxCont t cell) ys;
336332
cell
337333

338334
let mapU xs f =
@@ -374,7 +370,7 @@ let makeByU n f =
374370
let i = ref 1 in
375371
while i.contents < n do
376372
let v = mutableCell (f i.contents [@bs]) [] in
377-
m.unsafeMutateTail cur.contents v ;
373+
unsafeMutateTail cur.contents v ;
378374
cur.contents<- v ;
379375
i.contents <- i.contents + 1 ;
380376
done
@@ -391,7 +387,7 @@ let make (type a) n (v : a) : a list =
391387
let i = ref 1 in
392388
while i.contents < n do
393389
let v = mutableCell v [] in
394-
m.unsafeMutateTail cur.contents v ;
390+
unsafeMutateTail cur.contents v ;
395391
cur.contents<- v ;
396392
i.contents <- i.contents + 1 ;
397393
done
@@ -477,7 +473,7 @@ let reverse l = reverseConcat l []
477473

478474
let rec flattenAux prec xs =
479475
match xs with
480-
| [] -> m.unsafeMutateTail prec []
476+
| [] ->unsafeMutateTail prec []
481477
| h::r -> flattenAux (copyAuxCont h prec) r
482478

483479

0 commit comments

Comments
 (0)