Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Options.collect* performance #216

Merged
merged 1 commit into from
Aug 31, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 43 additions & 22 deletions src/Material/Options.elm
Original file line number Diff line number Diff line change
Expand Up @@ -88,51 +88,71 @@ type alias Summary c m =
}


{- `collect` and variants are called multiple times by nearly every use of
any elm-mdl component. Carefully consider performance implications before
modifying. In particular:

- Avoid closures. They are slow to create and cause subsequent GC.
- Pre-compute where possible.

Earlier versions of `collect`, violating these rules, consumed ~20% of
execution time for `Cards.view` and `Textfield.view`.
-}


collect1
: ((c -> c) -> c' -> c')
-> Property c m
-> Summary c' m
-> Summary c' m
collect1 f option acc =
: Property c m
-> Summary c m
-> Summary c m
collect1 option acc =
case option of
Class x -> { acc | classes = x :: acc.classes }
CSS x -> { acc | css = x :: acc.css }
Attribute x -> { acc | attrs = x :: acc.attrs }
Many options -> List.foldl (collect1 f) acc options
Set g -> { acc | config = f g acc.config }
Many options -> List.foldl collect1 acc options
Set g -> { acc | config = g acc.config }
None -> acc


recollect : Summary c m -> List (Property c m) -> Summary c m
recollect =
List.foldl (collect1 (<|))
List.foldl collect1


{-| Flatten a `Property a` into a `Summary a`. Operates as `fold`
over options; first two arguments are folding function and initial value.
-}
collect : c -> List (Property c m) -> Summary c m
collect config0 =
recollect { classes=[], css=[], attrs=[], config=config0 }
collect =
Summary [] [] [] >> recollect


{-| Special-casing of collect for `Property c ()`.
-}
collect1' : Property c m -> Summary () m -> Summary () m
collect1' options acc =
case options of
Class x -> { acc | classes = x :: acc.classes }
CSS x -> { acc | css = x :: acc.css }
Attribute x -> { acc | attrs = x :: acc.attrs }
Many options -> List.foldl collect1' acc options
Set _ -> acc
None -> acc


collect' : List (Property c m) -> Summary () m
collect' options =
List.foldl
(collect1 (\_ _ -> ()))
{ classes=[], css=[], attrs=[], config=() }
options
collect' =
List.foldl collect1' (Summary [] [] [] ())


addAttributes : Summary c m -> List (Attribute m) -> List (Attribute m)
addAttributes summary attrs =
List.concat
[ attrs
, [ Html.Attributes.style summary.css ]
, [ Html.Attributes.class (String.join " " summary.classes) ]
, summary.attrs
]
List.append
attrs
( Html.Attributes.style summary.css
:: Html.Attributes.class (String.join " " summary.classes)
:: summary.attrs
)


{-| Apply a `Summary m`, extra properties, and optional attributes
Expand All @@ -143,7 +163,8 @@ apply : Summary c m -> (List (Attribute m) -> a)
apply summary ctor options attrs =
ctor
(addAttributes
(recollect summary options) attrs)
(recollect summary options)
attrs)



Expand Down