Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(gql_http_link): improve json decoder performance #464

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading