Skip to content

Commit

Permalink
Optimized List module
Browse files Browse the repository at this point in the history
  • Loading branch information
ncave committed Jul 20, 2020
1 parent 9b5d2f2 commit 5bde218
Show file tree
Hide file tree
Showing 6 changed files with 414 additions and 415 deletions.
16 changes: 8 additions & 8 deletions src/Fable.Transforms/Fable2Babel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ module Util =
makeNativeTypeAnnotation com ctx [genArg] "Array"

and makeListTypeAnnotation com ctx genArg =
makeImportTypeAnnotation com ctx [genArg] "Types" "List"
makeImportTypeAnnotation com ctx [genArg] "List" "List"

and makeUnionTypeAnnotation com ctx genArgs =
List.map (typeAnnotation com ctx) genArgs
Expand Down Expand Up @@ -808,12 +808,12 @@ module Util =
| Fable.NewTuple vals -> makeTypedArray com ctx Fable.Any (Fable.ArrayValues vals)
// Optimization for bundle size: compile list literals as List.ofArray
| Replacements.ListLiteral(exprs, t) ->
[|List.rev exprs |> makeArray com ctx|] |> coreLibConstructorCall com ctx "Types" "List"
[|List.rev exprs |> makeArray com ctx|] |> coreLibCall com ctx r "List" "newList"
| Fable.NewList (headAndTail, _) ->
match headAndTail with
| None -> coreLibConstructorCall com ctx "Types" "List" [||]
| None -> coreLibCall com ctx r "List" "newList" [||]
| Some(TransformExpr com ctx head, TransformExpr com ctx tail) ->
callFunction r (get None tail "add") [head]
coreLibCall com ctx r "List" "cons" [|head; tail|]
| Fable.NewOption (value, t) ->
match value with
| Some (TransformExpr com ctx e) ->
Expand Down Expand Up @@ -1053,8 +1053,8 @@ module Util =
let expr = com.TransformAsExpr(ctx, fableExpr)
match getKind with
| Fable.ExprGet(TransformExpr com ctx prop) -> getExpr range expr prop
| Fable.ListHead -> get range expr "head"
| Fable.ListTail -> get range expr "tail"
| Fable.ListHead -> coreLibCall com ctx range "List" "head" [|expr|]
| Fable.ListTail -> coreLibCall com ctx range "List" "tail" [|expr|]
| Fable.FieldGet(fieldName,_,_) ->
let expr =
match fableExpr with
Expand Down Expand Up @@ -1150,7 +1150,7 @@ module Util =
| Fable.FunctionType _ -> jsTypeof "function" expr
| Fable.Array _ | Fable.Tuple _ ->
coreLibCall com ctx None "Util" "isArrayLike" [|com.TransformAsExpr(ctx, expr)|]
| Fable.List _ -> jsInstanceof (coreValue com ctx "Types" "List") expr
| Fable.List _ -> jsInstanceof (coreValue com ctx "List" "List") expr
| Replacements.Builtin kind ->
match kind with
| Replacements.BclGuid -> jsTypeof "string" expr
Expand Down Expand Up @@ -1211,7 +1211,7 @@ module Util =
let op = if nonEmpty then BinaryUnequal else BinaryEqual
upcast BinaryExpression(op, com.TransformAsExpr(ctx, expr), NullLiteral(), ?loc=range)
| Fable.ListTest nonEmpty ->
let expr = get range (com.TransformAsExpr(ctx, expr)) "isEmpty"
let expr = coreLibCall com ctx range "List" "isEmpty" [|com.TransformAsExpr(ctx, expr)|]
if nonEmpty then upcast UnaryExpression(UnaryNot, expr, ?loc=range)
else expr
| Fable.UnionCaseTest(uci, ent) ->
Expand Down
11 changes: 5 additions & 6 deletions src/fable-library/Array.fs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ module Helpers =
open Helpers

let private indexNotFoundMsg = "An index satisfying the predicate was not found in the collection."
let inline indexNotFound() = failwith indexNotFoundMsg

// Pay attention when benchmarking to append and filter functions below
// if implementing via native JS array .concat() and .filter() do not fall behind due to js-native transitions.
Expand Down Expand Up @@ -478,15 +477,15 @@ let partition (f: 'T -> bool) (source: 'T[]) ([<Inject>] cons: IArrayCons<'T>) =
let find (predicate: 'T -> bool) (array: 'T[]): 'T =
match findImpl predicate array with
| Some res -> res
| None -> indexNotFound()
| None -> failwith indexNotFoundMsg

let tryFind (predicate: 'T -> bool) (array: 'T[]): 'T option =
findImpl predicate array

let findIndex (predicate: 'T -> bool) (array: 'T[]): int =
match findIndexImpl predicate array with
| index when index > -1 -> index
| _ -> indexNotFound()
| _ -> failwith indexNotFoundMsg

let tryFindIndex (predicate: 'T -> bool) (array: 'T[]): int option =
match findIndexImpl predicate array with
Expand All @@ -496,7 +495,7 @@ let tryFindIndex (predicate: 'T -> bool) (array: 'T[]): int option =
let pick chooser (array: _[]) =
let rec loop i =
if i >= array.Length then
indexNotFound()
failwith indexNotFoundMsg
else
match chooser array.[i] with
| None -> loop(i+1)
Expand All @@ -513,7 +512,7 @@ let tryPick chooser (array: _[]) =

let findBack predicate (array: _[]) =
let rec loop i =
if i < 0 then indexNotFound()
if i < 0 then failwith indexNotFoundMsg
elif predicate array.[i] then array.[i]
else loop (i - 1)
loop (array.Length - 1)
Expand All @@ -534,7 +533,7 @@ let findLastIndex predicate (array: _[]) =

let findIndexBack predicate (array: _[]) =
let rec loop i =
if i < 0 then indexNotFound()
if i < 0 then failwith indexNotFoundMsg
elif predicate array.[i] then i
else loop (i - 1)
loop (array.Length - 1)
Expand Down
Loading

0 comments on commit 5bde218

Please sign in to comment.