<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I was thinking for reference types we would probably want to use the extra space in the refcount field (as discussed earlier in the thread). &nbsp;For value types, we might want to have a single data structure (probably COW) holding all associated values for the value types on the stack frame. &nbsp;That way, we don’t have to double the size of an Int if someone adds an associated value in one or two cases. &nbsp;We only add a pointer size to the entire stack frame in the case where no value types have associated values.</div><div class=""><br class=""></div><div class="">Thoughts?</div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Jon</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><pre style="white-space: pre-wrap; background-color: rgb(255, 255, 255);" class="">Karl, interesting point... perhaps a similar scheme could work for value
types (using the COW refcount)?

On Thu, 13 Oct 2016 at 16:02 Karl Wagner &lt;<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">razielim at gmail.com</a>&gt; wrote:

&gt;<i class=""> That's great! I suppose the idea of allocating a bit of extra storage for
</i>&gt;<i class=""> similar data in value types is some sort of heresy?
</i>&gt;<i class="">
</i>&gt;<i class=""> Would there be a conceptual reason for that; which explains why it's okay
</i>&gt;<i class=""> for reference-types but not for values? Personally I feel like it's a kind
</i>&gt;<i class=""> of C legacy, due to performance and layout expectations that C sets about
</i>&gt;<i class=""> value-types ¯\_(ツ)_/¯
</i>&gt;<i class="">
</i>&gt;<i class=""> Karl
</i>&gt;<i class="">
</i>&gt;<i class="">
</i>&gt;<i class=""> Sent from my iPad
</i>&gt;<i class="">
</i>&gt;<i class=""> On 12 Oct 2016, at 07:54, Greg Parker &lt;<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">gparker at apple.com</a>&gt; wrote:
</i>&gt;<i class="">
</i>&gt;<i class="">
</i>&gt;<i class=""> On Oct 11, 2016, at 3:02 PM, Jay Abbott via swift-evolution &lt;
</i>&gt;<i class=""> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">swift-evolution at swift.org</a>&gt; wrote:
</i>&gt;<i class="">
</i>&gt;<i class=""> Implementation idea No. 4:
</i>&gt;<i class="">
</i>&gt;<i class="">
</i>&gt;<i class=""> The basic concept is that the dynamic linker would fixup the offsets as
</i>&gt;<i class=""> well as relocating the addersses, allowing the size of objects (and maybe
</i>&gt;<i class=""> structs?) to change at link-time. The process might be something like this:
</i>&gt;<i class="">
</i>&gt;<i class=""> * References to members defined in extensions would compile to have an
</i>&gt;<i class=""> offset symbol instead of a value - so they can be fixed up later
</i>&gt;<i class=""> * The linker would scan all the shared objects that are referenced (and
</i>&gt;<i class=""> thus might get linked)
</i>&gt;<i class=""> * Build up a list of Stored Properties In ExtensionS (SPIES, muhahaha) for
</i>&gt;<i class=""> each class.
</i>&gt;<i class=""> * Append the extra fields (increase the size the class), decide where each
</i>&gt;<i class=""> member goes in the extended layout and fixup the offsets
</i>&gt;<i class=""> * Carry on with normal relocation
</i>&gt;<i class="">
</i>&gt;<i class=""> There are quite a few assumptions in the above, and probably quite a few
</i>&gt;<i class=""> misunderstandings about how things work on my part too (I'm not an expert
</i>&gt;<i class=""> at this), however I think it should work in principle. Some questions about
</i>&gt;<i class=""> my assumptions: Can linker know in advance all the potential modules that
</i>&gt;<i class=""> could be linked, or is this done more lazily and it only knows about what
</i>&gt;<i class=""> it's linking right now? Is it ok for the size to change - I don't know if
</i>&gt;<i class=""> it's a static sizeof() or if it could be (or already is) stored in the isa?
</i>&gt;<i class="">
</i>&gt;<i class="">
</i>&gt;<i class=""> This sort of scheme isn't dynamic enough. The worst-case is a extension in
</i>&gt;<i class=""> a library that gets dlopen()ed at runtime on a class that is already
</i>&gt;<i class=""> loaded. The build-time linker can't know anything about it. The loader and
</i>&gt;<i class=""> the runtime will see it, but at that point the class may already be in use
</i>&gt;<i class=""> and may already have instances allocated. If you want to handle the
</i>&gt;<i class=""> dlopen() case then you need some way to add storage to arbitrary objects
</i>&gt;<i class=""> that have already been allocated.
</i>&gt;<i class="">
</i>&gt;<i class="">
</i>&gt;<i class=""> Ole Begemann wrote:
</i>&gt;<i class="">
</i>&gt;<i class="">
</i>&gt;<i class=""> For what it's worth, Greg Parker (Cc'ed) started a discussion back in
</i>&gt;<i class=""> March that I think is relevant here:
</i>&gt;<i class=""> <a href="https://lists.swift.org/pipermail/swift-dev/Week-" class="">https://lists.swift.org/pipermail/swift-dev/Week-</a>
</i>&gt;<i class=""> of-Mon-20160314/001424.html
</i>&gt;<i class="">
</i>&gt;<i class=""> Here's the relevant part:
</i>&gt;<i class="">
</i>&gt;<i class=""> "I am considering a new representation for Swift refcounts and other
</i>&gt;<i class=""> per-object data. This is an outline of the scheme. Comments and suggestions
</i>&gt;<i class=""> welcome.
</i>&gt;<i class="">
</i>&gt;<i class=""> Today, each object stores 64-bits of refcounts and flags after the isa
</i>&gt;<i class=""> field.
</i>&gt;<i class="">
</i>&gt;<i class=""> In this new system, each object would store a pointer-size field after the
</i>&gt;<i class=""> isa field. This field would have two cases: it could store refcounts and
</i>&gt;<i class=""> flags, or it could store a pointer to a side allocation that would store
</i>&gt;<i class=""> refcounts and flags and additional per-object data.
</i>&gt;<i class="">
</i>&gt;<i class=""> Advantages:
</i>&gt;<i class=""> …
</i>&gt;<i class=""> * Allows inexpensive per-object storage for future features like
</i>&gt;<i class=""> associated references or class extensions with instance variables.
</i>&gt;<i class=""> …"
</i>&gt;<i class="">
</i>&gt;<i class=""> I don't know the current status of this idea (implemented? planned?
</i>&gt;<i class=""> abandoned?). Also, it's worth noting that this would only apply to classes,
</i>&gt;<i class=""> not value types.
</i>&gt;<i class="">
</i>&gt;<i class="">
</i>&gt;<i class=""> I'm working on this right now:
</i>&gt;<i class=""> <a href="https://github.com/gparker42/swift/tree/new-refcount-representation" class="">https://github.com/gparker42/swift/tree/new-refcount-representation</a>
</i>&gt;<i class="">
</i>&gt;<i class=""> If it goes well it will provide the runtime implementation space needed
</i>&gt;<i class=""> for associated objects or stored properties in extensions. Such storage
</i>&gt;<i class=""> would be less efficient than "real" stored properties. Any object with that
</i>&gt;<i class=""> storage attached would also suffer additional performance penalties to
</i>&gt;<i class=""> refcounting and deallocation. On the plus side there is no memory penalty
</i>&gt;<i class=""> to objects that don't have additional storage, and there is no contention
</i>&gt;<i class=""> over a big global association table like there is in Objective-C's
</i>&gt;<i class=""> associated object implementation.
</i>&gt;<i class="">
</i>&gt;<i class=""> Note that the runtime implementation is not the only problem. The
</i>&gt;<i class=""> optimizer folks hate the fact that stored properties in extensions defeat
</i>&gt;<i class=""> the compiler's visibility into the deinit behavior of all types, even if
</i>&gt;<i class=""> most types are unaffected at runtime.
</i>&gt;<i class="">
</i>&gt;<i class="">
</i>&gt;<i class=""> --
</i>&gt;<i class=""> Greg Parker     <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">gparker at apple.com</a>     Runtime Wrangler
</i>&gt;<i class="">
</i>&gt;<i class="">
</i>&gt;</pre></blockquote><div class=""><br class=""></div></div></body></html>