buhe/langchain-swift

Thread Performance Checker

Closed this issue ยท 4 comments

I have a warning in macOS app. Xcode 15

Thread Performance Checker: Thread running at User-initiated quality-of-service class waiting on a lower QoS thread running at Default quality-of-service class. Investigate ways to avoid priority inversions
...

Probably related with https://developer.apple.com/documentation/xcode/diagnosing-performance-issues-early

Specifically, you're using the dispatch_group_wait function, which doesn't provide a way to avoid priority inversion; thus, your waiting thread is susceptible to inversion. It looks like that's what's happening here.

The term "priority inversion" refers to a situation where a higher priority thread has to wait for a lower priority thread. This is undesirable because we may want higher priority threads to execute faster. In the context of your error, it seems that a high-priority thread is waiting for a lower-priority thread, leading to a warning.

As for the dispatch_group_wait function, it is used to pause a thread until all tasks in a certain group are completed. This can cause an issue if the paused thread is of higher priority than the tasks it is waiting for, which is referred to as priority inversion. To avoid this, you might want to consider using other mechanisms that help prevent priority inversion.

buhe commented

Thanks.

buhe commented

@lukasz-szyszkowski Task (priority: .background) is needed instead of Task to wrap asynchronous code. The essential reason is that swift-nio initializes MultiThreadedEventLoopGroup.

Such as:

         Task(priority: .background) {
                                let llm = OpenAI()
                                var p = "There are 6 sports in total, which are " + tags.joined(separator: ",")
                                p += ". My progress is: "
                                for i in viewModel.tops {
                                    p += i.title
                                    p += "  to complete "
                                    p += String(i.step)
                                    p += " step,"
                                }
                                p += "What's next for the suggestion? Give me advice anyway. "
//                                print(p)
                                let result = await llm.generate(text: p)
                                if let result = result {
                                    recommend = result.llm_output!
                                }
                                
                            }

Thanks, it solves the problem.
I'm closing this Issue.