<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:media="http://search.yahoo.com/mrss/"
>

<channel>
	<title>Time Profile &#8211; Wade Tregaskis</title>
	<atom:link href="https://wadetregaskis.com/tags/time-profile/feed/" rel="self" type="application/rss+xml" />
	<link>https://wadetregaskis.com</link>
	<description></description>
	<lastBuildDate>Mon, 24 Jun 2024 23:39:21 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://wadetregaskis.com/wp-content/uploads/2016/03/Stitch-512x512-1-256x256.png</url>
	<title>Time Profile &#8211; Wade Tregaskis</title>
	<link>https://wadetregaskis.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">226351702</site>	<item>
		<title>When all you have is a Core Data, everything looks like…</title>
		<link>https://wadetregaskis.com/when-all-you-have-is-a-core-data-everything-looks-like/</link>
					<comments>https://wadetregaskis.com/when-all-you-have-is-a-core-data-everything-looks-like/#comments</comments>
		
		<dc:creator><![CDATA[]]></dc:creator>
		<pubDate>Mon, 24 Jun 2024 23:32:00 +0000</pubDate>
				<category><![CDATA[Ancient History]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Core Data]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[NSCoding]]></category>
		<category><![CDATA[Shark]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[SwiftData]]></category>
		<category><![CDATA[Time Profile]]></category>
		<category><![CDATA[XML]]></category>
		<guid isPermaLink="false">https://wadetregaskis.com/?p=8235</guid>

					<description><![CDATA[Reading SwiftData vs Realm: Performance Comparison reminded me of an anecdote from my days working on Shark, at Apple. I don&#8217;t really remember the timing &#8211; sometime between 2006 and 2010 &#8211; but presumably around 2006 as I recall it was when Core Data was still relatively new. For whatever reason, there was a huge&#8230; <a class="read-more-link" href="https://wadetregaskis.com/when-all-you-have-is-a-core-data-everything-looks-like/" data-wpel-link="internal">Read more</a>]]></description>
										<content:encoded><![CDATA[
<p>Reading <a href="https://www.emergetools.com/blog/posts/swiftdata-vs-realm-performance-comparison" data-wpel-link="external" target="_blank" rel="external noopener">SwiftData vs Realm: Performance Comparison</a> reminded me of an anecdote from my days working on <a href="https://leopard-adc.pepas.com/documentation/DeveloperTools/Conceptual/SharkUserGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40005233-CH1-DontLinkElementID_6" data-wpel-link="external" target="_blank" rel="external noopener">Shark</a>, at Apple.</p>



<p>I don&#8217;t really remember the timing &#8211; sometime between 2006 and 2010 &#8211; but presumably around 2006 as I recall it was when <a href="https://en.wikipedia.org/wiki/Core_Data" data-wpel-link="external" target="_blank" rel="external noopener">Core Data</a> was still relatively new.  For whatever reason, there was a huge push internal to Apple to use Core Data <em>everywhere</em>.  People were running around all over the place asking &#8220;can it be made to use Core Data?&#8221;, for Apple&#8217;s frameworks and applications.</p>



<p>Keep in mind that Core Data at that time was similar to <a href="https://developer.apple.com/documentation/swiftdata" data-wpel-link="external" target="_blank" rel="external noopener">SwiftData</a> now &#8211; very limited functionality, and <em>chock full</em> of bugs.  But of course it&#8217;s the nature of &#8216;shiny&#8217; new things that their proponents think it&#8217;s the second coming and the cure for all ills.</p>



<p>So, I recall sitting down with a couple of folks from the Core Data team, that were there to see if Shark could adopt Core Data.  A little like letting the missionaries in, if only out of morbid curiosity.</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="1200" height="675" src="https://wadetregaskis.com/wp-content/uploads/2024/06/orgazmo-mormon-missionaries-have-you-heard-the-good-news-about-core-data.avif" alt="Still from the scene in Orgazmo with the Mormon Missionaries greeting a homeowner at their door and asking &quot;Have you heard the good news about Core Data?&quot;." class="wp-image-8240" srcset="https://wadetregaskis.com/wp-content/uploads/2024/06/orgazmo-mormon-missionaries-have-you-heard-the-good-news-about-core-data.avif 1200w, https://wadetregaskis.com/wp-content/uploads/2024/06/orgazmo-mormon-missionaries-have-you-heard-the-good-news-about-core-data-256x144.avif 256w, https://wadetregaskis.com/wp-content/uploads/2024/06/orgazmo-mormon-missionaries-have-you-heard-the-good-news-about-core-data-1024x576.avif 1024w, https://wadetregaskis.com/wp-content/uploads/2024/06/orgazmo-mormon-missionaries-have-you-heard-the-good-news-about-core-data-768x432.avif 768w, https://wadetregaskis.com/wp-content/uploads/2024/06/orgazmo-mormon-missionaries-have-you-heard-the-good-news-about-core-data@2x.avif 2400w, https://wadetregaskis.com/wp-content/uploads/2024/06/orgazmo-mormon-missionaries-have-you-heard-the-good-news-about-core-data-256x144@2x.avif 512w" sizes="(max-width: 1200px) 100vw, 1200px" /></figure>



<p>Have you heard the good news?  Core Data is here to save your very data.  It&#8217;s effortless and divine and its unintuitive, thread-unsafe API will definitely not be the bane of all its users for the next fifteen years.</p>



<p>Jokes aside, they were in fact earnestly curious if Shark could use Core Data, instead of its own purpose-built binary formats, for storing &amp; querying its profiling data.  It was perhaps the classic case of naively underestimating the complexity of a foreign domain.  By my recollection, they assumed our profiling data was just a small handful of homogenous, relatively trivial records.  &#8220;At second N, the program ran the function named XYZ&#8221; or somesuch.</p>



<p>I think we (Shark engineers) tried to be open-minded and kind.  We were sceptical, but you never know until you actually look.  We could see some potential for a more general query capability, for example.  But of course the first and most obvious hurdle was: how well does Core Data handle sizeable numbers of records?  Oh yes, was the response, it&#8217;s great even with tens of thousands of records.</p>



<figure class="wp-block-image size-full"><img decoding="async" width="1128" height="480" src="https://wadetregaskis.com/wp-content/uploads/2024/06/star-trek-iv-the-voyage-home-is-that-a-lot.avif" alt="Still image of the pawn shop scene from Star Trek IV (The Voyage Home) showing Kirk &amp; Spock responding to the offer of $100 for the antique spectacles with &quot;Is that a lot?&quot;." class="wp-image-8236" srcset="https://wadetregaskis.com/wp-content/uploads/2024/06/star-trek-iv-the-voyage-home-is-that-a-lot.avif 1128w, https://wadetregaskis.com/wp-content/uploads/2024/06/star-trek-iv-the-voyage-home-is-that-a-lot-256x109.avif 256w, https://wadetregaskis.com/wp-content/uploads/2024/06/star-trek-iv-the-voyage-home-is-that-a-lot-1024x436.avif 1024w, https://wadetregaskis.com/wp-content/uploads/2024/06/star-trek-iv-the-voyage-home-is-that-a-lot-768x327.avif 768w, https://wadetregaskis.com/wp-content/uploads/2024/06/star-trek-iv-the-voyage-home-is-that-a-lot@2x.avif 2256w, https://wadetregaskis.com/wp-content/uploads/2024/06/star-trek-iv-the-voyage-home-is-that-a-lot-256x109@2x.avif 512w" sizes="(max-width: 1128px) 100vw, 1128px" /></figure>



<p>We asked how it did with tens of <em>millions</em> of records, and that was pretty much the end of the conversation.</p>



<details class="wp-block-details is-layout-flow wp-block-details-is-layout-flow"><summary>Background on the Time Profile data structure</summary>
<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<p>For context, the data in a Shark Time Profile (for example) was basically an array of samples, where each sample records the process &amp; thread IDs, and the callstack (expressed as an array of pointer-sized values; the first being the current PC and the rest the return addresses found by walking back up the thread&#8217;s stack).</p>



<p>Callstacks back then were relatively small, by modern standards &#8211; this was predominately C/C++/Objective-C code which tended to be far simpler in its structure than e.g. Swift; <em>way</em> fewer closures (blocks), no async suspension points to split logical functions up into numerous implementation functions, etc.  So the average was probably something in the low tens.  A hundred frames was considered a <em>big</em> callstack (which is sadly funny in hindsight, given that&#8217;s trivial by e.g. SwiftUI&#8217;s standards 😒).</p>



<p>A useful profile had at least thousands of such samples, and typical profiles were in the tens to hundreds of thousands (the latter usually for All Threads States profiles, particularly those of the whole system).  Some profiles could run into the millions or tens of millions (it&#8217;s not always easy or predictable as to when a performance problem will exhibit itself, so recording sometimes had to start early and run long).</p>



<p>I&#8217;m pretty sure Shark used <code><a href="https://developer.apple.com/documentation/foundation/nscoding" data-wpel-link="external" target="_blank" rel="external noopener">NSCoding</a></code> for the overall serdes, but a lot of that serdes was of huge chunks of (as far as <code>NSCoding</code> was concerned) arbitrary bytes. The file format was overall fairly efficient (though I don&#8217;t recall it ever using explicit data compression, nor even delta encoding for callstacks).</p>
</div></div>
</details>



<p>It wasn&#8217;t just the volume of data, it was also the dramatic difference in representation efficiency. The in-memory representation in Shark was basically as efficient as it could be &#8211; basically just arrays of compact structs, sometimes with pointers to other arrays (which might share a <code>malloc</code> block to avoid the overhead of small allocations) which were usually just of <code>uint32_t</code> or <code>uint64_t</code>. The most important operations &#8211; indexing to an arbitrary point in the profile&#8217;s timeline, then scanning forward over the data &#8211; were about as fast as they can possibly be.</p>



<p>In contrast, Core Data would have required an entire <em>object</em> (<code><a href="https://developer.apple.com/documentation/coredata/nsmanagedobject" data-wpel-link="external" target="_blank" rel="external noopener">NSManagedObject</a></code> subclass) for at least every sample, if not every <code>uintXX_t</code> in the callstack (depending on how &#8216;pure&#8217; you wanted the design to be). It would have increased memory usage by at least an order of magnitude &#8211; and Shark already struggled with big profiles on the hardware of the day, which typically had just a couple of GiB of RAM. Even the most trivial operations &#8211; like reading the data in from disk and iterating it sequentially would have been <em>thousands</em> of times slower.</p>



<p>In defence of the Core Data folks in the meeting &#8211; and I don&#8217;t remember who specifically it was &#8211; they never tried to misrepresent or exaggerate what Core Data could do.  I seem to recall them being quite nice people.  But as soon as we started explaining the type and volume of data that we worked with, they clearly gave up on any kind of pitch.  Core Data was designed for <em>developer convenience</em>, not runtime efficiency or performance.</p>



<p>It&#8217;s never ceased to surprise and disappoint me how many folks try to arbitrarily apply generalised data storage systems &#8211; <em>particularly</em> SQLite and MySQL, or wrappers thereover.  Usually for the same reasons &#8211; perceived convenience to them, right now, not necessarily efficiency (nor the convenience of their successors).</p>



<p>I guess by modern standards SQLite is considered efficient and fast, but &#8211; hah &#8211; <em>back in my day</em> SQLite was what you used when you didn&#8217;t have time to write your own, <em>efficient and fast</em> persistent data management system.</p>



<p>See also JSON and its older sister XML. 😔</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wadetregaskis.com/when-all-you-have-is-a-core-data-everything-looks-like/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			<media:content url="https://wadetregaskis.com/wp-content/uploads/2024/06/orgazmo-mormon-missionaries-have-you-heard-the-good-news-about-core-data.avif" medium="image" />
<post-id xmlns="com-wordpress:feed-additions:1">8235</post-id>	</item>
	</channel>
</rss>
