• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to avoid swift.retain/release
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to avoid swift.retain/release


  • Subject: Re: How to avoid swift.retain/release
  • From: Charles Jenkins <email@hidden>
  • Date: Fri, 09 Sep 2016 06:52:33 -0400

Gerriet, Take this with a huge grain of salt because I don’t know what’s
really going on here and whether the API will permit it, but the first
thing I would do is try to get the expensive operation outside any loop if
possible:

 override function doSomething()
        {
                let ty = self.dynamicType
                for index = 0 to hugeNumber step 3      //      pseudo code
                {
                        ty.setBitFieldAtIndex(index)
                }
        }

On Fri, Sep 9, 2016 at 2:54 AM, Gerriet M. Denkmann <email@hidden>
wrote:

> This code executes in a strange way: SubClass takes much longer than
> BaseClass.
>
> class BaseClass
> {
>         let hugeNumber: Int
>         var bitfield: UnsafeMutablePointer<UInt8>
>
>         init()
>         {
>                 hugeNumber = 1_000_000_000
>                 bitfield = calloc( hugeNumber, 1)
>         }
>
>         function doSomething()
>         {
>                 for index = 0 to hugeNumber step 2      //      pseudo code
>                 {
>                         self.dynamicType.setBitFieldAtIndex(index)
>                 }
>         }
>
>         class function  setBitFieldAtIndex( index: Int )
>         {
>                 bitfield[index] = 5
>         }
> }
>
> final SubClass: BaseClass
> {
>         override function doSomething()
>         {
>                 for index = 0 to hugeNumber step 3      //      pseudo code
>                 {
>                         self.dynamicType.setBitFieldAtIndex(index)
>                 }
>         }
> }
>
> One assumes that SubClass is faster: it should take about 2/3 the time of
> BaseClass. But this is NOT the case.
>
> Instruments Time Profiler shows (total time, % of time, self time, name of
> function):
>
> BaseClass       (does NOT override doSomething)
>         5743 ms  92.0%  644                     doSomething
>      Instruments shows only Assembly code
>                 5099 ms  81.7% 4644                     setBitFieldAtIndex
>
> SubClass        (overrides doSomething)
>         8407 ms 92.6% 186                       doSomething
>      Instruments can show Swift code
>                 2693 29.6% 2576
>  setBitFieldAtIndex              this about expected
>                 2746 29.1% 2643                         swift_retain
> (_swift::HeapObject)      BAD
>                 2643 29.1% 2643                         swift_release
> (_swift::HeapObject)      BAD
>
> I guess that in BaseClass doSomething gets inlined and the compiler knows
> that retain/release is not needed.
>
> So: How can I convince the compiler not to use retain/release in SubClass?
>
> Gerriet.
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden




--

Charles
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


References: 
 >How to avoid swift.retain/release (From: "Gerriet M. Denkmann" <email@hidden>)

  • Prev by Date: Can't convert to swift 3 and support <iOS 10?
  • Next by Date: Re: Can't convert to swift 3 and support <iOS 10?
  • Previous by thread: How to avoid swift.retain/release
  • Next by thread: Can't convert to swift 3 and support <iOS 10?
  • Index(es):
    • Date
    • Thread