Not only did I see an improvement in performance after switching to IDictionary
from Map
(which is explored in a question from over 10 years ago), I got even more speed when I went from this type
:
type MyCollection =
static member Collection = [
(1, "one")
(2, "two")
...
] |> dict
static member Get id = MyCollection.Collection[id]
…to a type like this:
type MyCollection() =
static let collection = [
(1, "one")
(2, "two")
...
] |> dict
static member Get id = collection[id]
Why does performance improve when using static let
over static member
?
CodePudding user response:
You can see the difference if you look at the decompilation in Sharplab
static member Collection
compiles to a property with a getter. i.e. it is syntactic sugar for static member Collection()
hence when you call MyCollection.Collection it creates a new IDictionary
every call.
By contrast static let collection
is essentially a standard binding to a value with special auto-initialisation code. It only creates the IDictionary
once.