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 <gerr...@mdenkmann.de> 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 (Cocoa-dev@lists.apple.com) > > 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: > https://lists.apple.com/mailman/options/cocoa-dev/cejwork%40gmail.com > > This email sent to cejw...@gmail.com -- Charles _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com