Skip to content

Commit

Permalink
perf(gql_http_link): improve json decoder performance (#464)
Browse files Browse the repository at this point in the history
* perf(gql_http_link): improve json dedoder performance

* fix multipack
  • Loading branch information
knaeckeKami authored Jun 7, 2024
1 parent ff10ead commit dcb03fa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
5 changes: 5 additions & 0 deletions links/gql_http_link/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 14 additions & 8 deletions links/gql_http_link/lib/src/link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic>,
/// httpResponseDecoder : (http.Response httpResponse) => Isolate.run(() =>
/// const Utf8Decoder()
/// .fuse(const JsonDecoder())
/// .convert(response.bodyBytes) as Map<String, dynamic>?))
/// ```
HttpResponseDecoder httpResponseDecoder;

static Map<String, dynamic>? _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<Object?>(const JsonDecoder());

static Map<String, dynamic>? _defaultHttpResponseDecode(
http.Response httpResponse) =>
json.decode(
utf8.decode(
httpResponse.bodyBytes,
),
) as Map<String, dynamic>?;
_defaultHttpResponseDecoder.convert(httpResponse.bodyBytes)
as Map<String, dynamic>?;

http.Client? _httpClient;

Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions links/gql_http_link/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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'
sdk: '>=3.0.0 <4.0.0'
dependencies:
gql: ^1.0.0
gql_exec: ^1.0.0
Expand Down

0 comments on commit dcb03fa

Please sign in to comment.