-
Notifications
You must be signed in to change notification settings - Fork 131
Avoid wrapping List<int>/Stream<List<int>> response bodies in a CastList/CastStream #178
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
Changes from 5 commits
90acce8
b430a2b
767b236
671b4fd
26c4c08
3f107a5
1cf71a1
e32c5f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -52,9 +52,13 @@ class Body { | |||||||||
| contentLength = encoded.length; | ||||||||||
| stream = Stream.fromIterable([encoded]); | ||||||||||
| } | ||||||||||
| } else if (body is List<int>) { | ||||||||||
| // Avoid performance overhead from an unnecessary cast. | ||||||||||
| contentLength = body.length; | ||||||||||
| stream = Stream.value(body); | ||||||||||
| } else if (body is List) { | ||||||||||
| contentLength = body.length; | ||||||||||
| stream = Stream.fromIterable([body.cast()]); | ||||||||||
| stream = Stream.value(body.cast()); | ||||||||||
| } else if (body is Stream) { | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder, while we are here should we assume that
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely, seems worth the optimization |
||||||||||
| stream = body.cast(); | ||||||||||
| } else { | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |||||
|
|
||||||
| import 'dart:async'; | ||||||
| import 'dart:convert'; | ||||||
| import 'dart:typed_data'; | ||||||
|
|
||||||
| import 'package:shelf/shelf.dart' hide Request; | ||||||
| import 'package:test/test.dart'; | ||||||
|
|
@@ -25,6 +26,33 @@ void main() { | |||||
| }); | ||||||
| }); | ||||||
|
|
||||||
| test('supports a Uint8List body without copying', () async { | ||||||
| var bytes = Uint8List(10); | ||||||
| var response = Response.ok(bytes); | ||||||
|
|
||||||
| expect(response.contentLength, 10); | ||||||
| expect(await response.read().single, same(bytes)); | ||||||
| }); | ||||||
|
|
||||||
| test('supports a List<int> body without copying', () async { | ||||||
| var bytes = <int>[1, 2, 3, 4]; | ||||||
| var response = Response.ok(bytes); | ||||||
|
|
||||||
| expect(response.contentLength, 4); | ||||||
| expect(await response.read().single, same(bytes)); | ||||||
| }); | ||||||
|
|
||||||
| test('Copies a dynamic list of int elements', () async { | ||||||
| var bytes = <dynamic>[1, 2, 3, 4]; | ||||||
| var response = Response.ok(bytes); | ||||||
|
|
||||||
| expect(response.contentLength, 4); | ||||||
| expect( | ||||||
| await response.read().single, | ||||||
| isA<List<int>>() | ||||||
| .having((List<int> values) => values, 'values', [1, 2, 3, 4])); | ||||||
|
||||||
| .having((List<int> values) => values, 'values', [1, 2, 3, 4])); | |
| .having((values) => values, 'values', [1, 2, 3, 4])); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is likely to be iterated over multiple times, you may be better to eagerly copy the list with
List.frominstead of usingcast.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My hunch is this isn't a problem. I don't expect multiple iterations in most cases. Usually when we would need this cast it would be a
Responsewhich will be passed todart:iowhich I expect iterates once. Otherwise, when this stream is getting passed to user code (where it may be iterated more than once) I'd expect the optimizations in this PR should kick in since it will most often be aStream<List<int>>coming fromdart:ioto start with.