Skip to content

Commit c627f27

Browse files
TIHancartermp
authored andcommitted
Inline List.iter/List.iteri for performance (#8176)
1 parent 6cec616 commit c627f27

File tree

4 files changed

+6
-17
lines changed

4 files changed

+6
-17
lines changed

src/fsharp/FSharp.Core/list.fs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ namespace Microsoft.FSharp.Collections
100100
loop ([], state) (rev list)
101101

102102
[<CompiledName("Iterate")>]
103-
let iter action list = Microsoft.FSharp.Primitives.Basics.List.iter action list
103+
let inline iter action (list:'T list) = for x in list do action x
104104

105105
[<CompiledName("Distinct")>]
106106
let distinct (list:'T list) = Microsoft.FSharp.Primitives.Basics.List.distinctWithComparer HashIdentity.Structural<'T> list
@@ -164,7 +164,9 @@ namespace Microsoft.FSharp.Collections
164164
let takeWhile predicate (list: 'T list) = Microsoft.FSharp.Primitives.Basics.List.takeWhile predicate list
165165

166166
[<CompiledName("IterateIndexed")>]
167-
let iteri action list = Microsoft.FSharp.Primitives.Basics.List.iteri action list
167+
let inline iteri action (list: 'T list) =
168+
let mutable n = 0
169+
for x in list do action n x; n <- n + 1
168170

169171
[<CompiledName("Initialize")>]
170172
let init length initializer = Microsoft.FSharp.Primitives.Basics.List.init length initializer

src/fsharp/FSharp.Core/list.fsi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ namespace Microsoft.FSharp.Collections
380380
/// <param name="action">The function to apply to elements from the input list.</param>
381381
/// <param name="list">The input list.</param>
382382
[<CompiledName("Iterate")>]
383-
val iter: action:('T -> unit) -> list:'T list -> unit
383+
val inline iter: action:('T -> unit) -> list:'T list -> unit
384384

385385
/// <summary>Applies the given function to two collections simultaneously. The
386386
/// collections must have identical size.</summary>
@@ -395,7 +395,7 @@ namespace Microsoft.FSharp.Collections
395395
/// <param name="action">The function to apply to the elements of the list along with their index.</param>
396396
/// <param name="list">The input list.</param>
397397
[<CompiledName("IterateIndexed")>]
398-
val iteri: action:(int -> 'T -> unit) -> list:'T list -> unit
398+
val inline iteri: action:(int -> 'T -> unit) -> list:'T list -> unit
399399

400400
/// <summary>Applies the given function to two collections simultaneously. The
401401
/// collections must have identical size. The integer passed to the

src/fsharp/FSharp.Core/local.fs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ module internal List =
8888
[<SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")>]
8989
let nonempty x = match x with [] -> false | _ -> true
9090

91-
let rec iter f x = match x with [] -> () | h :: t -> f h; iter f t
92-
9391
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
9492
// tail cons cells is permitted in carefully written library code.
9593
let inline setFreshConsTail cons t = cons.( :: ).1 <- t
@@ -499,15 +497,6 @@ module internal List =
499497
else
500498
filter predicate t
501499

502-
let iteri action x =
503-
let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt(action)
504-
let rec loop n x =
505-
match x with
506-
| [] -> ()
507-
| h :: t -> f.Invoke(n, h); loop (n+1) t
508-
509-
loop 0 x
510-
511500
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
512501
// tail cons cells is permitted in carefully written library code.
513502
let rec concatToFreshConsTail cons h1 l =

src/fsharp/FSharp.Core/local.fsi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ module internal List =
3232
val distinctWithComparer : System.Collections.Generic.IEqualityComparer<'T> -> 'T list -> 'T list
3333
val distinctByWithComparer : System.Collections.Generic.IEqualityComparer<'Key> -> ('T -> 'Key) -> list:'T list -> 'T list when 'Key : equality
3434
val init : int -> (int -> 'T) -> 'T list
35-
val iter : ('T -> unit) -> 'T list -> unit
3635
val filter : predicate:('T -> bool) -> 'T list -> 'T list
3736
val collect : ('T -> 'U list) -> 'T list -> 'U list
3837
val partition : predicate:('T -> bool) -> 'T list -> 'T list * 'T list
@@ -48,7 +47,6 @@ module internal List =
4847
val exists : predicate:('T -> bool) -> 'T list -> bool
4948
val rev: 'T list -> 'T list
5049
val concat : seq<'T list> -> 'T list
51-
val iteri : action:(int -> 'T -> unit) -> 'T list -> unit
5250
val unfold : ('State -> ('T * 'State) option) -> 'State -> 'T list
5351
val unzip : ('T1 * 'T2) list -> 'T1 list * 'T2 list
5452
val unzip3 : ('T1 * 'T2 * 'T3) list -> 'T1 list * 'T2 list * 'T3 list

0 commit comments

Comments
 (0)