4 ms·
(For the iOS engineers reading along: please don't put network calls in +load, or __attribute__((constructor)), or a C++ static variable, or whatever other clev
by saagarjha 7y ago
(For the iOS engineers reading along: please don't put network calls in +load, or __attribute__((constructor)), or a C++ static variable, or whatever other clever way you think you can get code execution before main.)
- favorited 7y agoEven better: don't override +load or use static constructors!
- saagarjha 7y agoEh, I wouldn't go that far; they do have their uses. But for a SDK author, it pays to be excessively cautious when putting things on the application startup path. (Something which the Facebook is well aware of, as the dyld session is always full of their company's engineers, and the architecture of their app shows that put effort into meeting launch deadlines…)
- favorited 7y agoEvery bad thing has its uses. If an SDK author wants to be a responsible participant in the app's startup path, it will defer its own setup to the app. `__attribute__((constructor))` is most obviously a hack – like any great hack, it is useful enough to be implemented everywhere, but it will never be standardized because everyone acknowledges that it sucks. I used to use it! But it is extremely limited in its usefulness, and there are always better solutions to the problem.
- vvG94KbDUtRa 7y agoSpoken like someone who has never tried to optimize startup time. If network is your blocker, you need to do it as soon as possible
- saagarjha 7y agoPutting more code in a constructor is almost never advised. I would expect that whatever connection the Facebook SDK is making is not a blocker for application launch. (If you have more insight on why this code must unconditionally run this early, I would be glad to hear it.)
- jfkebwjsbx 7y agoRunning code before main has nothing to do with performance.
- unilynx 7y agoIt surely does, performance is more about just raw CPU. Prefetching data (even a DNS query) reduces the latency the user perceives. The web similarly added the various <link rel=preload, dns-prefetch> tags, so things can be connected and fetched before the JS/CSS code is ready
- munchbunny 7y agoThe gain from trying to eke out a millisecond by executing network or filesystem I/O code before main() is extremely marginal compared to the can of worms opened by doing that.
- kccqzy 7y agoNot just network calls, but also the file system, or basically anything nontrivial. C++ static variables can now be annotated with constinit to resolve issues like this: https://en.cppreference.com/w/cpp/language/constinit https://en.cppreference.com/w/cpp/language/constinit It basically asks the compiler to enforce that constructor calls can only do trivial things.
- rohansuri 7y ago(non C++ developer here) What is the +load being referred?
- zerocrates 7y agoIt's Objective-C being referred to here: the "+" prefix indicates a class method. Any class can implement +load and the runtime will call the method upon loading the class (note, this doesn't require using it at all). https://developer.apple.com/documentation/objectivec/nsobject/1418815-load?language=objc https://developer.apple.com/documentation/objectivec/nsobjec...
- red_admiral 7y agoFor the iOS developers reading along: please ban this behaviour in a future version?
- ynx 7y agoThis is akin to banning any application that calls 'malloc' and 'free'. To do ban static constructors they'd have to literally ban anything that links the C++ runtime, which is almost literally everything on your system. Not possible.
- red_admiral 7y agoNot 'ban static constructors', sorry if I wasn't clear. Ban network I/O before main() has started.