Thursday, September 2, 2021

Why Xcode Tools Are Slow After Reboot

Jeff Johnson:

When you attempt to run one of the developer tools, the _xcselect_invoke_xcrun function must look up the actual path of the tool. The paths of Xcode and the developer tools are cached on disk in a database file named xcrun_db located in your $TMPDIR. […]

Perhaps you already see the problem here: the contents of $TMPDIR are emptied on every reboot! Thus, the first time you run a developer tool after reboot, the xcrun_db cache needs to be regenerated.

[…]

Why does it take so long to regenerate the cache? While I was reproducing and diagnosing the issue, I noticed that when I ran a developer tool after reboot, the process syspolicyd went crazy and used almost 100% CPU until the command finished. I took samples of syspolicyd when this happened, and the process seemed to be spending a lot of time in the security framework checking code signing.

[…]

I discovered a way to accelerate the cache regeneration to around 3 seconds, down from over 10 seconds: disable System Integrity Protection.

Note that this also affects non-Apple tools like Git that are bundled inside of Xcode. The last few macOS releases, and especially since Catalina, it seems like my Mac is always pausing at inopportune times to verify security stuff. Often times the delay is much longer than the 10 seconds reported here, and sometimes one CPU core is pegged at 100% doing syspolicyd or tccd for the duration of what I’m doing. Disabling System Integrity Protection isn’t a great solution because it messes up testing.

Update (2021-09-07): Robin Kunde:

I wonder if this has implications for CI services like Circle CI that spin up a fresh VM every time you run a test suite. By restoring the database from cache, one might be able to work around this issue.

Comments RSS · Twitter

Leave a Comment