Skip to content

Commit ae87b8e

Browse files
authored
Merge pull request #1103 from kerams/opt
Optimize
2 parents c9b20dc + 34f9754 commit ae87b8e

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

src/Fabulous/Array.fs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module ArraySlice =
4949
module Array =
5050
let inline appendOne (v: 'v) (arr: 'v array) =
5151
let res = Array.zeroCreate(arr.Length + 1)
52-
res[.. arr.Length - 1] <- arr
52+
arr.CopyTo(res.AsSpan())
5353
res[arr.Length] <- v
5454
res
5555

@@ -89,7 +89,7 @@ module StackAllocatedCollections =
8989
[<NoComparison; NoEquality>]
9090
type private Part<'v> =
9191
| Empty
92-
| Filled of struct (Items<'v> * Part<'v>)
92+
| Filled of Items<'v> * Part<'v>
9393

9494
[<Struct; NoComparison; NoEquality>]
9595
type StackList<'v> =
@@ -258,8 +258,8 @@ module StackAllocatedCollections =
258258

259259
[<Struct; NoComparison; NoEquality>]
260260
type StackArray3<'v> =
261-
| Few of data: struct (Size * 'v * 'v * 'v)
262261
| Many of arr: 'v array
262+
| Few of data: Size * 'v * 'v * 'v
263263

264264
module StackArray3 =
265265

@@ -278,7 +278,7 @@ module StackAllocatedCollections =
278278

279279
let add (arr: StackArray3<'v> inref, v: 'v) : StackArray3<'v> =
280280
match arr with
281-
| Few(struct (size, v0, v1, v2)) ->
281+
| Few(size, v0, v1, v2) ->
282282
match size with
283283
| Size.Zero -> one v
284284
| Size.One -> two(v0, v)
@@ -290,13 +290,13 @@ module StackAllocatedCollections =
290290

291291
let inline length (arr: StackArray3<'v> inref) : int =
292292
match arr with
293-
| Few(struct (size, _, _, _)) -> int size
293+
| Few(size, _, _, _) -> int size
294294
| Many arr -> arr.Length
295295

296296

297297
let get (arr: StackArray3<'v> inref) (index: int) : 'v =
298298
match arr with
299-
| Few(struct (size, v0, v1, v2)) ->
299+
| Few(size, v0, v1, v2) ->
300300
if (index >= int size) then
301301
IndexOutOfRangeException() |> raise
302302
else
@@ -310,7 +310,7 @@ module StackAllocatedCollections =
310310

311311
let find (test: 'v -> bool) (arr: StackArray3<'v> inref) : 'v =
312312
match arr with
313-
| Few(struct (size, v0, v1, v2)) ->
313+
| Few(size, v0, v1, v2) ->
314314
match (size, test v0, test v1, test v2) with
315315
| Size.One, true, _, _
316316
| Size.Two, true, _, _
@@ -327,7 +327,7 @@ module StackAllocatedCollections =
327327
/// In Many case it sorts the Many variant inline for optimization reasons
328328
let rec inline sortInPlace<'T, 'V when 'V: comparison> ([<InlineIfLambda>] getKey: 'T -> 'V) (arr: StackArray3<'T> inref) : StackArray3<'T> =
329329
match arr with
330-
| Few(struct (size, v0, v1, v2)) ->
330+
| Few(size, v0, v1, v2) ->
331331
match size with
332332
| Size.Zero
333333
| Size.One -> arr
@@ -358,15 +358,13 @@ module StackAllocatedCollections =
358358
| _ -> empty() // should never happen but don't want to throw there
359359
| Many arr -> many(Array.sortInPlace getKey arr)
360360

361-
362-
let inline private arr0 () = [||]
363361
let inline private arr1 (v: 'v) = [| v |]
364362
let inline private arr2 (v0: 'v, v1: 'v) = [| v0; v1 |]
365363
let inline private arr3 (v0: 'v, v1: 'v, v2: 'v) = [| v0; v1; v2 |]
366364

367365
let toArray (arr: StackArray3<'v> inref) : 'v array =
368366
match arr with
369-
| Few(struct (size, v0, v1, v2)) ->
367+
| Few(size, v0, v1, v2) ->
370368
match size with
371369
| Size.Zero -> Array.empty
372370
| Size.One -> arr1 v0
@@ -377,7 +375,7 @@ module StackAllocatedCollections =
377375

378376
let combine (a: StackArray3<'v>) (b: StackArray3<'v>) : StackArray3<'v> =
379377
match (a, b) with
380-
| Few(struct (asize, a0, a1, a2)), Few(struct (bsize, b0, b1, b2)) ->
378+
| Few(asize, a0, a1, a2), Few(bsize, b0, b1, b2) ->
381379
match (asize, bsize) with
382380
| Size.Zero, _ -> b
383381
| _, Size.Zero -> a
@@ -609,10 +607,9 @@ module StackAllocatedCollections =
609607

610608
[<Struct>]
611609
type Op =
612-
| Added of added: uint16
613-
| Removed of removed: uint16
614-
| Changed of changed: uint16
615-
610+
| Added of value: uint16
611+
| Removed of value: uint16
612+
| Changed of value: uint16
616613

617614
let inline create () = DiffBuilder(stackalloc<uint16> 8, 0)
618615

@@ -650,7 +647,7 @@ module StackAllocatedCollections =
650647
| OpCode.ChangeCode -> Changed(value)
651648
| _ -> IndexOutOfRangeException() |> raise
652649

653-
let inline toArray (builder: DiffBuilder byref) (map: Op -> 't) : 't array =
650+
let inline toArray (builder: DiffBuilder byref) ([<InlineIfLambda>] map: Op -> 't) : 't array =
654651
let len = lenght &builder
655652
let res = Array.zeroCreate<'t> len
656653

src/Fabulous/View.fs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module View =
3030
WidgetBuilder<'msg, Memo.Memoized<'marker>>(Memo.MemoWidgetKey, Memo.MemoAttribute.WithValue(memo))
3131

3232
/// Map the widget's message type to the parent's message type to allow for view composition
33-
let inline map (fn: 'oldMsg -> 'newMsg) (x: WidgetBuilder<'oldMsg, 'marker>) : WidgetBuilder<'newMsg, 'marker> =
33+
let inline map ([<InlineIfLambda>] fn: 'oldMsg -> 'newMsg) (x: WidgetBuilder<'oldMsg, 'marker>) : WidgetBuilder<'newMsg, 'marker> =
3434
let replaceWith (oldAttr: ScalarAttribute) =
3535
let fnWithBoxing (msg: obj) =
3636
let oldFn = unbox<obj -> obj> oldAttr.Value
@@ -56,4 +56,5 @@ module View =
5656
WidgetBuilder<'newMsg, 'marker>(builder.Key, builder.Attributes)
5757

5858
/// Combine map and lazy. Map the widget's message type to the parent's message type, and then memoize it
59-
let inline lazyMap (mapFn: 'oldMsg -> 'newMsg) (viewFn: 'key -> WidgetBuilder<'oldMsg, 'marker>) (model: 'key) = lazy' (viewFn >> map mapFn) model
59+
let inline lazyMap ([<InlineIfLambda>] mapFn: 'oldMsg -> 'newMsg) ([<InlineIfLambda>] viewFn: 'key -> WidgetBuilder<'oldMsg, 'marker>) (model: 'key) =
60+
lazy' (viewFn >> map mapFn) model

0 commit comments

Comments
 (0)