[swift-evolution] [swift-evolution-announce] [Review] SE-0184: Unsafe[Mutable][Raw][Buffer]Pointer: add missing methods, adjust existing labels for clarity, and remove deallocation size

Taylor Swift kelvin13ma at gmail.com
Fri Sep 8 01:11:19 CDT 2017


let me summarize why i think deallocate should lose the capacity parameter
for now:


   - up to now, the capacity argument has been poorly, even misleadingly
   documented. this means it is unlikely that existing users of the API are
   using it correctly.
   - deallocate(capacity:) allows for optimizations that could improve the
   performance of the Swift heap. however, these optimizations are not in
   place yet, nor will they be any time soon, so dropping the parameter would
   result in no performance regressions.
   - users coming from C/C++ are used to the malloc(_:) – free() pattern.
   Swift should support this both for learnability, and for
malloc-compatibility.

   - completely departing from the malloc memory model might save a few
   bytes of headers in the heap, but as discussed in this thread, there are
   other upsides of Swift being malloc-compatible.
   - there is nothing preventing us from reintroducing
   deallocate(allocatedCapacity:) in the future, once the runtime actually
   supports this feature. informed users looking for a performance boost could
   opt-in to use this API instead.
   - deprecating, and then un-deprecating deallocate(capacity:) might sound
   extra, but keeping this method unbothered is actually a bad idea. whoever
   is using it right now, is probably not using it correctly. The best
   strategy is to reintroduce it later as a well-documented *opt-in*
   feature. Grandfathering in old incorrect code offers no benefits and many
   problems.
   - lying to users is never a good idea. for the forseeable future,
   Heap.cpp calls free(), and Swift shouldn’t pretend like it supports
   something it doesn’t support.




On Thu, Sep 7, 2017 at 8:18 PM, Andrew Trick <atrick at apple.com> wrote:

>
> On Sep 7, 2017, at 5:56 PM, Jordan Rose <jordan_rose at apple.com> wrote:
>
>
>
> On Sep 7, 2017, at 17:46, Andrew Trick <atrick at apple.com> wrote:
>
>
> On Sep 7, 2017, at 5:40 PM, Joe Groff <jgroff at apple.com> wrote:
>
>
>
> But then given that, I don't understand why the 'capacity' parameter is
> necessary. Under what circumstances would it actually be faster than "just"
> calling malloc_size?
>
>
> The runtime may need to hash the address or traverse a lookup table to
> find out which allocation pool the block resides in. Now, that’s only if
> some platform ditches full malloc compatibility for user allocations, so
> I’m not sure how realistic it is.
>
>
> It seems to me that you could still provide malloc/free compatibility with
> a zone that had to do a relatively expensive traversal on free() to recover
> the pool the memory came from; malloc/free just wouldn't be the ideal
> interface in that situation.
>
> -Joe
>
>
> Joe is right, and I just learned how amazing malloc zones are.
>
>
> As long as you support multiple allocators (or hide everything behind
> malloc/free), there's already a cost of malloc_zone_from_ptr or equivalent.
> Without seeing a concrete use case, I wouldn't want to stay with the
> harder-to-use API *in UnsafePointer itself.* It might be a feature of a
> *particular* allocator that you need to keep the capacity around, but it
> *isn't* something generally true about Swift's memory model, and probably
> never will be.
>
> (Interesting reference points: malloc/malloc.h and the implementation of
> malloc on macOS
> <https://opensource.apple.com/source/libmalloc/libmalloc-116.50.8/src/malloc.c.auto.html> -
> search for "free(void *ptr)".)
>
> Jordan
>
>
> I’m primarily arguing from the point of view that UnsafeBufferPointer
> should pass it’s deallocation capacity and should be implementable in terms
> of UnsafePointer. But I’m fine hiding the capacity argument from the public
> API for now. We know what the proposal author wants to do, so unless Joe
> still feels strongly, we could accept the proposal as-is, put the API
> decision to rest and focus on better documentation and and assertions.
>
> -Andy
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170908/8456a968/attachment.html>


More information about the swift-evolution mailing list