Skip to content

Commit 9ae18ac

Browse files
authored
[F#/Oxpecker] Improvements for json and fortunes benchmarks (#9246)
* [F#/Oxpecker] Improved fortunes rendering * [F#/Oxpecker] Use SpanJson for json benchmark
1 parent d62e690 commit 9ae18ac

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

frameworks/FSharp/oxpecker/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ This includes tests for plaintext, json, fortunes, single query, mutliple querie
1919

2020
* [Oxpecker](https://github.com/Lanayx/Oxpecker)
2121
* [Npgsql](https://github.com/npgsql/npgsql)
22-
* [System.Text.Json](https://github.com/dotnet/runtime/tree/main/src/libraries/System.Text.Json)
22+
* [SpanJson](https://github.com/Tornhoof/SpanJson)
2323
* ASP.NET Core

frameworks/FSharp/oxpecker/src/App/App.fsproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<ItemGroup>
1616
<PackageReference Update="FSharp.Core" Version="8.0.400" />
1717
<PackageReference Include="Oxpecker" Version="0.14.1" />
18+
<PackageReference Include="Oxpecker.ViewEngine" Version="0.14.0" />
1819
<PackageReference Include="Npgsql" Version="8.0.3" />
20+
<PackageReference Include="SpanJson" Version="4.2.0" />
1921
</ItemGroup>
2022
</Project>

frameworks/FSharp/oxpecker/src/App/Program.fs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace App
22

33
open System
44
open Oxpecker
5+
open System.Runtime.InteropServices
56

67
[<RequireQualifiedAccess>]
78
module HtmlViews =
@@ -26,21 +27,20 @@ module HtmlViews =
2627
) |> RenderHelpers.prerender
2728

2829
let fortunes (fortunesData: ResizeArray<Fortune>) =
29-
RenderHelpers.combine head tail (
30-
__() {
31-
for fortune in fortunesData do
32-
tr() {
33-
td() { raw <| string fortune.id }
34-
td() { fortune.message }
35-
}
30+
let fragment = __()
31+
for fortune in CollectionsMarshal.AsSpan fortunesData do
32+
tr() {
33+
td() { raw <| string fortune.id }
34+
td() { fortune.message }
3635
}
37-
)
36+
|> fragment.AddChild
37+
RenderHelpers.combine head tail fragment
3838

3939
[<RequireQualifiedAccess>]
4040
module HttpHandlers =
4141
open System.Text
4242
open Microsoft.AspNetCore.Http
43-
open System.Text.Json
43+
open SpanJson
4444

4545
let private extra =
4646
{
@@ -98,9 +98,9 @@ module HttpHandlers =
9898
ctx.WriteBytes(result)
9999

100100
let jsonSimple value : EndpointHandler =
101-
let options = JsonSerializerOptions(JsonSerializerDefaults.Web)
102101
fun ctx ->
103-
ctx.Response.WriteAsJsonAsync(value, options)
102+
ctx.SetContentType("application/json")
103+
JsonSerializer.Generic.Utf8.SerializeAsync<_>(value, stream = ctx.Response.Body).AsTask()
104104

105105
let endpoints =
106106
[|

0 commit comments

Comments
 (0)