Removing swift_deallocClassInstance from trace - swift

I have been attempting to optimize some Swift code and am seeing this trace:
308.00 ms 10.2% 25.00 ms swift_deallocClassInstance
I have no idea what is causing it and trying to figure out what would be dealloc-ing in the method. Has anyone seen this come up in profile or know what is causing it. It is showing up in this method. I've looked through for any sort class type vars but can't seem to pin it down. One of the methods is using some static struct OptionSet but those I wouldn't think would cause it. All other parameters passed are Ints
It is over 50% of the actual method and 10% of the overall trace:
final func generateTextures(options: TextureOptions, baseLevel: Int) -> [AreaData] {
otherMethodCall();
methodCall()
for i in 0..<size {
...
}
for i in 0..<width {
switch someVar {
...
default:
switch someVar {
...
}
}
}
for i in stride(from: width - 1, to: size, by: width) {
switch someVar {
....
default:
switch someVar{
...
}
}
}
// post processing
for i in 0..<size {
... method
}
for i in 0..<size {
..some more stuff
}
for i in 0..<size {
..some more stuff
}
anotherMethod()
return someMethod()
}

I removed all of the option set use in the method and went directly to raw Int's and this huge hit went away. I had hoped they compiled down option sets to int or the raw value operations directly, I guess not.

Related

"Result of '&&' unused" warning in a do/try/catch block

I've been trying to figure out what is going on to no avail. I've distilled the code as much as possible but I still get the "Result of operator && is unused warning (even though it is used) if I do it in a project but the same code copied to Playground is working fine with no warnings. This is just some dummy code, after me rewriting the basic code again while trying to find the problem.
enum WordError: Error {
case tooShort
case tooLong
}
func isTooShort(_ word: String) throws -> Bool {
if word.count < 3 { throw WordError.tooShort }
return true }
func isTooLong(_ word: String) throws -> Bool {
if word.count > 5 { throw WordError.tooLong }
return true }
func check(_ word: String) {
do {
try isTooShort(word) && isTooLong(word)
print(word)
} catch let error as WordError {
print("\(error)")
} catch {
}
}
Is this just a bug or am I doing something wrong here?
I figured I can silence the warning if I use:
try _ = isTooShort(word) && isTooLong(word)
But I'm not sure whether that's the right way of 'patching' it.
There is nothing wrong with doing it that way. The "right" way, when something like isTooShort belongs to you and you want to call it without capturing the result, is to mark it with #discardableResult. If you did that, then you could write
do {
try isTooShort(word)
try isTooLong(word)
print(word) // if we get here, it's neither too short nor too long
} catch ...
But what you're doing is also "right" in these circumstances.
The real question is why you would both return a Bool and throw an error. Your implementation of isTooShort is very odd. You seem to be misusing throw. It isn't clear what problem you are trying to solve by implementing it in this odd way. isTooShort can only fail one way: the word is too short. So why doesn't it just return a Bool? isTooShort asks a simple yes/no question, so just answer it: return a Bool and stop.
If your goal is to answer a three-way question - i.e., to tell the caller whether this word was too short, too long, or just right, then again, just return a custom enum that answers the question:
enum WordLength {
case tooShort
case tooLong
case justRight
}
func howIs(_ word: String) -> WordLength {
if word.count < 3 { return .tooShort }
if word.count > 5 { return .tooLong }
return .justRight
}

How to Pause a Background Thread in Swift

In my Swift code I have a process that's running on a secondary thread. In it there is a recursive method that I would like to pause every once in a while for a second or less. I was hoping I could just say sleep(0.5) but of course that would be too easy. BTW, anyone wanting to tell me that pausing is bad needn't bother. I'm aware of the various issues.
The process is kicked off with DispatchQueue.global(qos: .utility).async { }
It makes a callback for some UI calls which are done in a DispatchQueue.main.async { }
Does anyone have a nice little way to suspend and resume a thread at a specific point in the code like:
func myMethod() {
for n in 1...100 {
do some stuff
callback(parameters)
based on some logic possibly Thread.pause
recursively call myMethod()
}
}
My recursion works just fine but I just want to slow it down occasionally. I assume with some refactoring I could achieve the same effect but it would be so simple to just insert a quick little inline pause.
I would like to credit Rob with actually answering my question by saying to use Thread.sleep. I tried that but it didn't seem to work at first. However, as I told him, I kept trying and eventually I got it to work. Not sure exactly which "comma" or "period" actually did the trick because I tried so many variations. Bottom line is that I used usleep to get a finer resolution on the pause since sleep has to be integer seconds and I wanted less than a second. Despite the warnings about using pauses in the first place or recursing from inside a for-loop, the following code works exactly as needed:
view controller
object.callback = { () -> () in
DispatchQueue.main.async {
self.otherView.setSomeUI()
}
}
DispatchQueue.global(qos: .background).async() {
if self.object.method() { print("done") }
else { print("error") }
}
object
private func method() -> Bool
{
if conditionA { return true } // done
if conditionB {
if method() { return true }
} else {
for n in 1...N {
if test() {
self.callback?()
usleep(x)
if self.method() {
return true
} else {
self.callback?()
}
}
}
}
return false
}

What does `outlined init with copy of protocol` mean?

I wrote very simple piece of code in Swift:
protocol MultiplyByTwoProtocol {
func multiply() -> Int
}
class MultiplyByTwoClass: MultiplyByTwoProtocol {
private let n: Int
init(n: Int) { self.n = n }
func multiply() -> Int { return 2 * n }
}
class DynamicDispatchSwift {
private let items: [MultiplyByTwoProtocol]
init(n: Int) {
self.items = Array<Int>.generate(size: n).map(MultiplyByTwoClass.init)
}
func run() {
items.forEach { input in
_ = input.multiply()
}
}
}
(btw generate method for Array just creates an array of random Ints)
Then, I run that code in Instruments and I got following results:
As you can see, almost half of the time takes entry called outlined init with copy of MultiplyByTwoProtocol. Does anyone know what it is?
Thanks for your help.
I just ran into outlined init with copy of ... as the top of a stack trace for crashes coming from an app built with release configuration. I found that turning off the compiler optimization settings would prevent the crash. Ultimately I also found an equivalent syntax for the code the stack trace pointed to, that the optimizer was having problems with, that did not cause crashes and was able to turn optimization back on.
In my case the line of code causing the issue was accessing a static constant inside a for loop. Assigning that same static constant to nothing just prior to the loop fixed the crash, as in this example:
let _ = Foo.fooConstant // This line fixed the crash
for index in 0..<values.count {
let someBar = Bar(foo: .fooConstant)
...
}

RxSwift: Nested Queries and ReplaySubject

I have to fetch three types of data (AType, BType, CType) using three separate API requests. The objects returned by the APIs are related by one-to-many:
1 AType object is parent of N BType objects
1 BType object is parent of P CType objects)
I'm using the following three functions to fetch each type:
func get_A_objects() -> Observable<AType> { /* code here */ }
func get_B_objects(a_parentid:Int) -> Observable<BType> { /* code here */}
func get_C_objects(b_parentid:Int) -> Observable<CType> { /* code here */}
and to avoid nested subscriptions, these three functions are chained using flatMap:
func getAll() -> Observable<CType> {
return self.get_A_objects()
.flatMap { (aa:AType) in return get_B_objects(aa.id) }
.flatMap { (bb:BType) in return get_C_objects(bb.id) }
}
func setup() {
self.getAll().subscribeNext { _ in
print ("One more item fetched")
}
}
The above code works fine, when there are M objects of AType, I could see the text "One more item fetched" printed MxNxP times.
I'd like to setup the getAll() function to deliver status updates throughout the chain using ReplaySubject<String>. My initial thought is to write something like:
func getAll() -> ReplaySubject<String> {
let msg = ReplaySubject<String>.createUnbounded()
self.get_A_objects().doOnNext { aobj in msg.onNext ("Fetching A \(aobj)") }
.flatMap { (aa:AType) in
return get_B_objects(aa.id).doOnNext { bobj in msg.onNext ("Fetching B \(bobj)") }
}
.flatMap { (bb:BType) in
return get_C_objects(bb.id).doOnNext { cobj in msg.onNext ("Fetching C \(cobj)") }
}
return msg
}
but this attempt failed, i.e., the following print() does not print anything.
getAll().subscribeNext {
print ($0)
}
How should I rewrite my logic?
Problem
It's because you're not retaining your Disposables, so they're being deallocated immediately, and thus do nothing.
In getAll, you create an Observable<AType> via get_A_objects(), yet it is not added to a DisposeBag. When it goes out of scope (at the end of the func), it will be deallocated. So { aobj in msg.onNext ("Fetching A \(aobj)") } will never happen (or at least isn't likely to, if it's async).
Also, you aren't retaining the ReplaySubject<String> returned from getAll().subscribeNext either. So for the same reason, this would also be a deal-breaker.
Solution
Since you want two Observables: one for the actual final results (Observable<CType>), and one for the progress status (ReplaySubject<String>), you should return both from your getAll() function, so that both can be "owned", and their lifetime managed.
func getAll() -> (Observable<CType>, ReplaySubject<String>) {
let progress = ReplaySubject<String>.createUnbounded()
let results = self.get_A_objects()......
return (results, progress)
}
let (results, progress) = getAll()
progress
.subscribeNext {
print ($0)
}
.addDisposableTo(disposeBag)
results
.subscribeNext {
print ($0)
}
.addDisposableTo(disposeBag)
Some notes:
You shouldn't need to use createUnbounded, which could be dangerous if you aren't careful.
You probably don't really want to use ReplaySubject at all, since it would be a lie to say that you're "fetching" something later if someone subscribes after, and gets an old progress status message. Consider using PublishSubject.
If you follow the above recommendation, then you just need to make sure that you subscribe to progress before results to be sure that you don't miss any progress status messages, since the output won't be buffered anymore.
Also, just my opinion, but I would re-word "Fetching X Y" to something else, since you aren't "fetching", but you have already "fetched" it.

Fail to imply _Reflectable protocol

I am reading MirrorType at nshipster,and trying to adopt it's code to Swift 2.1. Everything works fine until when I tried to custom the _MirrorTypewith :
extension WWDCSession : _Reflectable {
func _getMirror() -> _MirrorType {
return WWDCSessionMirror(self)
}
}
An error occured :
error: Playground execution aborted: Execution was interrupted,
reason: EXC_BAD_ACCESS (code=2, address=0x7fff58273e87).
And I found out it's because the init method in WWDCSessionMirror was being called infinite times.
struct WWDCSessionMirror: _MirrorType {
private let _value: WWDCSession
init(_ value: WWDCSession) {
_value = value
}
var value: Any { return _value }
var valueType: Any.Type { return WWDCSession.self }
var objectIdentifier: ObjectIdentifier? { return nil }
var disposition: _MirrorDisposition { return .Struct }
var count: Int { return 4 }
subscript(index: Int) -> (String, _MirrorType) {
switch index {
case 0:
return ("number", _reflect(_value.number))
case 1:
return ("title", _reflect(_value.title))
case 2:
return ("track", _reflect(_value.track))
case 3:
return ("summary", _reflect(_value.summary))
default:
fatalError("Index out of range")
}
}
var summary: String {
return "WWDCSession \(_value.number) [\(_value.track.rawValue)]: \(_value.title)"
}
var quickLookObject: PlaygroundQuickLook? {
print(summary)
return .Text(summary)
}
}
I want to ask why it happened , and how to fix it?
_Reflectable and _MirrorType are not the droids you're looking for.
They are legacy types, which have been superseded by CustomReflectable (among others). The 2015 WWDC session about LLDB goes into some detail about this (disclaimer: I am the speaker of that part of that session, so conflict of interests all around :-)
But, anyway, the issue you're running into is because of this line:
_value = value
Since you're typing this line in your playground, that tells the playground logic to capture for display ("log" in playground parlance) the thing you're assigning. To do so, the playground uses the Mirror attached to that type. So, we go off and create one, which causes us to run
_value = value
again, which tells the playground logic to log value, which then means we create a Mirror, ...
You should first of all check if you can adopt Mirror and CustomReflectable instead of _MirrorType and if using those APIs fixes your problem. If it doesn't a possible workaround is to put the reflection support code in an auxiliary source file which will cause the playground logic to not log things inside of it.