Browse Source

[F#/Oxpecker] Improvements for json and fortunes benchmarks (#9246)

* [F#/Oxpecker] Improved fortunes rendering

* [F#/Oxpecker] Use SpanJson for json benchmark
Vladimir Shchur 11 months ago
parent
commit
9ae18ac387

+ 1 - 1
frameworks/FSharp/oxpecker/README.md

@@ -19,5 +19,5 @@ This includes tests for plaintext, json, fortunes, single query, mutliple querie
 
 * [Oxpecker](https://github.com/Lanayx/Oxpecker)
 * [Npgsql](https://github.com/npgsql/npgsql)
-* [System.Text.Json](https://github.com/dotnet/runtime/tree/main/src/libraries/System.Text.Json)
+* [SpanJson](https://github.com/Tornhoof/SpanJson)
 * ASP.NET Core

+ 2 - 0
frameworks/FSharp/oxpecker/src/App/App.fsproj

@@ -15,6 +15,8 @@
   <ItemGroup>
     <PackageReference Update="FSharp.Core" Version="8.0.400" />
     <PackageReference Include="Oxpecker" Version="0.14.1" />
+    <PackageReference Include="Oxpecker.ViewEngine" Version="0.14.0" />
     <PackageReference Include="Npgsql" Version="8.0.3" />
+    <PackageReference Include="SpanJson" Version="4.2.0" />
   </ItemGroup>
 </Project>

+ 11 - 11
frameworks/FSharp/oxpecker/src/App/Program.fs

@@ -2,6 +2,7 @@ namespace App
 
 open System
 open Oxpecker
+open System.Runtime.InteropServices
 
 [<RequireQualifiedAccess>]
 module HtmlViews =
@@ -26,21 +27,20 @@ module HtmlViews =
         ) |> RenderHelpers.prerender
 
     let fortunes (fortunesData: ResizeArray<Fortune>) =
-        RenderHelpers.combine head tail (
-            __() {
-                for fortune in fortunesData do
-                    tr() {
-                        td() { raw <| string fortune.id }
-                        td() { fortune.message }
-                    }
+        let fragment = __()
+        for fortune in CollectionsMarshal.AsSpan fortunesData do
+            tr() {
+                td() { raw <| string fortune.id }
+                td() { fortune.message }
             }
-        )
+            |> fragment.AddChild
+        RenderHelpers.combine head tail fragment
 
 [<RequireQualifiedAccess>]
 module HttpHandlers =
     open System.Text
     open Microsoft.AspNetCore.Http
-    open System.Text.Json
+    open SpanJson
 
     let private extra =
         {
@@ -98,9 +98,9 @@ module HttpHandlers =
             ctx.WriteBytes(result)
 
     let jsonSimple value : EndpointHandler =
-        let options = JsonSerializerOptions(JsonSerializerDefaults.Web)
         fun ctx ->
-            ctx.Response.WriteAsJsonAsync(value, options)
+            ctx.SetContentType("application/json")
+            JsonSerializer.Generic.Utf8.SerializeAsync<_>(value, stream = ctx.Response.Body).AsTask()
 
     let endpoints =
         [|