<?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/"
	>

<channel>
	<title>DelphiTools.info &#187; Stack</title>
	<atom:link href="http://delphitools.info/tag/stack/feed/" rel="self" type="application/rss+xml" />
	<link>http://delphitools.info</link>
	<description>SamplingProfiler, DWS and other Delphi tools</description>
	<lastBuildDate>Thu, 02 Feb 2012 11:33:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>begin&#8230;end as bottlenecks?</title>
		<link>http://delphitools.info/2009/03/25/beginend-as-bottlenecks/</link>
		<comments>http://delphitools.info/2009/03/25/beginend-as-bottlenecks/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 12:02:49 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[Bottleneck]]></category>
		<category><![CDATA[Breakpoint]]></category>
		<category><![CDATA[CPU]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[IDE]]></category>
		<category><![CDATA[Profiler]]></category>
		<category><![CDATA[Stack]]></category>

		<guid isPermaLink="false">http://delphitools.info/?p=68</guid>
		<description><![CDATA[There will come a time when SamplingProfiler may report you that begin or end are your bottlenecks. This may sound a little surprising, but it&#8217;s actually quite a common occurrence, and something that instrumenting profilers are not going to point out, so it might be worth a little explanation. This can be illustrated it with [...]]]></description>
			<content:encoded><![CDATA[<p>There will come a time when SamplingProfiler may report you that <em>begin</em> or <em>end</em> are your bottlenecks. This may sound a little surprising, but it&#8217;s actually quite a common occurrence, and something that instrumenting profilers are not going to point out, so it might be worth a little explanation.</p>
<p>This can be illustrated it with the minimalistic example of an array property getter. Witness the innocuous looking code below:</p>
<pre style="padding-left: 30px">function TMyList.GetItem(index : Integer) : T;
begin
    if (index &lt; 0) or (index &gt;= Count) then
       Error(index);
    Result := FItems[index];
 end;</pre>
<p style="text-align: left">Nothing out of the ordinary there, you can find similar looking code in practically every array-based collection in the RTL and many third party libraries. But someday, that <em>GetItem</em> will be bottleneck, and you could be left looking at code profiling results like those:</p>
<p style="text-align: center;"><img class="size-full wp-image-85 aligncenter" title="begin-end-critical-01" src="http://delphitools.info/wp-content/uploads/2009/03/begin-end-critical-01.png" alt="begin-end-critical-01" width="567" height="213" /></p>
<p style="text-align: left">Yes, those are the are the <em>begin</em> and <em>end</em> lines taking up more than 70% of the CPU time spent inside <em>GetItem</em>&#8230;<br />
 You knew it! Sampling profilers are unreliable&#8230; or are they? Surely the index range checking must be the culprit? or the assignment and the reference counting business? Well, they could be, but in this case they aren&#8217;t.</p>
<p style="text-align: left">To understand why, let&#8217;s have a look in the CPU view. Place a breakpoint on your <em>begin</em>, run up to there and hit <em>Ctr+Alt+C</em>, here is what you could see:</p>
<p style="text-align: center;"><img class="size-full wp-image-86 aligncenter" title="begin-end-critical-02" src="http://delphitools.info/wp-content/uploads/2009/03/begin-end-critical-02.png" alt="begin-end-critical-02" width="429" height="354" /></p>
<p style="text-align: left">That&#8217;s a whole lot of traffic to the stack: 3 registers saved, 3 copies. Those things aren&#8217;t free, they can dwarf what your explicit code does, and in this example, they do. We didn&#8217;t even have any local variables, if we did, they would have taken setup and teardown code, and this code would have been &#8220;hidden&#8221; in <em>begin</em> and <em>end</em> too.</p>
<p style="text-align: left">This illustrates a difference of sampling vs instrumenting profilers: the ability to pinpoint an actual bottleneck, even if it is &#8220;outside&#8221; of your explicit code, so you can find where the actual bottleneck is, and don&#8217;t waste time trying to optimize what isn&#8217;t critical.</p>
<p style="text-align: left">Now what can you do to improve things locally? With generics, an interface type and Delphi 2009 sp2, nothing much, short of going BASM. The bottleneck code is compiler-generated, optimizing the assignment or the range checking would only provide minimal benefits. If you want to go faster, you&#8217;ll have to reduce the number of calls to <em>GetItem</em>, ie. open that <em>&#8220;Show Callers&#8221;</em> pane, have a look there, and solve the issue at the higher-level routines that are involved.</p>
<p style="text-align: left">But there are other situations in which you can influence the auto-generated <em>begin</em>/<em>end</em> code, the solutions then typically revolve around distributing the code across smaller local functions or methods, tweaking your variable usage, separating branches, or if all else fails, going BASM&#8230; but that is food for future posts!</p>
]]></content:encoded>
			<wfw:commentRss>http://delphitools.info/2009/03/25/beginend-as-bottlenecks/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  www.delphitools.info/tag/stack/feed/ ) in 0.82800 seconds, on Feb 4th, 2012 at 1:28 pm UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 5th, 2012 at 1:28 pm UTC -->
