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

Add typed queue classes #21

Merged
merged 10 commits into from
Jan 17, 2020
Merged

Add typed queue classes #21

merged 10 commits into from
Jan 17, 2020

Conversation

nex3
Copy link
Contributor

@nex3 nex3 commented Oct 18, 2019

These make it easy to, for example, efficiently enqueue incoming
chunked binary data for processing.

These make it easy to, for example, efficiently enqueue incoming
chunked binary data for processing.
@nex3 nex3 requested a review from nshahan October 18, 2019 09:10
@nshahan
Copy link

nshahan commented Oct 18, 2019

I'm not familiar with this package.

@eernstg I think you are in the process of migrating dart:typed_data. Would you also want to review this addition to package:typed_data with your API changes in mind?

@eernstg
Copy link

eernstg commented Oct 18, 2019

Hi @nshahan and @nex3,
I think @lrhn would be the obvious choice for such a review—he has been the maintainer of the core libraries for a long time.

@lrhn, wdyt? We could split the work, too.

@nex3
Copy link
Contributor Author

nex3 commented Nov 6, 2019

Ping!

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.

Looks very good!

int get length => (_tail - _head) & (_table.length - 1);

List<E> toList({bool growable = true}) {
var list = growable ? (<E>[]..length = length) : _createList(length);
Copy link
Contributor

Choose a reason for hiding this comment

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

Might as well start to prepare for NNBD. The ..length = length won't work there, you can't grow a non-nullable list.
(Usual workaround is List<E>.filled(length, ..someDefaultValue..), where the default value here is probably either 0 or 0.0/NaN depending on E).
Maybe have a E get _defaultValue; which you can override to return the value to use.

Since we are in package:typed_data, we have access to growable typed data, so consider returning a Uint8Queue or Uint8Buiffer instead of a plain growable list. Both are valid lists.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

lib/src/typed_queue.dart Show resolved Hide resolved
var delta = value - length;
if (delta >= 0) {
if (_table.length <= value) _growTo(value);
_tail = (_tail + delta) & (_table.length - 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

You should also fill the range with zeros (at least when you don't grow).

Since removeLast doesn't clear the entry it removed, the data is still in the backing store. Increasing the length will make it accessible again, which seems like something that users could end up depending on, even if it's not actually promised. Better to zero out the range before making it available.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

}

E operator [](int index) {
RangeError.checkValidIndex(index, this);
Copy link
Contributor

Choose a reason for hiding this comment

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

Use RangeError.checkValidIndex(index, this, null, this.length);.
If you don't pass the length, it is found through a dynamic lookup on this. That's probably not good for performance any more.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. That's probably a good note to add to the docs.


L sublist(int start, [int end]) {
var length = this.length;
RangeError.checkValidRange(start, end, length);
Copy link
Contributor

Choose a reason for hiding this comment

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

end = RangeError.checkValidRange(start, end, length);

will assign end to length or itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


// Create a new typed list.
L _createList(int size);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This entire class seems to be indifferent to it being intended for typed data.

Consider making it publicly available as a general ListQueue skeleton class, e.g., which takes the implementation of _createList as a function argument to the constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that could be a good addition to package:collection (or dart:collection, which implements QueueList basically the same way), but unfortunately I don't have the bandwidth for it.

import "package:collection/collection.dart";

/// The shared superclass of all the typed queue subclasses.
abstract class _TypedQueue<E, L extends List<E>> extends Object
Copy link
Contributor

@lrhn lrhn Nov 7, 2019

Choose a reason for hiding this comment

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

extends Object with ListMixin<E> can now be written as just with ListMixin<E>.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

/// The underlying data buffer.
///
/// This is always both a List<E> and a TypedData, which we don't have a type
/// for that. For example, for a `Uint8Buffer`, this is a `Uint8List`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Uint8Buffer -> Uint8Queue

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

L _createList(int size);
}

abstract class _IntQueue<L extends List<int>> extends _TypedQueue<int, L> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this class necessary?
It doesn't seem to be used for anything except writing
extends _IntQueue<Uint8List>
instead of
extends _TypedQueue<int, Uint8List>
or similar for each actual class.

(If you had a _defaultValue getter, it would make sense to have it return 0 here).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have _defaultValue now.

test/queue_test.dart Show resolved Hide resolved
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.

LGTM. Thanks!

lib/src/typed_queue.dart Show resolved Hide resolved
@kevmoo
Copy link
Contributor

kevmoo commented Jan 12, 2020

What's the story here?

@nex3
Copy link
Contributor Author

nex3 commented Jan 13, 2020

Sorry, this dropped off my radar. I'll get it mergeable some time this week.

@nex3
Copy link
Contributor Author

nex3 commented Jan 15, 2020

A bunch of new lints were added since I opened this. I've fixed a few one-offs, but the remainders have many failures and no way to do automated fixes. Does someone else care enough to fix them, or can I just comment them out for now?

@kevmoo
Copy link
Contributor

kevmoo commented Jan 15, 2020

A bunch of new lints were added since I opened this. I've fixed a few one-offs, but the remainders have many failures and no way to do automated fixes. Does someone else care enough to fix them, or can I just comment them out for now?

Just pushed the fixes for you.

dart bin/dartfix.dart --fix=prefer-single-quotes --fix=annotate-overrides --required --pedantic -w ~/github/typed_data/

From /sdk/pkg/dartfix

pubspec.yaml Outdated
@@ -9,6 +9,9 @@ homepage: https://github.com/dart-lang/typed_data
environment:
sdk: '>=2.4.0 <3.0.0'

dependencies:
collection: ^1.1.0

dev_dependencies:
pedantic: ^1.0.0
Copy link
Contributor

Choose a reason for hiding this comment

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

Could also just not depend on pedantic, or lock it to pedantic: 1.7.0 are thereabout.
The newest versions of pedantic have jumped the shark, and I do not want to support the lints they have chose to enable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -27,17 +27,20 @@ abstract class _TypedQueue<E, L extends List<E>> with ListMixin<E> {

// Iterable interface.

@override
Copy link
Contributor

Choose a reason for hiding this comment

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

Please don't add @OverRide.
Please don't change quotes.

(So, in general, no to all these changes).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@lrhn lrhn merged commit 0ba3ca8 into master Jan 17, 2020
@kevmoo kevmoo deleted the queue branch February 27, 2020 00:04
mosuem pushed a commit to dart-lang/core that referenced this pull request Oct 16, 2024
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.

6 participants