Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Faster ChunkedStreamReader. #182

Merged
merged 3 commits into from
Jun 17, 2021

Conversation

jonasfj
Copy link
Contributor

@jonasfj jonasfj commented May 21, 2021

  • Add an internal _offset to track offset in _buffer, reducing the
    number of times we need to create a sublist internally.
  • Specialize to handle cases where _buffer is a Uint8List by
    creating a Uint8List.sublistView when we need to split a chunk.

Fixes dart-lang/core#343.

 * Add an internal `_offset` to track offset in `_buffer`, reducing the
   number of times we need to create a sublist internally.
 * Specialize to handle cases where `_buffer` is a `Uint8List` by
   creating a `Uint8List.sublistView` when we need to split a chunk.

Fixes #181.
Copy link
Contributor

@lrhn lrhn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The buffering is fine, the specialization seems half-baked. I'd do it with a specialized class for Uint8List streams.

if (_buffer.length - _offset > 0) {
if (size < _buffer.length - _offset) {
late List<T> output;
if (_buffer is Uint8List) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer a specialized ChunkedByteStreamReader with special code for Uint8List everywhere.
The readChunk(size) above is not particularly efficient when it dumps all these Uint8Lists into a plain List<int>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why we have ChunkedStreamReaderByteStreamExt, but yes, in hindsight... maybe we should only have made ChunkedByteStreamReader -- because in practice most other streams aren't chunked.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ones that are ... are probably Stream<String>, not list based. So yes.

@kevmoo
Copy link
Contributor

kevmoo commented May 21, 2021

I wish we had benchmarks for these types of things. 🤷

if (_buffer.length - _offset > 0) {
if (size < _buffer.length - _offset) {
late List<T> output;
if (_buffer is Uint8List) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ones that are ... are probably Stream<String>, not list based. So yes.

@jonasfj jonasfj merged commit 27e3a92 into dart-archive:master Jun 17, 2021
@jonasfj jonasfj deleted the faster-ChunkedStreamReader branch June 17, 2021 12:46
mosuem pushed a commit to dart-lang/core that referenced this pull request Oct 14, 2024
* Faster ChunkedStreamReader.

 * Add an internal `_offset` to track offset in `_buffer`, reducing the
   number of times we need to create a sublist internally.
 * Specialize to handle cases where `_buffer` is a `Uint8List` by
   creating a `Uint8List.sublistView` when we need to split a chunk.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging this pull request may close these issues.

ChunkedStreamReader is slow with large chunks
4 participants