Skip to content

Const typed data (Int8List, et al.) #6378

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

Closed
a-siva opened this issue Oct 29, 2012 · 11 comments
Closed

Const typed data (Int8List, et al.) #6378

a-siva opened this issue Oct 29, 2012 · 11 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-not-planned Closed as we don't intend to take action on the reported issue P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@a-siva
Copy link
Contributor

a-siva commented Oct 29, 2012

e.g:

const Int8Array int8 = const [10, 100];

does not work, instead we need to write code as follows:
Int8Array initInt8Array() {
  var list = new Int8Array(2);
  list[0] = 10;
  list[1] = 100;
  return list;
}
Int8Array int8 = initInt8Array();

We should try and make it possible to initialize constant scalar lists
similar to how we initialize all lists.

@sethladd
Copy link
Contributor

Siva,

Running in production mode, that code is:

const var int8 = const [10, 100];

Looks like you're asking for a new literal for scalar lists?


Added Triaged label.

@lrhn
Copy link
Member

lrhn commented Oct 15, 2013

It could be handled by a const constructor on Uint8List that takes a const List<int> as argument. It will require compiler magic to actually become a real Uint8List, and I don't see it happening any time soon.
I'd recommend just using
  new Int8List.from(const [10, 100]);
or just use:
  const [10, 100]
directly (it won't be a Int8List, but it will be a list, and the system could decide to have an efficient implementation of a constant list of small integers).

@lrhn
Copy link
Member

lrhn commented Jan 14, 2014

Removed Type-Defect label.
Added Type-Enhancement, Library-TypedData labels.

@floitschG
Copy link
Contributor

Set owner to @lrhn.

@lrhn
Copy link
Member

lrhn commented May 12, 2015

Creating a const typed-data list is not a library issue - it needs language support.

For one thing, all type-data lists are currently mutable. Maybe if we had an immutable ByteBuffer, it would make more sense.


Removed the owner.
Removed Area-Library, Library-TypedData labels.
Added Area-Language label.

@a-siva a-siva added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels May 12, 2015
@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed triaged labels Feb 29, 2016
@munificent munificent changed the title Initialization of scalar lists using compile time constants is not possible Const typed data (Int8List, et al.) Dec 16, 2016
@munificent
Copy link
Member

Not unreasonable, but not planned either. The set of classes and operations that could be usefully made const is potentially unbounded and it's hard to decide what makes the cut and what doesn't. At least for now, we don't have any intention of including typed_data's types in it.

@munificent munificent added the closed-not-planned Closed as we don't intend to take action on the reported issue label Dec 19, 2016
@truongsinh
Copy link

any update for const typed data, is it still unplanned for the time being?

@lrhn
Copy link
Member

lrhn commented Mar 18, 2019

There are still no current plans.

@truongsinh
Copy link

So const a = [4,3,5] and final b = Uint8List.fromList([4,3,5]) which one is more preferred, or does it depend on scenarios? I understand that the reason we have typed data in the first place is for performance (time and space), but I assume having a compile-time const also has it's advantages.

@truongsinh
Copy link

@munificent @lrhn any recommendation for performance junkies?

@lrhn
Copy link
Member

lrhn commented Jun 6, 2019

For performance, I'd definitely go with:

final b = Uint8List(3)..[0] = 4..[1] = 3..[2] = 5;

If I could trust the compiler to optimize Uint8List.from(listLiteral), then that would be nice. Until then I'd avoid the extra allocation and iteration.

Creating and populating a Uint8List increases code-size.
A const list may or may not increase code size or memory pressure, depending on how it's implemented.

Also consider:

final b = "\x04\x03\x05".codeUnits; 

👿

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-not-planned Closed as we don't intend to take action on the reported issue P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

7 participants