-
Notifications
You must be signed in to change notification settings - Fork 26
Conversation
996e376
to
38bf38a
Compare
Note: this is also useful as default value for functions/methods that takes a |
Should add a changelog entry and maybe bump the version? CC @lrhn |
I would definitely put this in |
@lrhn, sorry the example at top was a bit confusing, this is an identity I updated the PR description. And updated changelog + bumped version. |
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.
Also need (some smallish) testing.
lib/src/identity_codec.dart
Outdated
const IdentityCodec(); | ||
|
||
@override | ||
Converter<T, T> get decoder => _IdentityConverter(); |
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.
I prefer to be explicit instead of depending on inference, so consider _IdentityConverter<T>()
here and below.
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.
Consider making it a field:
final Converter<T, T> decoder = _IdentityConverter<T>();
Converter<T, T> get encoder => decoder;
Then you only create one converter per codec. It costs more if you end up not using the codec, but otherwise it should be a wash.
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.
Neat trick..
To be fair I was hoping the JIT would just optimize it out...
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.
So...
final Converter<T, T> decoder = const _IdentityConverter<T>();
Converter<T, T> get encoder => decoder;
Won't work because:
[dart] A constant creation can't use a type parameter as a type argument. [const_with_type_parameters]
It also fails in test.
We should want IdentityCodec
to have a const constructor, so that it can be used as default value for optional parameters.
If we really want to optimize things we could do:
// global identity converter, so we only create one instance
final _identityConverter = const _IdentityConverter<dynamic>();
class IdentityCodec<T> extends Codec<T, T> {
const IdentityCodec();
Converter<T, T> get decoder => _identityConverter.cast();
Converter<T, T> get encoder => _identityConverter.cast();
Codec<T, R> fuse<R>(Codec<T, R> other) => other;
}
But I'm not quite sure of the wisdom of using dynamic, vs. hoping the optimizer will trace away the useless _IdentityConverter
instances created with:
Converter<T, T> get decoder => _IdentityConverter<T>();
Converter<T, T> get encoder => _IdentityConverter<T>();
Maybe it's best to wait for a performance problem before we think ahead. Or we could argue that we should have language features that allows const type parameterization :)
/// | ||
/// This will return the [other] codec, as the identity codec is trivial. | ||
@override | ||
Codec<T, R> fuse<R>(Codec<T, R> other) => other; |
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.
This is not handling the case of other.fuse(identityCodec)
.
On the other hand, I don't want the generic fuse
to know about IdentifyCodec
(it could mean that the class is harder to tree-shake).
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.
We could document this behavior, but that seems overkill.. And cost of doing it wrong is likely small anyways.
b8e1426
to
5914dc1
Compare
@lrhn, if by chance you have time to accept/reject this that would be great. |
Thanks. I've merged it and will publish a new version. |
+ Fixes DDC compilation regression introduced by dart-archive#17
Fixes DDC compilation regression introduced by #17
Added IdentityCodec
…nvert#18) Fixes DDC compilation regression introduced by dart-archive/convert#17
This is useful you're making a parameterized type that can be fused with a
Codec
to create a wrapper with different type parameters...Example of when this could be useful to have:
When implementing functions that accept an optional
Codec
a good default isIdentityCodec
, if you expect users to use the function without theCodec
.