{"id":1245,"date":"2012-10-01T14:26:34","date_gmt":"2012-10-01T02:26:34","guid":{"rendered":"https:\/\/www.deltics.co.nz\/blog\/?p=1245"},"modified":"2012-10-01T14:33:35","modified_gmt":"2012-10-01T02:33:35","slug":"lazy-or-efficient","status":"publish","type":"post","link":"https:\/\/www.deltics.co.nz\/blog\/posts\/1245\/","title":{"rendered":"Lazy or Efficient ?"},"content":{"rendered":"<span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\">3<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span><p>In <a href=\"http:\/\/delphihaven.wordpress.com\/2012\/09\/30\/potential-xe3-gotcha-dodgy-old-code-vs-new-tstream-overloads\/\" target=\"_blank\">his most recent post<\/a>, Chris Rolliston points out a gotcha resulting from a change in the VCL with XE3.  The post itself is quite interesting and makes some valid and significant observations.  However, in the follow up comments Chris makes the observation that the old code was lazy, relied on a compiler detail and should have been &#8220;properly&#8221; re-written, rather than replacing reliance on one compiler detail with another.<\/p>\n<p>I think it is worth highlighting that Chris&#8217; observations here are &#8211; I believe &#8211; directed at the use of untyped pointers and dynamic arrays, not the use of the pointer exposed by the <strong>TList<\/strong> class itself, as the two things could easily be conflated when reading Chris&#8217; post and the QC issue it refers to.<\/p>\n<p><!--more--><\/p>\n<p>The fact that <strong>TList<\/strong> is implemented as a pointer to an array of pointers ceased to be a private implementation detail the day that that internal pointer (strongly typed I might add) was exposed through the public interface of the <strong>TList<\/strong> class.<\/p>\n<p>At that point it became a contract &#8211; for good or bad.<\/p>\n<p>It was certainly never a <em>compiler<\/em> implementation detail.<\/p>\n<p>This was what crystallised for me that Chris was talking about the <em>overload selection made by the compiler<\/em>, and thus identified the comparison of untyped pointers and dynamic arrays as the subject of the comment, rather than the use of the list pointer in the code per se.<\/p>\n<p>Otherwise I think it goes without saying that writing code that takes advantage of a contracted interface in the most optimal fashion &#8211; especially (but not necessarily only) when runtime efficiency is required &#8211; imho is not &#8220;lazy&#8221;, it&#8217;s efficient.<\/p>\n<p>Indeed, I have taken advantage of this myself in the past to implement a super-fast mechanism for &#8220;cloning&#8221; <strong>TList<\/strong> instances by directly allocating the required memory in the destination list (setting the <strong>Count<\/strong> property) and then directly copying <strong>all<\/strong> list items in a single <strong>CopyMemory<\/strong> operation from one list to the other gratefully using their exposed <strong>List<\/strong> pointers.<\/p>\n<p>Even without going to the extra lengths of trying to suppressing the zeroing of the new items triggered by setting <strong>Count<\/strong> directly &#8211; which would have necessitated some <em>real<\/em> dirty tricks &#8211; this is <strong>vastly<\/strong> more efficient than enumerating the items in one list and adding them to the other.<\/p>\n<p>But I digress&#8230;<\/p>\n<p>To draw a comparison from the physical world &#8230; ask yourself which is the more lazy:<\/p>\n<ul>\n<li>Getting in your car and driving 3 km on public highways to get to a shop<\/li>\n<li>Using a footpath &#8211; less obvious to a habitual car driver and perhaps less well known generally &#8211; that takes you directly to the same shop after just a 300 m walk<\/li>\n<\/ul>\n<p>Is it &#8220;lazy&#8221; to take the quicker, but physically more demanding, option of walking via the footpath ?<\/p>\n<p>I would say it is more lazy to get in the car and drive the more obvious but less efficient route.<\/p>\n<p>And if you use your car on the basis that the council might close the footpath and leave you stranded at the shop with no way to get home, then I would say you are not just trying to justify your own laziness but are paranoid to boot!  \ud83d\ude42<\/p>\n<p>The key here is that the footpath is official, is on the map and does not involve hacking your way with a machette through thick bush obscuring the route across private property.<\/p>\n<p>It may be poorly lit and make you nervous in the dark, but carrying a flashlight can help in that situation (comment your code!).  \ud83d\ude42<\/p>\n<p>On the other hand &#8230;.<\/p>\n<p>Adding overloads to an existing class with thousands if not millions of lines of existing, dependent code without &#8211; apparently &#8211; taking the time to consider the impact of those overloads on that existing code, <em>that<\/em> is not just lazy but downright inconsiderate!<\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\">3<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span> In his most recent post, Chris Rolliston points out a gotcha resulting from a change in the VCL with XE3. The post itself is quite interesting and makes some valid and significant observations. However, in the follow up comments Chris makes the observation that the old code was lazy, relied on a compiler detail and [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[4,163],"tags":[292,188,164],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1TKYv-k5","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1068,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1068\/","url_meta":{"origin":1245,"position":0},"title":"Info From the World Tour (Hamburg)","date":"22 Aug 2012","format":false,"excerpt":"In the Embarcadero forums, Roland Kossow posted his report on the first of the \"RAD Studio World Tour\" events in Hamburg yesterday, reporting on what's new in XE3 and adding some more detail to the \"XE3 And Beyond\" blog post. In a nutshell we have \"FM2\" (FireMonkey 2 - no\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1097,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1097\/","url_meta":{"origin":1245,"position":1},"title":"EULA Change:  No Client\/Server in XE3 Pro. Not even 3rd Party.","date":"28 Aug 2012","format":false,"excerpt":"I don't enjoy posting this news. I really don't. I had hoped that my next post would be a more positive report following the Auckland leg of the XE3 World Tour this morning (and that will come later today). But I feel this news is so important that it has\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":338,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/338\/","url_meta":{"origin":1245,"position":2},"title":"Delphi 2009 &#8211; A Heads-Up for Low-Level Coders","date":"13 Sep 2008","format":false,"excerpt":"Prompted by a conversation with some colleagues where-in we collectively speculated about the implementation details of a generic class and what impact - if any - this might have on performance vs a \"traditional\" polymorphic equivalent, I threw together a quick performance test case in my Smoketest framework, and as\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1080,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1080\/","url_meta":{"origin":1245,"position":3},"title":"Why Delphi Cannot (currently) Support WinRT","date":"23 Aug 2012","format":false,"excerpt":"Nor for that matter can any other unmanaged compiler except Microsoft's own C\/C++, apparently. Who would have thought that in a forum reply in a thread about and entitled \"HTML Builder\" you would stumble across crucial, technical information relating to the paucity of WinRT support in Delphi ? But it's\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1138,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1138\/","url_meta":{"origin":1245,"position":4},"title":"Widening of Upgrade Eligibility in XE3&#8230;?","date":"03 Sep 2012","format":false,"excerpt":"Having checked the online store listing for Delphi XE3 which is now up, it appears that the upgrade eligibility window for XE3 may have been nudged open ever-so-slightly wider. The eligibility window introduced by Embarcadero has previously, consistently, been current version and the two immediately prior versions. Additionally they generously\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1166,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1166\/","url_meta":{"origin":1245,"position":5},"title":"XE3 Upgrade from Delphi 2009 &#8211; Only Until the End of the Year","date":"05 Sep 2012","format":false,"excerpt":"I previously speculated that the upgrade eligibility window for XE3 might have been edged open ever so slightly. Sadly it seems this is not the case (German language forum). At least, not unless they extend another upgrade offer as they did with XE2. Here's the response, as translated by Google:\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1245"}],"collection":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/comments?post=1245"}],"version-history":[{"count":11,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1245\/revisions"}],"predecessor-version":[{"id":1255,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1245\/revisions\/1255"}],"wp:attachment":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/media?parent=1245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/categories?post=1245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/tags?post=1245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}