-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
1,266 additions
and
401 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Isolate parsing performance | ||
|
||
Parsing JSON on an isolate allows you to perform the operation in the background without blocking the main thread, which can be useful for large JSON files or when you need to perform other operations while parsing. | ||
|
||
**Parsing on main thread took 20-60 microseconds.** | ||
|
||
**Parsing used IsolateManager took 2400-2500 microseconds.** | ||
|
||
**Parsing used Isolate.run took 110000-130000 microseconds.** | ||
|
||
```dart | ||
Future<T> executeInIsolate<T>({ | ||
required T Function(Response) mapper, | ||
required Response response, | ||
}) { | ||
return Isolate.run(() => mapper.call(response)); | ||
} | ||
``` | ||
|
||
These results were obtained : | ||
|
||
```dart | ||
final performRequest = (tokenPair) async { | ||
final response = await _createRequest(params, tokenPair); | ||
final timer = Stopwatch()..start(); | ||
params.responseMapper.call(response); | ||
log('Sync mapper: ${timer.elapsedMicroseconds}'); | ||
timer.stop(); | ||
final timer2 = Stopwatch()..start(); | ||
final result = await isolateManager.send(response, params.responseMapper); | ||
log('isolateManager.send: ${timer2.elapsedMicroseconds}'); | ||
timer2.stop(); | ||
final timer3 = Stopwatch()..start(); | ||
await executeInIsolate(mapper: params.responseMapper, response: response); | ||
log('execute in Isolate.run: ${timer3.elapsedMicroseconds}'); | ||
timer3.stop(); | ||
return result; | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:dash_kit_network/dash_kit_network.dart'; | ||
|
||
// This is an abstract class for managing isolates It defines the basic methods | ||
// needed to start, stop and send messages to an isolate. Flutter Web doesn't | ||
// support isolates so the `IsolateManager` class has a different implementation | ||
// for web. | ||
// ignore: prefer-match-file-name | ||
abstract class IsolateManagerInterface { | ||
// Method for starting the isolate. Should be called before sending messages. | ||
Future<void> start(); | ||
|
||
// Method for stopping the isolate. Should be called when the isolate is no | ||
//longer needed. | ||
void stop(); | ||
|
||
// Method for sending messages to the isolate. The `mapper` function is | ||
// executed in the isolate and the result is returned. | ||
Future<FutureOr<T>> send<T>( | ||
Response<dynamic> response, | ||
FutureOr<T> Function(Response<dynamic>) mapper, | ||
); | ||
} | ||
|
||
// Class Task is used to store the data needed to send a message to the isolate. | ||
class Task<T> { | ||
const Task( | ||
this.id, | ||
this.response, | ||
this.mapper, | ||
); | ||
|
||
final String id; | ||
final Response<dynamic> response; | ||
final T Function(Response<dynamic>) mapper; | ||
|
||
@override | ||
String toString() { | ||
return 'Task{id: $id, response: $response, mapper: $mapper}'; | ||
} | ||
} | ||
|
||
// Class Result is used to store the result of the mapper function executed in | ||
// the isolate. | ||
class Result<T> { | ||
const Result({ | ||
required this.id, | ||
this.result, | ||
this.error, | ||
}); | ||
|
||
final String id; | ||
final T? result; | ||
final dynamic error; | ||
|
||
@override | ||
String toString() { | ||
return 'Result{id: $id, result: $result, error: $error}'; | ||
} | ||
} | ||
|
||
// The IsolateStatus enum is used to track the status of the isolate. | ||
enum IsolateStatus { | ||
created, | ||
initializing, | ||
initialized, | ||
stopped, | ||
} |
Oops, something went wrong.