-
-
Notifications
You must be signed in to change notification settings - Fork 694
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
AlignedStorage and InstanceStorage types #5426
Conversation
Not sure why tests would fail on win32. Can anyone offer insight? |
Gah... So there is no way of telling at compile time whether invariants are enabled or not? |
@andralex I tagged you because I can't remember if it's all Phobos symbols, or only std.algorithm/std.range that you want to be notified of. |
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.
What is the motivating case for this? How is it different from simply placing align(x)
on the needed member?
@andralex This need not necessarily be a member, it could be a local, a global, an allocated storage... |
@thewilsonator unstall please, I'm resuming this :) |
Please rebase it then ;) |
c1df322
to
1afacb2
Compare
Thanks for your pull request and interest in making D better, @radcapricorn! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub fetch digger
dub run digger -- build "master + phobos#5426" |
|
These are two untyped storage types that can be used in Phobos and in user code when there's a need for specific aligned space. E.g. we could use this as static storage to emplace() objects instead of allocating them with GC, where functions need to be made @nogc. Added `cent` check, improved coverage. unittest versioning; typo Removed invariant test cent fix rebase, remove format Example tests, style fixes
1afacb2
to
9eca834
Compare
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 think some serious convincing is needed on why this belongs in the standard library.
at compile time, an assert will be issued at runtime when a misaligned | ||
AlignedStorage is used. | ||
*/ | ||
struct AlignedStorage(size_t Size, size_t Alignment = platformAlignment) |
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 somewhat sophisticated API is ill-advised. We're looking at an "abstraction that doesn't abstract": a data member that is nominally private, yet trivially made public by means of accessors. There's no need for APIs for length, slicing etc because all they do is morally equivalent with:
struct AlignedStorage(size_t Size, size_t Alignment = platformAlignment)
{
align(Alignment) void[Size] payload;
}
Everything needed is readily accessible by means of .payload
. This also emphasizes the question of why we need to put in the standard library what's essentially a three-line trick.
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.
...with the exception of an invariant that safeguards against misaligned moves :)
For classes, the size and alignment of storage are the size and alignment | ||
of class instance representation, not those of class reference. | ||
*/ | ||
template InstanceStorage(T) |
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.
nicely done
static assert(InstanceStorage!Arbitrary.alignof == Arbitrary.alignof); | ||
} | ||
|
||
private size_t platformAlignment() @safe @nogc nothrow pure |
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 think we have something like this in std.experimental.allocator.
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.
Yes. What would you suggest? Make this package
and import in std.experimental.allocator
? Or perhaps even public
? User code could benefit from that information.
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.
Is it public in std.experimental.allocator
? If so public
, if not package
and unify the definitions.
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.
It is public
.
} | ||
|
||
InstanceStorage!SafeClass buf; | ||
auto support = (() @trusted => cast(SafeClass) (buf.ptr))(); |
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.
If this is the intended usage of InstanceStorage
it seems to me that it would be better done as a type safe layer over AlignedStorage
than simply an alias.
As far as convincing goes, there are already places in the standard library that use this kind of storage. Allocators is one, |
@andralex are you convinced about this addition? @atilaneves What are your thoughts on this? @radcapricorn you still need to fix some style issues for this to be ready to merge. Also, a changelog entry is required since you are adding new symbols. |
I'm unclear about (a) what the use cases are, and (b) how fit this API is for those. Far as I can tell the main use case is "I want inert storage fit for one object of a given type". If that's the case, all we need is:
Maybe we could promote that as an idiom instead of putting it in stdlib because it's an advanced systems use? For class instances, it seems more difficult so maybe that would be worth finding a good API for. But even in that case, don't all classes have the same alignment (same as a pointer)? So an array of |
I don't think enough justification has been given for the inclusion of this in the standard library. |
@andralex @atilaneves The argument given by @radcapricorn is that the invariant safeguards against misaligned moves. |
But why necessarily in the standard library? |
I am going to close this as @radcapricorn seems to have abandoned it and @atilaneves and @andralex are against adding it to the standard library. @radcapricorn please reopen if you ever turn back it. |
These are two untyped storage types that can be used in Phobos
and in user code when there's a need for specific aligned space.
E.g. we could use this as static storage to emplace() objects
instead of allocating them with GC, where functions need to be made
@nogc
.