Discussion:
[dart-misc] const vs. new
Alexandre Ardhuin
2015-08-28 21:02:52 UTC
Permalink
Hi all,

Suppose you have a class that can be used with a const constructor:

class A {
const A();
}

From a performance point of view I expected that using `const A()` was way
better than using `new A()` . However in a quick benchmark on DartVM-1.11.3
there's almost no difference.

1. Is it better to use `const` when possible?
2. What's the use-case where you do want to use `new` instead of `const`
(when `const` can be used)?
3. Should we see better performances with `const` ?

Cheers,
Alexandre
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
'Srdjan Mitrovic' via Dart Misc
2015-08-28 21:14:36 UTC
Permalink
Hi,

Can you please provide the source of your benchmark?

The expected difference:

var a = const A(); // allocated at compile time (i.e., once)

vs

var a = new A(); // allocated at runtime (i.e., every time that line is
executed)

Cheers,

- Srdjan


On Fri, Aug 28, 2015 at 2:02 PM, Alexandre Ardhuin <
Post by Alexandre Ardhuin
Hi all,
class A {
const A();
}
From a performance point of view I expected that using `const A()` was way
better than using `new A()` . However in a quick benchmark on DartVM-1.11.3
there's almost no difference.
1. Is it better to use `const` when possible?
2. What's the use-case where you do want to use `new` instead of `const`
(when `const` can be used)?
3. Should we see better performances with `const` ?
Cheers,
Alexandre
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Alexandre Ardhuin
2015-08-28 21:31:51 UTC
Permalink
Here's the benchmark that gives me on Dart VM 1.11.3

$ dart bin/main.dart
new(RunTime): 480.07681228996637 us.
const(RunTime): 484.8484848484849 us.


import 'package:benchmark_harness/benchmark_harness.dart';

final NB_LOOP = 100000;
class NewBenchmark extends BenchmarkBase {
const NewBenchmark() : super("new");
void run() {
for (int i = 0; i < NB_LOOP; i++) new A();
}
}
class ConstBenchmark extends BenchmarkBase {
const ConstBenchmark() : super("const");
void run() {
for (int i = 0; i < NB_LOOP; i++) const A();
}
}
class A {
const A();
}
main() {
// Run TemplateBenchmark
const NewBenchmark().report();
const ConstBenchmark().report();
}

Alexandre


2015-08-28 23:14 GMT+02:00 'Srdjan Mitrovic' via Dart Misc <
Post by 'Srdjan Mitrovic' via Dart Misc
Hi,
Can you please provide the source of your benchmark?
var a = const A(); // allocated at compile time (i.e., once)
vs
var a = new A(); // allocated at runtime (i.e., every time that line is
executed)
Cheers,
- Srdjan
On Fri, Aug 28, 2015 at 2:02 PM, Alexandre Ardhuin <
Post by Alexandre Ardhuin
Hi all,
class A {
const A();
}
From a performance point of view I expected that using `const A()` was
way better than using `new A()` . However in a quick benchmark on
DartVM-1.11.3 there's almost no difference.
1. Is it better to use `const` when possible?
2. What's the use-case where you do want to use `new` instead of `const`
(when `const` can be used)?
3. Should we see better performances with `const` ?
Cheers,
Alexandre
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
'Bob Nystrom' via Dart Misc
2015-08-28 21:17:39 UTC
Permalink
On Fri, Aug 28, 2015 at 2:02 PM, Alexandre Ardhuin <
Post by Alexandre Ardhuin
1. Is it better to use `const` when possible?
I generally use const when it's required—default values and metadata
annotations—or when I want to make it clear that a blob of data really is a
true compile time, global lifetime constant. Otherwise, I don't bother. I
don't think it adds much value over, say, final.
Post by Alexandre Ardhuin
2. What's the use-case where you do want to use `new` instead of `const`
(when `const` can be used)?
If you want the objects to have different identities:

class Key {}

var map = {};
map[new Key()] = "first";
map[new Key()] = "second";


This map will only end up with one item if you change those news to const.
Post by Alexandre Ardhuin
3. Should we see better performances with `const` ?
<shrug>

Optimization is implementation-specific and a moving target. I try to write
code to be simple and readable. Then, if needed, I optimize for the
implementations it runs on based on the feedback I get from an actual
profiler. I can't think of any cases where I found it useful to optimize by
making something const.

Cheers!

- bob
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Alexandre Ardhuin
2015-08-28 21:44:20 UTC
Permalink
Thanks for the answers. I agree for 1 and 3.

I was also wondering if const constructors were included on the topic
"optional new". If yes the vm could automatically use `const` when it's
possible (according that `const` is better from a perf/mem point of view).

Cheers,
Alexandre
Post by 'Srdjan Mitrovic' via Dart Misc
On Fri, Aug 28, 2015 at 2:02 PM, Alexandre Ardhuin <
Post by Alexandre Ardhuin
1. Is it better to use `const` when possible?
I generally use const when it's required—default values and metadata
annotations—or when I want to make it clear that a blob of data really is a
true compile time, global lifetime constant. Otherwise, I don't bother. I
don't think it adds much value over, say, final.
Post by Alexandre Ardhuin
2. What's the use-case where you do want to use `new` instead of `const`
(when `const` can be used)?
class Key {}
var map = {};
map[new Key()] = "first";
map[new Key()] = "second";
This map will only end up with one item if you change those news to const.
Post by Alexandre Ardhuin
3. Should we see better performances with `const` ?
<shrug>
Optimization is implementation-specific and a moving target. I try to
write code to be simple and readable. Then, if needed, I optimize for the
implementations it runs on based on the feedback I get from an actual
profiler. I can't think of any cases where I found it useful to optimize by
making something const.
Cheers!
- bob
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
'Vyacheslav Egorov' via Dart Misc
2015-08-29 00:24:12 UTC
Permalink
You need to make new A() escape (e.g. store it into some global) otherwise
VM will remove the allocation and both loops will do nothing:

- new A() will do nothing because optimizer optimizes new A() away;
- const A() will do nothing because it is evaluated at compile time once.




On Fri, Aug 28, 2015 at 11:44 PM Alexandre Ardhuin <
Post by Alexandre Ardhuin
Thanks for the answers. I agree for 1 and 3.
I was also wondering if const constructors were included on the topic
"optional new". If yes the vm could automatically use `const` when it's
possible (according that `const` is better from a perf/mem point of view).
Cheers,
Alexandre
Post by 'Srdjan Mitrovic' via Dart Misc
On Fri, Aug 28, 2015 at 2:02 PM, Alexandre Ardhuin <
Post by Alexandre Ardhuin
1. Is it better to use `const` when possible?
I generally use const when it's required—default values and metadata
annotations—or when I want to make it clear that a blob of data really is a
true compile time, global lifetime constant. Otherwise, I don't bother. I
don't think it adds much value over, say, final.
Post by Alexandre Ardhuin
2. What's the use-case where you do want to use `new` instead of `const`
(when `const` can be used)?
class Key {}
var map = {};
map[new Key()] = "first";
map[new Key()] = "second";
This map will only end up with one item if you change those news to const.
Post by Alexandre Ardhuin
3. Should we see better performances with `const` ?
<shrug>
Optimization is implementation-specific and a moving target. I try to
write code to be simple and readable. Then, if needed, I optimize for the
implementations it runs on based on the feedback I get from an actual
profiler. I can't think of any cases where I found it useful to optimize by
making something const.
Cheers!
- bob
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an
--
// Vyacheslav Egorov
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Alexandre Ardhuin
2015-08-29 13:20:02 UTC
Permalink
Indeed I can see now a difference :

$ dart bin/main.dart
new(RunTime): 29753000.0 us.
const(RunTime): 12531000.0 us.

Thanks for the explanation,
Alexandre

------

final NB_LOOP = 10000000;
class NewBenchmark extends BenchmarkBase {
const NewBenchmark() : super("new");
static var l = [];
void run() {
for (int i = 0; i < NB_LOOP; i++) l.add(new A());
}
}
class ConstBenchmark extends BenchmarkBase {
const ConstBenchmark() : super("const");
static var l = [];
void run() {
for (int i = 0; i < NB_LOOP; i++) l.add(const A());
}
}
class A {
const A();
}
main() {
// Run TemplateBenchmark
const NewBenchmark().report();
const ConstBenchmark().report();
}




2015-08-29 2:24 GMT+02:00 'Vyacheslav Egorov' via Dart Misc <
Post by 'Vyacheslav Egorov' via Dart Misc
You need to make new A() escape (e.g. store it into some global) otherwise
- new A() will do nothing because optimizer optimizes new A() away;
- const A() will do nothing because it is evaluated at compile time once.
On Fri, Aug 28, 2015 at 11:44 PM Alexandre Ardhuin <
Post by Alexandre Ardhuin
Thanks for the answers. I agree for 1 and 3.
I was also wondering if const constructors were included on the topic
"optional new". If yes the vm could automatically use `const` when it's
possible (according that `const` is better from a perf/mem point of view).
Cheers,
Alexandre
Post by 'Srdjan Mitrovic' via Dart Misc
On Fri, Aug 28, 2015 at 2:02 PM, Alexandre Ardhuin <
Post by Alexandre Ardhuin
1. Is it better to use `const` when possible?
I generally use const when it's required—default values and metadata
annotations—or when I want to make it clear that a blob of data really is a
true compile time, global lifetime constant. Otherwise, I don't bother. I
don't think it adds much value over, say, final.
Post by Alexandre Ardhuin
2. What's the use-case where you do want to use `new` instead of
`const` (when `const` can be used)?
class Key {}
var map = {};
map[new Key()] = "first";
map[new Key()] = "second";
This map will only end up with one item if you change those news to const.
Post by Alexandre Ardhuin
3. Should we see better performances with `const` ?
<shrug>
Optimization is implementation-specific and a moving target. I try to
write code to be simple and readable. Then, if needed, I optimize for the
implementations it runs on based on the feedback I get from an actual
profiler. I can't think of any cases where I found it useful to optimize by
making something const.
Cheers!
- bob
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to
http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an
--
// Vyacheslav Egorov
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Alexandre Ardhuin
2015-08-30 20:41:31 UTC
Permalink
Looking at the sources of the sdk `const` and `new` don't seem to be always
used consistently. For instance with dart.convert:LineSplitter there are 39
occurances of `new LineSplitter()` and 8 of `const LineSplitter()`.

If `const` performs better (like the previous bench showed) there are
several places where the performances could be improved.

Another exemple with `UTF8.encode` that uses `Utf8Codec.encoder` which
returns every time a `new Utf8Encoder()` although `const Utf8Encoder()`
could be used.

The more I think about "optional new", the more I hope "infered new/const".

Alexandre
Post by Alexandre Ardhuin
Thanks for the answers. I agree for 1 and 3.
I was also wondering if const constructors were included on the topic
"optional new". If yes the vm could automatically use `const` when it's
possible (according that `const` is better from a perf/mem point of view).
Cheers,
Alexandre
Post by 'Srdjan Mitrovic' via Dart Misc
On Fri, Aug 28, 2015 at 2:02 PM, Alexandre Ardhuin <
Post by Alexandre Ardhuin
1. Is it better to use `const` when possible?
I generally use const when it's required—default values and metadata
annotations—or when I want to make it clear that a blob of data really is a
true compile time, global lifetime constant. Otherwise, I don't bother. I
don't think it adds much value over, say, final.
Post by Alexandre Ardhuin
2. What's the use-case where you do want to use `new` instead of `const`
(when `const` can be used)?
class Key {}
var map = {};
map[new Key()] = "first";
map[new Key()] = "second";
This map will only end up with one item if you change those news to const.
Post by Alexandre Ardhuin
3. Should we see better performances with `const` ?
<shrug>
Optimization is implementation-specific and a moving target. I try to
write code to be simple and readable. Then, if needed, I optimize for the
implementations it runs on based on the feedback I get from an actual
profiler. I can't think of any cases where I found it useful to optimize by
making something const.
Cheers!
- bob
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Dániel Varga
2015-08-31 09:26:20 UTC
Permalink
I believe there is a place for consts even if there was no performance gain
at all. I did a little something here to explain a
usecase: https://dartpad.dartlang.org/5bca3915f4b55dfa7c4e

Daniel
Post by Alexandre Ardhuin
Looking at the sources of the sdk `const` and `new` don't seem to be
always used consistently. For instance with dart.convert:LineSplitter there
are 39 occurances of `new LineSplitter()` and 8 of `const LineSplitter()`.
If `const` performs better (like the previous bench showed) there are
several places where the performances could be improved.
Another exemple with `UTF8.encode` that uses `Utf8Codec.encoder` which
returns every time a `new Utf8Encoder()` although `const Utf8Encoder()`
could be used.
The more I think about "optional new", the more I hope "infered new/const".
Alexandre
Post by Alexandre Ardhuin
Thanks for the answers. I agree for 1 and 3.
I was also wondering if const constructors were included on the topic
"optional new". If yes the vm could automatically use `const` when it's
possible (according that `const` is better from a perf/mem point of view).
Cheers,
Alexandre
2015-08-28 23:17 GMT+02:00 'Bob Nystrom' via Dart Misc <
Post by 'Srdjan Mitrovic' via Dart Misc
On Fri, Aug 28, 2015 at 2:02 PM, Alexandre Ardhuin <
Post by Alexandre Ardhuin
1. Is it better to use `const` when possible?
I generally use const when it's required—default values and metadata
annotations—or when I want to make it clear that a blob of data really is a
true compile time, global lifetime constant. Otherwise, I don't bother. I
don't think it adds much value over, say, final.
Post by Alexandre Ardhuin
2. What's the use-case where you do want to use `new` instead of
`const` (when `const` can be used)?
class Key {}
var map = {};
map[new Key()] = "first";
map[new Key()] = "second";
This map will only end up with one item if you change those news to const.
Post by Alexandre Ardhuin
3. Should we see better performances with `const` ?
<shrug>
Optimization is implementation-specific and a moving target. I try to
write code to be simple and readable. Then, if needed, I optimize for the
implementations it runs on based on the feedback I get from an actual
profiler. I can't think of any cases where I found it useful to optimize by
making something const.
Cheers!
- bob
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to
http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Felipe
2015-08-31 12:46:57 UTC
Permalink
Hi Daniel. The "identical" (is for performance) and you can use factory
constructors insted.

As you put it in the example the way to verify that two objects are equal
must be overwriting the "equals" (business identity) and not using
"const". As Alexandre says the "const" has to be inferred.


I believe there is a place for consts even if there was no performance gain
Post by Dániel Varga
https://dartpad.dartlang.org/5bca3915f4b55dfa7c4e
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
'Bob Nystrom' via Dart Misc
2015-08-31 16:21:03 UTC
Permalink
Post by Felipe
Hi Daniel. The "identical" (is for performance) and you can use factory
constructors insted.
I agree.
Post by Felipe
As you put it in the example the way to verify that two objects are equal
must be overwriting the "equals" (business identity) and not using
"const". As Alexandre says the "const" has to be inferred.
Because of the way const is specified, it can't be automatically inferred.
The language *requires* constant constructor invocations with the same
arguments to be canonicalized. In other words, this:

class Foo {
final String arg;
const Foo(this.arg);
}

var a = const Foo("string");
var b = const Foo("string");
print(identical(a, b)); // true


The language says this *must* print true. However this:

class Foo {
final String arg;
const Foo(this.arg);
}

var a = new Foo("string");
var b = new Foo("string");
print(identical(a, b)); // false


is likewise specified that it *must* print false. That means we can't infer
const since it changes the visible semantics of the code.

Personally, I think the const canonicalization behavior isn't worth the
complexity it causes. I would remove the requirement that instances of
const classes with the same parameters are identical. If you want to
canonicalize them, do it yourself. Like you note, a factory constructor is
perfectly suited for that.

Cheers!

- bob
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Alexandre Ardhuin
2015-08-31 16:34:02 UTC
Permalink
By "infered new/const" I meant at call site.

class Foo {
final String arg;
const Foo(this.arg);
}

var a = Foo("string"); // the same as `const Foo("string")`

f(String s) => Foo(s); // the same as `new Foo(s)`


Alexandre
Post by 'Bob Nystrom' via Dart Misc
Post by Felipe
Hi Daniel. The "identical" (is for performance) and you can use factory
constructors insted.
I agree.
Post by Felipe
As you put it in the example the way to verify that two objects are equal
must be overwriting the "equals" (business identity) and not using
"const". As Alexandre says the "const" has to be inferred.
Because of the way const is specified, it can't be automatically inferred.
The language *requires* constant constructor invocations with the same
class Foo {
final String arg;
const Foo(this.arg);
}
var a = const Foo("string");
var b = const Foo("string");
print(identical(a, b)); // true
class Foo {
final String arg;
const Foo(this.arg);
}
var a = new Foo("string");
var b = new Foo("string");
print(identical(a, b)); // false
is likewise specified that it *must* print false. That means we can't
infer const since it changes the visible semantics of the code.
Personally, I think the const canonicalization behavior isn't worth the
complexity it causes. I would remove the requirement that instances of
const classes with the same parameters are identical. If you want to
canonicalize them, do it yourself. Like you note, a factory constructor is
perfectly suited for that.
Cheers!
- bob
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
tatumizer-v0.2
2015-08-31 17:14:15 UTC
Permalink
@Alexandre: sorry for giving unsolicited advice, but this idea helps me a
lot in performance testing: don't print total time for entire loop - print
time per a single iteration instead, in nanoseconds. E.g., in your first
test, had you printed nanosec/iteration, you would see that creation of
object instance takes 4 nanoseconds. Clearly, this is beyond wildest
dreams, so you would know right away that compiler is cheating :)
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Alexandre Ardhuin
2015-08-31 20:48:43 UTC
Permalink
Indeed. Here are the numbers for the code I paste below.
$ dart bin/main.dart
new(RunTime): 0.1950216625187184 us.
const(RunTime): 0.07694578776828206 us.

Alexandre

----
import 'package:benchmark_harness/benchmark_harness.dart';
import 'dart:convert';

var a, b;

class NewBenchmark extends BenchmarkBase {
const NewBenchmark() : super("new");

void run() {
a = new Utf8Encoder();
b = new Utf8Decoder();
}
}

class ConstBenchmark extends BenchmarkBase {
const ConstBenchmark() : super("const");

void run() {
a = const Utf8Encoder();
b = const Utf8Decoder();
}
}

main() {
const NewBenchmark().report();
const ConstBenchmark().report();
}
Post by tatumizer-v0.2
@Alexandre: sorry for giving unsolicited advice, but this idea helps me a
lot in performance testing: don't print total time for entire loop - print
time per a single iteration instead, in nanoseconds. E.g., in your first
test, had you printed nanosec/iteration, you would see that creation of
object instance takes 4 nanoseconds. Clearly, this is beyond wildest
dreams, so you would know right away that compiler is cheating :)
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
'Bob Nystrom' via Dart Misc
2015-08-31 20:45:46 UTC
Permalink
On Mon, Aug 31, 2015 at 9:34 AM, Alexandre Ardhuin <
Post by Alexandre Ardhuin
By "infered new/const" I meant at call site.
class Foo {
final String arg;
const Foo(this.arg);
}
var a = Foo("string"); // the same as `const Foo("string")`
f(String s) => Foo(s); // the same as `new Foo(s)`
Oof, I think that could cause really strange bugs. Say you have code like:

var a = Foo(someVariableFromSomeOtherPackage);


When you write that code, someVariableFromSomeOtherPackage is a constant.
At some point, it changes to a final variable. Now your instance of Foo is
no longer const and its canonicalization behavior has changed. :-/

- bob
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Alexandre Ardhuin
2015-08-31 21:23:47 UTC
Permalink
Perhaps canonicalization could be removed (in a breaking change) but it is
specified in the current version of the language. So is it a bad practice
to use const for this feature?

Actually I don't understand why const still exists without its
canonicalization property. If I understand the thread
https://groups.google.com/a/dartlang.org/forum/#!topic/misc/pR2ClUIB_Aw
correctly, the constant expressions landed to fix an initialization issue
and to be sure main is executed first (with lazy initializations it's no
longer the case).
Post by 'Bob Nystrom' via Dart Misc
On Mon, Aug 31, 2015 at 9:34 AM, Alexandre Ardhuin <
Post by Alexandre Ardhuin
By "infered new/const" I meant at call site.
class Foo {
final String arg;
const Foo(this.arg);
}
var a = Foo("string"); // the same as `const Foo("string")`
f(String s) => Foo(s); // the same as `new Foo(s)`
var a = Foo(someVariableFromSomeOtherPackage);
When you write that code, someVariableFromSomeOtherPackage is a constant.
At some point, it changes to a final variable. Now your instance of Foo is
no longer const and its canonicalization behavior has changed. :-/
I didn't see this issue with identical(a,b) and indeed, it can lead to some
strange behaviours. Thanks for your example.

Alexandre
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
'Bob Nystrom' via Dart Misc
2015-08-31 22:26:39 UTC
Permalink
On Mon, Aug 31, 2015 at 2:23 PM, Alexandre Ardhuin <
Post by Alexandre Ardhuin
Perhaps canonicalization could be removed (in a breaking change) but it is
specified in the current version of the language. So is it a bad practice
to use const for this feature?
It's specified in the language, so it's not crazy to rely on
canonicalization. But, personally, I don't use const often and when I do I
don't tend to rely on canonicalization either. In all of the cases I can
think of where I used the "same" constant in multiple places, I just stored
it in a named constant somewhere and accessed through that name instead of
"constructing" it in both places and relying on canonicalization.
Post by Alexandre Ardhuin
Actually I don't understand why const still exists without its
canonicalization property.
Probably mostly historical baggage, though I can't speak for everyone on
the team.

There are a few places where it still has some use:

1. Default values must be const. It's a static warning if an override of
a method with an optional parameter uses a different default value than the
inherited member. To determine if the values are different statically, you
need to be able to evaluate those expressions reliably. Requiring them to
be const makes that trivial.

2. Static warnings have to be reported if the value in a switch case is
of a disallowed type. Requiring them to be const expressions helps that too.

But, honestly, those restrictions could be removed without causing too much
pain, I think.
Post by Alexandre Ardhuin
If I understand the thread
https://groups.google.com/a/dartlang.org/forum/#!topic/misc/pR2ClUIB_Aw
correctly, the constant expressions landed to fix an initialization issue
and to be sure main is executed first (with lazy initializations it's no
longer the case).
Yup.

Cheers!

- bob
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
'Paul Berry' via Dart Misc
2015-08-31 22:37:35 UTC
Permalink
Another reason constness exists in the language is for annotations.
Annotations have to be possible to evaluate at compile-time, since they
affect the behavior of mirrors as well as enabling and disabling certain
analyzer warnings. if we dropped the notion of "const" from the language,
then both dart2js and the analysis server would need to contain a full
execution engine in order to evaluate annotations. That would carry
implications for security as well as responsiveness (we want the analysis
server to be able to update its analysis fast, so that it can provide code
completions and errors/warnings as you type).
Post by 'Bob Nystrom' via Dart Misc
On Mon, Aug 31, 2015 at 2:23 PM, Alexandre Ardhuin <
Post by Alexandre Ardhuin
Perhaps canonicalization could be removed (in a breaking change) but it
is specified in the current version of the language. So is it a bad
practice to use const for this feature?
It's specified in the language, so it's not crazy to rely on
canonicalization. But, personally, I don't use const often and when I do I
don't tend to rely on canonicalization either. In all of the cases I can
think of where I used the "same" constant in multiple places, I just stored
it in a named constant somewhere and accessed through that name instead of
"constructing" it in both places and relying on canonicalization.
Post by Alexandre Ardhuin
Actually I don't understand why const still exists without its
canonicalization property.
Probably mostly historical baggage, though I can't speak for everyone on
the team.
1. Default values must be const. It's a static warning if an override
of a method with an optional parameter uses a different default value than
the inherited member. To determine if the values are different statically,
you need to be able to evaluate those expressions reliably. Requiring them
to be const makes that trivial.
2. Static warnings have to be reported if the value in a switch case
is of a disallowed type. Requiring them to be const expressions helps that
too.
But, honestly, those restrictions could be removed without causing too
much pain, I think.
Post by Alexandre Ardhuin
If I understand the thread
https://groups.google.com/a/dartlang.org/forum/#!topic/misc/pR2ClUIB_Aw
correctly, the constant expressions landed to fix an initialization issue
and to be sure main is executed first (with lazy initializations it's no
longer the case).
Yup.
Cheers!
- bob
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
'Bob Nystrom' via Dart Misc
2015-08-31 23:40:01 UTC
Permalink
On Mon, Aug 31, 2015 at 3:37 PM, 'Paul Berry' via Dart Misc <
Post by 'Paul Berry' via Dart Misc
Another reason constness exists in the language is for annotations.
Annotations have to be possible to evaluate at compile-time, since they
affect the behavior of mirrors as well as enabling and disabling certain
analyzer warnings. if we dropped the notion of "const" from the language,
then both dart2js and the analysis server would need to contain a full
execution engine in order to evaluate annotations. That would carry
implications for security as well as responsiveness (we want the analysis
server to be able to update its analysis fast, so that it can provide code
completions and errors/warnings as you type).
Ah, yes. You are exactly right.

I meant to add that as bullet point 3, but I got distracted and forgot
about it by the time I got back to my unfinished email.

This is an issue not just with analysis but with static metaprogramming
too. When packages like Angular want to run transformers at "compile" time
that process and use annotations, it's important that they can get the
actual values of annotation parameters without having to evaluate arbitrary
Dart code.

Cheers!

- bob
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Alexandre Ardhuin
2015-09-01 07:43:25 UTC
Permalink
Thanks for your time and the explanations.

Alexandre
Post by 'Bob Nystrom' via Dart Misc
On Mon, Aug 31, 2015 at 3:37 PM, 'Paul Berry' via Dart Misc <
Post by 'Paul Berry' via Dart Misc
Another reason constness exists in the language is for annotations.
Annotations have to be possible to evaluate at compile-time, since they
affect the behavior of mirrors as well as enabling and disabling certain
analyzer warnings. if we dropped the notion of "const" from the language,
then both dart2js and the analysis server would need to contain a full
execution engine in order to evaluate annotations. That would carry
implications for security as well as responsiveness (we want the analysis
server to be able to update its analysis fast, so that it can provide code
completions and errors/warnings as you type).
Ah, yes. You are exactly right.
I meant to add that as bullet point 3, but I got distracted and forgot
about it by the time I got back to my unfinished email.
This is an issue not just with analysis but with static metaprogramming
too. When packages like Angular want to run transformers at "compile" time
that process and use annotations, it's important that they can get the
actual values of annotation parameters without having to evaluate arbitrary
Dart code.
Cheers!
- bob
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Felipe
2015-08-29 03:17:49 UTC
Permalink
I think the "const" keyword should be removed unless it involves a
tremendous improvement in performance.

It is a confusing concept and from the abstract point of view I do not care
if it is allocated at "compile" or "runtime".

Cheers.

- Felipe
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Filipe Morgado
2015-08-29 17:33:37 UTC
Permalink
I find const to be a fundamental concept of Dart, specially if one uses annotations extensively.

I think it should be improved, if possible, rather than removed.
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Continue reading on narkive:
Loading...