Skip to content

Commit 93db5f8

Browse files
Copilotxperiandri
andcommitted
Optimize: cache JsonSerializerOptions and reduce StringBuilder overhead
Agent-Logs-Url: https://github.com/fsprojects/FSharp.Data.GraphQL/sessions/ce0fba11-9043-452f-b948-e03c8b644f26 Co-authored-by: xperiandri <2365592+xperiandri@users.noreply.github.com>
1 parent 86faa93 commit 93db5f8

2 files changed

Lines changed: 18 additions & 15 deletions

File tree

src/FSharp.Data.GraphQL.Shared/AstExtensions.fs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,18 @@ type Document with
108108
let escaped = StringBuilder(s.Length + 2)
109109
escaped.Append('"') |> ignore
110110
for c in s do
111-
match c with
112-
| '"' -> escaped.Append("\\\"") |> ignore
113-
| '\\' -> escaped.Append("\\\\") |> ignore
114-
| '\b' -> escaped.Append("\\b") |> ignore
115-
| '\f' -> escaped.Append("\\f") |> ignore
116-
| '\n' -> escaped.Append("\\n") |> ignore
117-
| '\r' -> escaped.Append("\\r") |> ignore
118-
| '\t' -> escaped.Append("\\t") |> ignore
119-
| c when c < '\u0020' -> escaped.AppendFormat("\\u{0:X4}", int c) |> ignore
120-
| c -> escaped.Append(c) |> ignore
111+
let appendStr =
112+
match c with
113+
| '"' -> "\\\""
114+
| '\\' -> "\\\\"
115+
| '\b' -> "\\b"
116+
| '\f' -> "\\f"
117+
| '\n' -> "\\n"
118+
| '\r' -> "\\r"
119+
| '\t' -> "\\t"
120+
| c when c < '\u0020' -> sprintf "\\u%04X" (int c)
121+
| c -> string c
122+
escaped.Append(appendStr) |> ignore
121123
escaped.Append('"').ToString()
122124
let withQuotes = escapeGraphQLString
123125
let rec printValue x =

src/FSharp.Data.GraphQL.Shared/ValidationResultCache.fs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@ module SchemaId =
2121
let private formatByteAsLowerHex (value : byte) =
2222
value.ToString("x2", System.Globalization.CultureInfo.InvariantCulture)
2323

24+
let private jsonOptions = JsonSerializerOptions(
25+
WriteIndented = false,
26+
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.Never
27+
)
28+
2429
/// <summary>
2530
/// Computes a deterministic schema identifier from an introspection schema.
2631
/// </summary>
2732
/// <param name="introspectionSchema">The introspection schema to hash.</param>
2833
/// <returns>A lowercase hexadecimal SHA-256 hash string that uniquely identifies the schema structure.</returns>
2934
let fromIntrospectionSchema (introspectionSchema : IntrospectionSchema) =
30-
let options = JsonSerializerOptions(
31-
WriteIndented = false,
32-
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.Never
33-
)
34-
let json = JsonSerializer.Serialize(introspectionSchema, options)
35+
let json = JsonSerializer.Serialize(introspectionSchema, jsonOptions)
3536
let jsonBytes = Encoding.UTF8.GetBytes json
3637
use sha256 = SHA256.Create()
3738
let hash = sha256.ComputeHash jsonBytes

0 commit comments

Comments
 (0)