fable-compiler / fable-react

Fable bindings and helpers for React and React Native
MIT License
273 stars 67 forks source link

Fable.ReactServer.cssProp very slow #215

Closed medigor closed 3 years ago

medigor commented 3 years ago

https://github.com/fable-compiler/fable-react/blob/d813cee23227158b0be9af25fcc1f890253806f4/src/Fable.ReactServer.fs#L44-L51

instead: ConcurrentDictionary<TKey,TValue>.GetOrAdd(TKey, TValue) should use: ConcurrentDictionary<TKey,TValue>.GetOrAdd(TKey, Func<TKey,TValue>)

delneg commented 3 years ago

Changing the above to

let getStringFromObj(v)=
        let r = v.GetType().GetCustomAttributes(false)
                |> Seq.exists (function
                    | :? Core.StringEnumAttribute -> true
                    | _ -> false)
        if r then stringEnum v else v.ToString()

    let cssProp = cssPropsCache.GetOrAdd(value,System.Func<obj,string>(getStringFromObj))
    escapeHtml html cssProp

leads to the following results: https://github.com/delneg/fable-react/blob/fix/ssr-sample/docs/server-side-rendering.md#run-simple-benchmark-test-in-sample-app

Before: [Single thread] 72367ms 4421.905req/s [16 tasks] Total: 14685ms Memory footprint: 46.992MB Requests/sec: 21790.943

After: [Single thread] 20842ms 15353.613req/s [16 tasks] Total: 5610ms Memory footprint: 46.934MB Requests/sec: 57040.998

delneg commented 3 years ago

@alfonsogarciacaro Please take a look as this means a huge perfomance boost

alfonsogarciacaro commented 3 years ago

Awesome, thank you both for spotting and fixing this! I will push a new release with the change :+1:

alfonsogarciacaro commented 3 years ago

Fixed by #216