Skip to content

Commit ca6e129

Browse files
committed
Simplify Static_const.apply_renaming
1 parent a434cd1 commit ca6e129

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

middle_end/flambda2/terms/static_const.ml

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -224,35 +224,19 @@ let apply_renaming t renaming =
224224
if or_var == or_var' then t else Boxed_nativeint or_var'
225225
| Mutable_string { initial_value = _ } | Immutable_string _ -> t
226226
| Immutable_float_block fields ->
227-
let changed = ref false in
228-
let fields =
229-
List.map
230-
(fun (field : _ Or_variable.t) ->
231-
let field' : _ Or_variable.t =
232-
match field with
233-
| Var (v, dbg) -> Var (Renaming.apply_variable renaming v, dbg)
234-
| Const _ -> field
235-
in
236-
if not (field == field') then changed := true;
237-
field')
227+
let fields' =
228+
Misc.Stdlib.List.map_sharing
229+
(fun field -> Or_variable.apply_renaming field renaming)
238230
fields
239231
in
240-
if not !changed then t else Immutable_float_block fields
232+
if fields == fields' then t else Immutable_float_block fields
241233
| Immutable_float_array fields ->
242-
let changed = ref false in
243-
let fields =
244-
List.map
245-
(fun (field : _ Or_variable.t) ->
246-
let field' : _ Or_variable.t =
247-
match field with
248-
| Var (v, dbg) -> Var (Renaming.apply_variable renaming v, dbg)
249-
| Const _ -> field
250-
in
251-
if not (field == field') then changed := true;
252-
field')
234+
let fields' =
235+
Misc.Stdlib.List.map_sharing
236+
(fun field -> Or_variable.apply_renaming field renaming)
253237
fields
254238
in
255-
if not !changed then t else Immutable_float_array fields
239+
if fields == fields' then t else Immutable_float_array fields
256240
| Empty_array -> Empty_array
257241

258242
let all_ids_for_export t =

ocaml/utils/misc.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,17 @@ module Stdlib = struct
174174
}
175175
in
176176
find_prefix ~longest_common_prefix_rev:[] first second
177+
178+
let rec map_sharing f l =
179+
match l with
180+
| [] -> l
181+
| h :: t ->
182+
let new_t = map_sharing f t in
183+
let new_h = f h in
184+
if h == new_h && t == new_t then
185+
l
186+
else
187+
new_h :: new_t
177188
end
178189

179190
module Option = struct

ocaml/utils/misc.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ module Stdlib : sig
138138
(** Returns the longest list that, with respect to the provided equality
139139
function, is a prefix of both of the given lists. The input lists,
140140
each with such longest common prefix removed, are also returned. *)
141+
142+
val map_sharing : ('a -> 'a) -> 'a list -> 'a list
143+
(** [map_sharing f l] is [List.map f l].
144+
If for each [x] in [l], [f x == x] then [map_sharing f l == l] *)
141145
end
142146

143147
module Option : sig

0 commit comments

Comments
 (0)