From 6df951a632b18d510535029e592eefec0795f0e0 Mon Sep 17 00:00:00 2001 From: Martin Kamleithner Date: Fri, 7 Jun 2024 20:29:39 +0100 Subject: [PATCH 1/2] perf(gql_http_link): improve json dedoder performance --- links/gql_http_link/CHANGELOG.md | 5 +++++ links/gql_http_link/lib/src/link.dart | 22 ++++++++++++++-------- links/gql_http_link/pubspec.yaml | 12 ++++++------ 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/links/gql_http_link/CHANGELOG.md b/links/gql_http_link/CHANGELOG.md index 91bb7a17..e8c262cd 100644 --- a/links/gql_http_link/CHANGELOG.md +++ b/links/gql_http_link/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.1.0 + +- improve JSON decoding performance by taking advantage + Darts hidden _JsonUtf8Decoder obtained by fusing `Utf8Decoder` and `JsonDecoder` + ## 1.0.1+1 - add topics diff --git a/links/gql_http_link/lib/src/link.dart b/links/gql_http_link/lib/src/link.dart index 757bd582..566689b7 100644 --- a/links/gql_http_link/lib/src/link.dart +++ b/links/gql_http_link/lib/src/link.dart @@ -39,17 +39,23 @@ class HttpLink extends Link { /// the decoded map will be then passes to the `RequestSerializer`. /// It is recommended for performance to decode the response using `compute` function. /// ``` - /// httpResponseDecoder : (http.Response httpResponse) async => await compute(jsonDecode, httpResponse.body) as Map, + /// httpResponseDecoder : (http.Response httpResponse) => Isolate.run(() => + /// const Utf8Decoder() + /// .fuse(const JsonDecoder()) + /// .convert(response.bodyBytes) as Map?)) /// ``` HttpResponseDecoder httpResponseDecoder; - static Map? _defaultHttpResponseDecoder( + // use the hidden _JsonUtf8Decoder obtained by fusing + // Utf8Decoder and JsonDecoder + // see https://github.com/dart-lang/sdk/blob/5b2ea0c7a227d91c691d2ff8cbbeb5f7f86afdb9/sdk/lib/_internal/vm/lib/convert_patch.dart#L40 + static final Converter _defaultHttpResponseDecoder = + const Utf8Decoder().fuse(const JsonDecoder()); + + static Map? _defaultHttpResponseDecode( http.Response httpResponse) => - json.decode( - utf8.decode( - httpResponse.bodyBytes, - ), - ) as Map?; + _defaultHttpResponseDecoder.convert(httpResponse.bodyBytes) + as Map?; http.Client? _httpClient; @@ -65,7 +71,7 @@ class HttpLink extends Link { http.Client? httpClient, this.serializer = const RequestSerializer(), this.parser = const ResponseParser(), - this.httpResponseDecoder = _defaultHttpResponseDecoder, + this.httpResponseDecoder = _defaultHttpResponseDecode, this.followRedirects = false, }) : uri = Uri.parse(uri) { _httpClient = httpClient ?? http.Client(); diff --git a/links/gql_http_link/pubspec.yaml b/links/gql_http_link/pubspec.yaml index 1ed60341..8495b9e3 100644 --- a/links/gql_http_link/pubspec.yaml +++ b/links/gql_http_link/pubspec.yaml @@ -1,23 +1,23 @@ name: gql_http_link -version: 1.0.1+1 +version: 1.1.0 description: GQL Terminating Link to execute requests via HTTP using JSON. repository: https://github.com/gql-dart/gql -environment: - sdk: '>=2.13.0 <4.0.0' -dependencies: +environment: + sdk: '>=3.0.0 <4.0.0' +dependencies: gql: ^1.0.0 gql_exec: ^1.0.0 gql_link: ^1.0.0 http: '>0.13.0 <2.0.0' http_parser: ^4.0.0 meta: ^1.3.0 -dev_dependencies: +dev_dependencies: build_runner: ^2.0.0 code_builder: ^4.2.0 gql_pedantic: ^1.0.2 mockito: ^5.3.0 test: ^1.16.2 -topics: +topics: - graphql - gql - gql-link From 5a1a5e34f74b26eb3487cecffb8b96ac718ad20b Mon Sep 17 00:00:00 2001 From: Martin Kamleithner Date: Fri, 7 Jun 2024 20:36:09 +0100 Subject: [PATCH 2/2] fix multipack --- links/gql_http_link/pubspec.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/links/gql_http_link/pubspec.yaml b/links/gql_http_link/pubspec.yaml index 8495b9e3..b8862b35 100644 --- a/links/gql_http_link/pubspec.yaml +++ b/links/gql_http_link/pubspec.yaml @@ -2,22 +2,22 @@ name: gql_http_link version: 1.1.0 description: GQL Terminating Link to execute requests via HTTP using JSON. repository: https://github.com/gql-dart/gql -environment: +environment: sdk: '>=3.0.0 <4.0.0' -dependencies: +dependencies: gql: ^1.0.0 gql_exec: ^1.0.0 gql_link: ^1.0.0 http: '>0.13.0 <2.0.0' http_parser: ^4.0.0 meta: ^1.3.0 -dev_dependencies: +dev_dependencies: build_runner: ^2.0.0 code_builder: ^4.2.0 gql_pedantic: ^1.0.2 mockito: ^5.3.0 test: ^1.16.2 -topics: +topics: - graphql - gql - gql-link