<?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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Kenny Kerr</title>
	<atom:link href="http://kennykerr.ca/feed/" rel="self" type="application/rss+xml" />
	<link>http://kennykerr.ca</link>
	<description></description>
	<lastBuildDate>Thu, 16 May 2013 13:05:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='kennykerr.ca' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Kenny Kerr</title>
		<link>http://kennykerr.ca</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://kennykerr.ca/osd.xml" title="Kenny Kerr" />
	<atom:link rel='hub' href='http://kennykerr.ca/?pushpress=hub'/>
		<item>
		<title>The Essentials of COM – Now on Pluralsight!</title>
		<link>http://kennykerr.ca/2013/05/14/essentials-of-com/</link>
		<comments>http://kennykerr.ca/2013/05/14/essentials-of-com/#comments</comments>
		<pubDate>Tue, 14 May 2013 23:24:00 +0000</pubDate>
		<dc:creator>Kenny Kerr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kennykerr.ca/?p=366</guid>
		<description><![CDATA[My latest Pluralsight course is now available: The Essentials of COM The short version: Learn the essentials of Microsoft’s Component Object Model. Understand its history and design, its relationship to classic C++, and how to write COM code effectively with modern C++. Need more convincing? This is the first in a series of courses exploring [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=366&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>My latest Pluralsight course is now available: <a href="http://pluralsight.com/training/Courses/TableOfContents/com-essentials"><strong>The Essentials of COM</strong></a></p>
<p>The short version: Learn the essentials of Microsoft’s Component Object Model. Understand its history and design, its relationship to classic C++, and how to write COM code effectively with modern C++.</p>
<p>Need more convincing?</p>
<p>This is the first in a series of courses exploring the essentials of COM and the Windows Runtime. In this first course, you are going to learn everything you need to know to use COM confidently and effectively. This course explores the history of COM. You will learn about how C++ shaped the design of COM. You will learn the essentials of compilation and linking, fundamental to the idea of dynamically linking applications and components together. By the end of this course you will have a thorough understanding of COM’s IUnknown interface, how and why it works the way it does, how to implement it, how to use it, and how to wrap it up in a safe and efficient manner with modern C++ libraries.</p>
<p>Here’s a brief description of the modules in <a href="http://pluralsight.com/training/Courses/TableOfContents/com-essentials">The Essentials of COM</a>.</p>
<p><strong>Microsoft&#8217;s Component Object Model:</strong> the essence of COM, the core concepts, where it came from, and the key players.</p>
<p><strong>COM and C++:</strong> COM as a better C++98, dynamic linking, exporting objects, managing lifetime, and extending objects.</p>
<p><strong>IUnknown and modern C++:</strong> HRESULTs, GUIDs, implementing IUnknown, IUnknown and C#, smart pointers, and C++11 as a better COM.</p>
<p><strong>Libraries:</strong> Windows Runtime C++ Template Library and the Active Template Library</p>
<p>Stay tuned for &#8220;The Essentials of WinRT&#8221; as well as part 2 of this course, covering activation, apartments, and security!</p>
<p>I realize that many developers are captivated by managed code, but if you’re one of the lucky ones that still use C++ then please help spread the word regarding <a href="http://www.pluralsight.com/training/Authors/Details/kenny-kerr">my Pluralsight courses</a>. I love producing these courses, but it’s only possible with your support. So go out there and tell everyone you know that Pluralsight is the place to go for <a href="http://www.pluralsight.com/training/Courses#cpp">hardcode C++ developer training</a>.</p>
<p>&nbsp;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kennykerrca.wordpress.com/366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kennykerrca.wordpress.com/366/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=366&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kennykerr.ca/2013/05/14/essentials-of-com/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/71d4efa35b3d11457c35543f9f55fba3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kennykerrca</media:title>
		</media:content>
	</item>
		<item>
		<title>Introducing Direct2D 1.1 (and DirectX 11.1)</title>
		<link>http://kennykerr.ca/2013/05/01/introducing-direct2d-1-1-and-directx-11-1/</link>
		<comments>http://kennykerr.ca/2013/05/01/introducing-direct2d-1-1-and-directx-11-1/#comments</comments>
		<pubDate>Wed, 01 May 2013 19:17:37 +0000</pubDate>
		<dc:creator>Kenny Kerr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kennykerr.ca/?p=358</guid>
		<description><![CDATA[My latest column for the May 2013 issue of MSDN Magazine is now available online. Direct2D 1.1 might sound like a minor version update, and in some ways, it is. It doesn’t fundamentally change the API. Everything you know about Direct2D continues to be every bit as relevant today. It’s still modeled around device-specific and [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=358&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>My latest column for the May 2013 issue of MSDN Magazine <a href="http://msdn.microsoft.com/en-us/magazine/dn198239.aspx">is now available online</a>.</p>
<blockquote><p>Direct2D 1.1 might sound like a minor version update, and in some ways, it is. It doesn’t fundamentally change the API. Everything you know about Direct2D continues to be every bit as relevant today. It’s still modeled around device-specific and device-independent resources, render targets, geometries, brushes and so on. But in version 1.1, Direct2D grows up. The original version of Direct2D that launched with Windows 7 was in some ways an outsider to DirectX. It lagged behind, being tied to DirectX 10 rather than 11, the version of DirectX that it launched with. Even though it provided an excellent interop story for GDI and the Windows Imaging Component, it didn’t provide the best possible experience for working with DirectX itself. It wasn’t bad, but in Direct2D 1.1 things get a whole lot better. Direct3D and Direct2D are now in many ways siblings in the DirectX family. Thanks to this greater parity, even more of the graphics processing unit (GPU) is now available to the Direct2D developer without the need to jump out of the 2D abstraction. Moreover, when you do need to make the leap, it’s both simple and efficient.</p></blockquote>
<p>Check out <a href="http://pluralsight.com/training/Courses/TableOfContents/direct2d-fundamentals-part2">my latest course on Pluralsight</a> for a crash course on DirectX 11.1 and Direct2D 1.1!</p>
<p>You can find links to <a href="http://kennykerr.ca/articles/">more of my articles here</a>.</p>
<a href='http://twitter.com/kennykerr' class='twitter-follow-button' data-show-count='false' data-text-color='#1c1c1c' data-link-color='#004276'>Follow @kennykerr</a>
<p>&nbsp;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kennykerrca.wordpress.com/358/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kennykerrca.wordpress.com/358/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=358&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kennykerr.ca/2013/05/01/introducing-direct2d-1-1-and-directx-11-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/71d4efa35b3d11457c35543f9f55fba3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kennykerrca</media:title>
		</media:content>
	</item>
		<item>
		<title>Rendering in a Desktop Application with Direct2D</title>
		<link>http://kennykerr.ca/2013/03/01/rendering-in-a-desktop-application-with-direct2d/</link>
		<comments>http://kennykerr.ca/2013/03/01/rendering-in-a-desktop-application-with-direct2d/#comments</comments>
		<pubDate>Fri, 01 Mar 2013 19:41:20 +0000</pubDate>
		<dc:creator>Kenny Kerr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kennykerr.ca/?p=353</guid>
		<description><![CDATA[My latest column for the March 2013 issue of MSDN Magazine is now available online. One of the key design underpinnings of Direct2D is that it focuses on rendering and leaves the other aspects of Windows application development to you or other libraries that you might employ. Although Direct2D was designed to render in a [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=353&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>My latest column for the March 2013 issue of MSDN Magazine <a href="http://msdn.microsoft.com/en-us/magazine/jj991972.aspx">is now available online</a>.</p>
<blockquote><p>One of the key design underpinnings of Direct2D is that it focuses on rendering and leaves the other aspects of Windows application development to you or other libraries that you might employ. Although Direct2D was designed to render in a desktop window, it’s up to you to actually provide this window and optimize it for Direct2D rendering. So this month, I’m going to focus on the unique relationship between Direct2D and the desktop application window. You can do many things to optimize the window handling and rendering process. You want to reduce unnecessary painting and avoid flicker, and just provide the best possible experience for the user.</p></blockquote>
<p>This is the last installment before I dive into Direct2D 1.1 and everything related to Windows 8.</p>
<p>You can find links to <a href="http://kennykerr.ca/articles/">more of my articles here</a>.</p>
<a href='http://twitter.com/kennykerr' class='twitter-follow-button' data-show-count='false' data-text-color='#1c1c1c' data-link-color='#004276'>Follow @kennykerr</a>
<p>&nbsp;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kennykerrca.wordpress.com/353/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kennykerrca.wordpress.com/353/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=353&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kennykerr.ca/2013/03/01/rendering-in-a-desktop-application-with-direct2d/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/71d4efa35b3d11457c35543f9f55fba3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kennykerrca</media:title>
		</media:content>
	</item>
		<item>
		<title>Direct2D Fundamentals &#8211; Part 2 &#8211; Now on Pluralsight!</title>
		<link>http://kennykerr.ca/2013/02/16/direct2d-fundamentals-part-2-now-on-pluralsight/</link>
		<comments>http://kennykerr.ca/2013/02/16/direct2d-fundamentals-part-2-now-on-pluralsight/#comments</comments>
		<pubDate>Sat, 16 Feb 2013 13:00:34 +0000</pubDate>
		<dc:creator>Kenny Kerr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://kennykerrca.wordpress.com/?p=345</guid>
		<description><![CDATA[My latest course is now available on Pluralsight! Direct2D Fundamentals – Part 2 is so much more than Direct2D. In many ways, it’s an intense crash course in DirectX programming. You’re going to learn about Direct3D, DXGI, devices, swap chains, but it’s more than that. This course is about developing stunning applications for Windows. You’re [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=345&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>My latest course is now available on Pluralsight!</p>
<p><a href="http://www.pluralsight.com/training/Courses/TableOfContents/direct2d-fundamentals-part2">Direct2D Fundamentals – Part 2</a> is so much more than Direct2D. In many ways, it’s an intense crash course in DirectX programming. You’re going to learn about Direct3D, DXGI, devices, swap chains, but it’s more than that.</p>
<p>This course is about developing stunning applications for Windows. You’re going to learn how to develop applications that can run cross-platform, reaching the Windows desktop, Windows Store, and Windows Phone environments. You’re going to learn about performance and efficiency. You’re going to learn how to produce high-quality animation. Moreover, you’re going to do it all in C++.</p>
<p>If you haven’t done so already, start by working through <a href="http://pluralsight.com/training/Courses/TableOfContents/direct2d-fundamentals">part 1 of Direct2D Fundamentals</a>. This will prepare you by laying the foundation for everything you will learn in this new course.</p>
<p>Here’s a brief description of the modules in <a href="http://pluralsight.com/training/Courses/TableOfContents/direct2d-fundamentals-part2">Direct2D Fundamentals – Part 2</a>.</p>
<p><strong>Introduction</strong><br />
What is Direct2D 1.1? Where can it be found? Introduction to the course demo.</p>
<p><strong>Common Foundation<br />
</strong>Building the foundation, error handling, and a common set of abstractions for a cross-platform application.</p>
<p><strong>Desktop Window<br />
</strong>The reference platform, optimizing window plumbing, and resource handlers.</p>
<p><strong>Direct3D<br />
</strong>Understanding and creating the underlying Direct3D device, hardware and software drivers, and the Direct2D device and device context.</p>
<p><strong>Swap Chain</strong><br />
What is DXGI? What is a swap chain? Creating and resizing a swap chain. Targeting, rendering, and presentation with Direct2D and DXGI.</p>
<p><strong>The Clock<br />
</strong>How to draw an analog clock with Direct2D primitives and transforms. Adding a background image with DPI awareness. Using image effects to add a shadow.</p>
<p><strong>Animation</strong><br />
Revisiting the message loop. Understanding swap chain occlusion and its impact on performance. Power management and efficiency. Using the Windows Animation Manager for scheduled animation.</p>
<p><strong>Windows Runtime<br />
</strong>Understanding the CoreWindow abstraction. Optimizing with visibility and the message loop. Using DirectX in the Windows Runtime.</p>
<p><strong>Windows Phone</strong><br />
Limitations in the Windows Phone SDK. Porting the clock application, and unleashing the phone’s awesome power with the restricted API.</p>
<a href='http://twitter.com/kennykerr' class='twitter-follow-button' data-show-count='false' data-text-color='#1c1c1c' data-link-color='#004276'>Follow @kennykerr</a>
<p>&nbsp;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kennykerrca.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kennykerrca.wordpress.com/345/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=345&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kennykerr.ca/2013/02/16/direct2d-fundamentals-part-2-now-on-pluralsight/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/71d4efa35b3d11457c35543f9f55fba3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kennykerrca</media:title>
		</media:content>
	</item>
		<item>
		<title>Creating Desktop Apps with Visual C++ 2012</title>
		<link>http://kennykerr.ca/2013/02/15/creating-desktop-apps-with-visual-c-2012/</link>
		<comments>http://kennykerr.ca/2013/02/15/creating-desktop-apps-with-visual-c-2012/#comments</comments>
		<pubDate>Fri, 15 Feb 2013 13:07:11 +0000</pubDate>
		<dc:creator>Kenny Kerr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://kennykerrca.wordpress.com/?p=343</guid>
		<description><![CDATA[It’s been a busy few weeks and I’m just now catching up with some announcements. My latest column for the February 2013 issue of MSDN Magazine is available online as well as in print. With all the hype over Windows 8 and what are now known as Windows Store apps, I’ve received some questions about [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=343&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>It’s been a busy few weeks and I’m just now catching up with some announcements. </p>
<p>My latest column for the February 2013 issue of MSDN Magazine <a href="http://msdn.microsoft.com/en-us/magazine/jj891018.aspx">is available online as well as in print</a>.</p>
<blockquote><p>With all the hype over Windows 8 and what are now known as Windows Store apps, I’ve received some questions about the relevance of desktop apps and whether Standard C++ is still a viable choice going forward. These questions are sometimes hard to answer, but what I can tell you is that the Visual C++ 2012 compiler is more committed than ever to Standard C++ and it remains the best toolchain, in my humble opinion, for building great desktop apps for Windows whether you’re targeting Windows 7, Windows 8 or even Windows XP.</p>
</blockquote>
<p>You can find links to <a href="http://kennykerr.ca/articles/">more of my articles here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kennykerrca.wordpress.com/343/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kennykerrca.wordpress.com/343/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=343&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kennykerr.ca/2013/02/15/creating-desktop-apps-with-visual-c-2012/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/71d4efa35b3d11457c35543f9f55fba3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kennykerrca</media:title>
		</media:content>
	</item>
		<item>
		<title>The API behind the API</title>
		<link>http://kennykerr.ca/2013/01/26/the-api-behind-the-api/</link>
		<comments>http://kennykerr.ca/2013/01/26/the-api-behind-the-api/#comments</comments>
		<pubDate>Sun, 27 Jan 2013 04:34:27 +0000</pubDate>
		<dc:creator>Kenny Kerr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://kennykerrca.wordpress.com/?p=339</guid>
		<description><![CDATA[Despite what anyone might tell you, the Windows Runtime API is not a clean break from the past. Like .NET before it, WinRT includes a backdoor without which it would be practically useless. The Common Language Runtime’s backdoor was called Platform Invocation Services or P/Invoke for short. It was amazingly powerful, but also complex and [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=339&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Despite what anyone might tell you, the Windows Runtime API is not a clean break from the past. Like .NET before it, WinRT includes a backdoor without which it would be practically useless. The Common Language Runtime’s backdoor was called Platform Invocation Services or P/Invoke for short. It was amazingly powerful, but also complex and troublesome. WinRT’s backdoor is a lot simpler. It’s called reinterpret_cast.</p>
<p>As I’ve already illustrated in <a href="http://kennykerr.ca/2011/10/18/the-road-to-windows-8/">The Road to Windows 8</a> and <a href="http://kennykerr.ca/2012/11/09/windows-8-whered-you-put-my-hwnd/">Windows 8, where’d you put my HWND?!</a>, WinRT is projected into C++ through a set of extensions that, among other things, allows the compiler to insert code to automatically manage reference counts as if a COM smart pointer class were used. For example, every WinRT application (or at least those that run within an app container) has a CoreWindow:</p>
<p><span style="font-family:Consolas;">auto w = CoreWindow::GetForCurrentThread();</span></p>
<p>If you were feeling verbose, you might write:</p>
<p><span style="font-family:Consolas;">CoreWindow ^ cw = CoreWindow::GetForCurrentThread();</span></p>
<p>The ^, pronounced &#8220;hat&#8221;, defines a handle-to-object, which the compiler treats as a pointer-to-object with intrusive reference counting provided by the object’s IUnknown interface and administered by the compiler.</p>
<p>Without WinRT’s backdoor however, no Windows Store or Windows Phone app would be able to function. In the case of CoreWindow, the application ultimately needs to bind the application’s swap chain or composition target to the HWND that the CoreWindow represents. Without reinterpret_cast, this would not be possible. I’m obviously speaking of reinterpret_cast in the proverbial sense. You don’t need to tell me that a C-style cast will do or that it could all be done without /ZW.</p>
<p>CoreWindow is not alone. While ICoreWindowInterop may not be documented, another WinRT type, namely SwapChainBackgroundPanel, openly flaunts its “other” API. It is after all, the only reason for this XAML type’s existence. The ISwapChainBackgroundPanelNative interface provides the only panel method that you really need to use.</p>
<p><span style="font-family:Consolas;">SwapChainBackgroundPanel ^ panel = &#8230;<br />
IUnknown * unknown = reinterpret_cast&lt;IUnknown *&gt;(panel);<br />
ComPtr&lt;ISwapChainBackgroundPanelNative&gt; native;<br />
HR(unknown-&gt;QueryInterface(native.GetAddressOf()));</span></p>
<p>There are other examples, but the point is that there is again an API behind the API.</p>
<p>As developers begin to use C++/CX more, I felt it would be useful to offer a littler helper to make access to this backdoor a little more convenient, and safe.</p>
<p><span style="font-family:Consolas;">template &lt;typename To&gt;<br />
ComPtr&lt;To&gt; winrt_cast(Object ^ from)<br />
{<br />
ComPtr&lt;To&gt; to;<br />
HR(reinterpret_cast&lt;IUnknown *&gt;(from)-&gt;QueryInterface(to.GetAddressOf()));<br />
return to;<br />
}</span></p>
<p>With the winrt_cast function template, you can quickly and easily reach in and retrieve the native or interop interface that a particular WinRT type might provide.</p>
<p><span style="font-family:Consolas;">auto native = winrt_cast&lt;ISwapChainBackgroundPanelNative&gt;(panel);</span></p>
<p><span style="font-family:Consolas;">auto interop = winrt_cast&lt;ICoreWindowInterop&gt;(window);</span></p>
<p>The winrt_cast function ensures that the resulting “hat-less” COM pointer is safely wrapped inside WRL’s excellent smart pointer. Since ComPtr is move-aware, returning it in this way is guaranteed not to introduce an unnecessary reference-counting heartbeat. Error handling is also not an issue. Although QueryInterface is traditionally analogous to dynamic_cast in the sense that it allows feature discovery at run-time, in this case the WinRT types are guaranteed to provide the particular interfaces. Without this capability, they would be useless. Still, it’s up to you to decide how best to deal with errors in your application or library. I tend to define HR as follows.</p>
<p><span style="font-family:Consolas;">#ifdef DEBUG<br />
#define HR(expression) ASSERT(S_OK == (expression))<br />
#else<br />
inline void HR(HRESULT hr) { if (S_OK != hr) throw Exception::CreateException(hr); }<br />
#endif</span></p>
<p>If I’ve done something wrong, I’m treated to an assertion during development. If something goes horribly wrong at run-time, my application is quickly torn down. Obviously, as with P/Invoke, this is not something to be used lightly or without thinking. Don’t go using winrt_cast on a String^, which holds an HSTRING rather than IUnknown pointer. You can for example disable this helper for strings as follows:</p>
<p><span style="font-family:Consolas;">template &lt;typename To&gt; ComPtr&lt;To&gt; winrt_cast(String ^);</span></p>
<p>Indeed, this solution is for those cases where you know a particular WinRT type exposes a particular COM interface outside of the discoverable type system. <a href="http://twitter.com/jamesmcnellis">James McNellis</a> offers a slightly more verbose solution, but one that is <a href="http://stackoverflow.com/questions/13708755/how-to-get-access-to-writeablebitmap-pixelbuffer-pixels-with-c/13715814#13715814">more widely applicable here</a>. He also discusses hats in a <a href="http://blogs.msdn.com/b/vcblog/archive/2012/09/17/cxxcxpart02typesthatwearhats.aspx">lot more detail here</a>.</p>
<p>Hope this helps.</p>
<a href='http://twitter.com/kennykerr' class='twitter-follow-button' data-show-count='false' data-text-color='#1c1c1c' data-link-color='#004276'>Follow @kennykerr</a>
<p>&nbsp;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kennykerrca.wordpress.com/339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kennykerrca.wordpress.com/339/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=339&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kennykerr.ca/2013/01/26/the-api-behind-the-api/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/71d4efa35b3d11457c35543f9f55fba3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kennykerrca</media:title>
		</media:content>
	</item>
		<item>
		<title>The Great C++ Wordament: Meet Windows</title>
		<link>http://kennykerr.ca/2013/01/21/the-great-c-wordament-meet-windows/</link>
		<comments>http://kennykerr.ca/2013/01/21/the-great-c-wordament-meet-windows/#comments</comments>
		<pubDate>Tue, 22 Jan 2013 04:02:28 +0000</pubDate>
		<dc:creator>Kenny Kerr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://kennykerrca.wordpress.com/?p=336</guid>
		<description><![CDATA[This article is a reaction to some code that appeared on the Visual C++ blog a few days ago. You might say that we, James McNellis and Kenny Kerr, are addicted to writing code—preferably, fast code. On Friday, Eric Battalio wrote about his adventures jumping back into C++. It involved a simple program to count [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=336&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This article is a reaction to some code that appeared on the Visual C++ blog a few days ago. You might say that we, <a href="https://twitter.com/JamesMcNellis">James McNellis</a> and <a href="http://twitter.com/kennykerr">Kenny Kerr</a>, are addicted to writing code—preferably, fast code.</p>
<p>On Friday, <a href="https://twitter.com/ebattalio">Eric Battalio</a> wrote about his adventures <a href="http://blogs.msdn.com/b/vcblog/archive/2013/01/18/jumping-into-c.aspx">jumping back into C++</a>. It involved a simple program to count the frequency of words contained within text files. In the comments, Stephan T. Lavavej contributed his own version that was both simpler and more effective at matching words. To be fair to Eric, Stephan is one of the foremost experts in the Standard Template Library.</p>
<p>Eric’s objective was to demonstrate some modern C++ techniques and to show how to complete some simple tasks using the C++ Standard Library.&#160; Stephan’s solution built on that by replacing the word parser with the new C++11 &lt;regex&gt; library.&#160; While writing the fastest possible implementation was not one of their objectives, it struck us that neither implementation was particularly fast.&#160; We didn’t need profilers or benchmarks to determine this—it was evident from looking at the code that both implementations left buckets of performance sitting on the table.&#160; We’ll use Stephan’s implementation as a reference implementation, the essence of which is as follows:</p>
<p><font face="Consolas">int main(int argc, char * argv[])      <br />{       <br />&#160;&#160;&#160; std::regex const word_regex(&quot;\\w+&quot;);       <br />&#160;&#160;&#160; std::map&lt;std::string, unsigned&gt; result;</font></p>
<p><font face="Consolas">&#160;&#160;&#160; for (int argi = 1; argi &lt; argc; ++argi)      <br />&#160;&#160;&#160; {       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; std::ifstream file(argv[argi]);</font></p>
<p><font face="Consolas">&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (!file)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; continue;       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</font></p>
<p><font face="Consolas">&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (std::string line; std::getline(file, line); )      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (std::sregex_token_iterator it(line.begin(), line.end(), word_regex),       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; end; it != end; ++it)       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ++result[*it];       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }       <br />&#160;&#160;&#160; }       <br />}</font></p>
<p>Performance analysis is an art and something that only comes with experience. It’s very tempting to assume you know what the performance bottlenecks are, but such assumptions are often wrong and one should always use a profiler to be sure. Still, looking at this code, a few things immediately jump out:</p>
<ul>
<li>As much as we love regular expressions, for a match as simple as <strong>\w+</strong> you will almost certainly achieve better performance with a trivial, hand-written implementation. </li>
<li>The Standard C++ I/O library—ios and friends—is a thing of beauty but it is not particularly fast. It is designed primarily for convenience and is most effective when handling console I/O rather than I/O in general. </li>
<li>In this implementation, the text of the file is scanned twice. First, it scans the file in the call to std::getline looking for the newline character. It then scans each line looking for words. The result is that each character in each file is visited twice. </li>
<li>Since each line is copied into a string and each word is then further copied into its own string, there is a great deal of copying and a whole lot of memory allocations. </li>
<li>Although the files obviously have no overlapping data they are scanned in sequence. The data set could easily be spread across available cores for greater throughput. </li>
</ul>
<p>More improvements could be made. The trick is to identify some of the low-hanging fruit and consider whether they will provide meaningful improvements. It’s time to break the rules.</p>
<p><strong>Mapped Files</strong></p>
<p>The first step is realizing that the files in question are relatively small. We struggled to find any novels on Project Gutenberg that amounted to more than 2MB of text and settled on a handful of Dickens novels.&#160; There’s no reason to stream such small files off the disk when you can simply map them into the address space of the process. This avoids repeated calls to the Windows API ReadFile function and the need to copy the data into user-mode buffers. This is in fact the way Windows loads executables and DLLs so presumably it’s going to be fast. File mapping objects are provided by the Windows Memory Manager, one of the most complex and efficient components of the operating system. Internally called section objects and mapped files in the Windows API, a file mapping object represents sections of an open file on disk. </p>
<p><font face="Consolas">wrl::FileHandle const file(CreateFile(name,      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; GENERIC_READ,       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; FILE_SHARE_READ,       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; nullptr, // default security       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; OPEN_EXISTING,       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; FILE_ATTRIBUTE_NORMAL,       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; nullptr)); // no template</font></p>
<p><font face="Consolas">if (!file.IsValid())      <br />{       <br />&#160;&#160;&#160; // Possibly doesn&#8217;t exist &#8211; check with GetLastError.       <br />}</font></p>
<p>In addition to the possibility of a nonexistent file, the file may in fact be empty. It’s important to check this, as the kernel won’t map an empty file. It’s also obviously useful to have the file size and we’ll need that in a moment.</p>
<p><font face="Consolas">LARGE_INTEGER size = { };</font></p>
<p><font face="Consolas">VERIFY(GetFileSizeEx(file.Get(),      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &amp;size));</font></p>
<p><font face="Consolas">if (!size.QuadPart)      <br />{       <br />&#160;&#160;&#160; // file is empty       <br />}</font></p>
<p>Given a handle to a file, the CreateFileMapping function creates a file mapping object and returns a handle to represent it. Such objects can actually refer to files that are much larger than what might fit into the address space of a process or even physical memory. Still, in this case it simpler to assume, and request through the Windows API, that the files will be mapped in their entirety. Given a handle to the file mapping object, the MapViewOfFile function actually maps it into the address space of the process. This function returns the address, a pointer, of the mapped view.</p>
<p><font face="Consolas">typedef wrl::HandleT&lt;wrl::HandleTraits::HANDLENullTraits&gt; MapHandle;</font></p>
<p><font face="Consolas">MapHandle const map(CreateFileMapping(file.Get(),      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; nullptr, // default security       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; PAGE_READONLY,       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 0, 0, // match file size       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; nullptr)); // no name</font></p>
<p><font face="Consolas">VERIFY(map.IsValid());</font></p>
<p><font face="Consolas">auto view = static_cast&lt;char const *&gt;(MapViewOfFile(map.Get(),      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; FILE_MAP_READ,       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 0, 0, // offset       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 0)); // match file size</font></p>
<p>Keep in mind that unlike CreateFile, the CreateFileMapping function does not return INVALID_HANDLE_VALUE on failure. Instead, it simply returns a nullptr value, thus the need for the alternative WRL traits class above. Of course, the primary reason that this function might fail is if the file is empty. It does not actually commit any memory so it is reasonably inexpensive and unlikely to fail if used correctly. </p>
<p>It helps to keep in mind that these Windows API functions create kernel objects that are reference counted. This is not unlike COM’s intrusive reference counting, at least conceptually. Unlike COM, the kernel keeps track of outstanding references held by each process and automatically releases them if the process fails to do so. When you call the CreateFile function, you effectively hold one reference to the file object. To release the reference you must call the CloseHandle function. The same goes for the CreateFileMapping function. The file mapping object however also holds a reference to the file object. You can thus release your reference to the file object and it will remain open as long as you still hold a reference to the file mapping object. It follows that the MapViewOfFile function works the same way. Holding onto just this reference will keep both the file and the file mapping object alive until you call the UnmapViewOfFile function. </p>
<p><font face="Consolas">if (view)      <br />{       <br />&#160;&#160;&#160; VERIFY(UnmapViewOfFile(view));       <br />}</font></p>
<p>In the complete program attached to this post, we use a simple class type to represent the mapped view of the file.&#160; This class serves as an RAII container that automatically unmaps the view when it goes out of scope and provides access to the view as a range of bytes, via begin() and end() member functions.</p>
<p><strong>Word Matching</strong></p>
<p>Given that the text files are now mapped into memory, the program can simply scan the entire contents as if it were one long string. We can thus avoid scanning each file twice since there is no need to break it up into lines that are contiguous in memory. The two for loops in the original example now becomes a single loop, scanning each file just once.</p>
<p><font face="Consolas">for (std::cregex_token_iterator it(file.begin(), file.end(), word_regex),      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; end; it != end; ++it)       <br />{       <br />&#160;&#160;&#160; ++result[*it];       <br />}</font></p>
<p>Here file.begin() and file.end() are simply pointers delimiting the range of characters in the file (just as the begin() and end() members of a std::vector delimit the range of characters stored in the vector).&#160; This implementation already performs much better for several reasons:</p>
<ol>
<li>we avoid the high overhead of the C++ I/O library </li>
<li>we scan the file only once and do not make unnecessary copies of the text </li>
<li>the characters are contiguous in memory and are scanned from beginning to end, and modern CPUs are designed to maximize performance of forward iteration over an array </li>
</ol>
<p>However, we can still improve performance even further:&#160; regular expressions are a wonderful tool, but there is some overhead to using them.&#160; This overhead is especially acute with very simple expressions, like <strong>\w+</strong>.&#160; This regular expression can be trivially implemented using a simple lexer that performs far better than the regular expression.&#160; Here is the implementation:</p>
<p><font face="Consolas">char const * word_first = nullptr;</font></p>
<p><font face="Consolas">for (auto it = file.begin(); it != file.end(); ++it)      <br />{       <br />&#160;&#160;&#160; if (*it &lt; 0 || (!std::isalnum(*it) &amp;&amp; *it != &#8216;_&#8217;))       <br />&#160;&#160;&#160; {       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (word_first)       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ++result[std::string(word_first, it)];       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</font></p>
<p><font face="Consolas">&#160;&#160;&#160;&#160;&#160;&#160;&#160; word_first = nullptr;      <br />&#160;&#160;&#160; }       <br />&#160;&#160;&#160; else if (!word_first)       <br />&#160;&#160;&#160; {       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; word_first = it;       <br />&#160;&#160;&#160; }       <br />}</font></p>
<p><font face="Consolas">if (word_first)      <br />{       <br />&#160;&#160;&#160; ++result[std::string(word_first, file.end())];       <br />}</font></p>
<p>This lexer makes a single pass over the text using the iterator named <strong>it</strong>.&#160; When the lexer is scanning a word, the <strong>word_first</strong> iterator points to the initial character of the word; when it is not scanning a word (e.g. when it is scanning punctuation or whitespace), word_first is null.</p>
<p>The <strong>if</strong> statement tests true if the current character is <em>not</em> a word character.&#160; We define word characters as letters, numbers, and the underscore (this is the same set of characters matched by <strong>\w</strong>).&#160; For each character, we do one of three things depending on what the character is and what the current state of the lexer is:</p>
<p>If the character <strong>*it</strong> is a word character and…</p>
<ul>
<li>…we are not currently scanning a word, then <strong>*it</strong> is the first character of a word and we set <strong>word_first = it</strong> </li>
<li>…we are currently scanning a word, then we do nothing (we simply continue with the next character) </li>
</ul>
<p>If the character <strong>*it</strong> is not a word character and…</p>
<ul>
<li>…we are not currently scanning a word, then we do nothing (we simply continue with the next character) </li>
<li>…we are currently scanning a word, then <strong>[word_first, it)</strong> is a word:&#160; we increment the number of times we have seen it then reset <strong>word_first</strong> </li>
</ul>
<p>Finally, if the file ends with a word, we process the last word.</p>
<p>Some might argue, “oh, you’re using pointers—that’s not modern C++ at all!”&#160; Note, however, that the pointers used here are really just iterators into an array.&#160; There are no raw pointers owning dynamically allocated objects.&#160; There is no arithmetic that wouldn’t otherwise be valid for, say, <strong>std::vector&lt;T&gt;::iterator</strong>.&#160; This particular function could be nicely wrapped up into a more generic “lexing” algorithm.</p>
<p><strong>Concurrency</strong></p>
<p>The final area worth exploring is whether a significant performance improvement could be made by scanning the files concurrently. It’s a reasonable assumption since the algorithm itself is compute-bound and the data is already partitioned into separate files and thus independent sections of memory. The challenge of course is to produce a single map of the results while avoiding the locking overhead required for updating it from different threads. Fortunately, the Visual C++ Concurrency Runtime (ConcRT) provides everything we might need. Still, there are many ways to solve the problem such as with explicit locks or replacing std::map with a currency-safe container. However, we prefer to avoid locks entirely.&#160; The PPL’s combinable type is just what we need.</p>
<p><font face="Consolas">typedef std::map&lt;std::string, unsigned&gt; word_map;      <br />ppl::combinable&lt;word_map&gt; shared;</font></p>
<p>The combinable type is sort of like an encapsulated thread-local storage wrapper.&#160; We can then refer to the <strong>shared </strong>from within a parallel algorithm such as the PPL’s parallel_for_each and each thread will receive a local copy of the map.</p>
<p><font face="Consolas">ppl::parallel_for_each(argv + 1, argv + argc, [&amp;](char const * name)      <br />{       <br />&#160;&#160;&#160; auto &amp; result = shared.local();</font></p>
<p><font face="Consolas">&#160;&#160;&#160; // map and scan file &#8230;      <br />});</font></p>
<p>Note that this is not necessarily one local copy per file but rather one per runtime thread. If the computer only has two cores and the scheduler decides that those threads are rarely blocking, which they won’t be in this case, then it’s unlikely that more than two maps will be used no matter how many files are processed. Of course, the results still need to be combined, which can be done using the combine_each function.</p>
<p><font face="Consolas">word_map result;</font></p>
<p><font face="Consolas">shared.combine_each([&amp;] (word_map const &amp; source)      <br />{       <br />&#160;&#160;&#160; for (auto const &amp; w : source)       <br />&#160;&#160;&#160; {       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; result[w.first] += w.second;       <br />&#160;&#160;&#160; }       <br />});</font></p>
<p>The lambda will be called for each local copy of the map thereby making it easy to combine the results in one simple step.&#160; With about ten lines of code, we’ve made our program concurrent.</p>
<p><strong>The End Result</strong></p>
<p>Our final implementation is roughly 45x faster than the reference implementation when processing eight files on a quad core Intel i7.&#160; Its single-threaded throughput is substantially better as well:&#160; it is 12x faster when processing a single file, in part due to the use of memory-mapped I/O, and in part due to the use of a custom lexer.&#160; Four large text files totaling 243MB in size were used for measurement.</p>
<p>Does this implementation offer the best possible performance?&#160;&#160; Hardly.&#160; We’ve identified a few opportunities where we might be able to improve performance:</p>
<ol>
<li>Our implementation still uses std::string, which stores a copy of the string that it represents.&#160; Each time we construct a new std::string, a new copy is made.&#160; These copies are all unnecessary:&#160; since we are mapping the entire file in memory, we could simply store pointers to the words represented in the mapped range. </li>
<li>Words are only removed from the map when the map is destroyed, so a custom allocator could be used that offers better performance for this particular usage scenario than the default heap allocator. </li>
<li>We scan the text linearly, so it is possible to build a hash of each word as we read them, without making an additional scan over the text.&#160; This could allow use of unordered containers or could be used to offer better ordered container insertion performance (e.g. by sorting first by hash, then by text). </li>
</ol>
<p>We prototyped (1) and (2) and found that both yielded small performance improvements (on the order of about 7% for each of them).&#160; (1) yielded less of a benefit than we expected because most words are small, so the small string optimization (SSO) allows most string constructions not to require heap allocation.&#160; However, both of these changes resulted in code that was substantially more complex than our final implementation, so we chose not to include them.</p>
<p>One of our goals was to demonstrate that it’s not particularly difficult to write high performance code in C++, though it does take a bit more work and some knowledge of the platform.&#160; Our final implementation totals 148 lines, including #includes and timing code for performance measurement.&#160; This is just under 3x longer than the reference implementation, though much of that is because we chose to implement a proper RAII container—file_view—to own a mapped view of a file.&#160; The algorithm is more complex, but not substantially more so.</p>
<p>While some platform-specific code is required, it would be quite straightforward to encapsulate this platform-specific code in a library that has a platform-neutral API.&#160; Most platforms support mapping of files into memory, they just support it differently.</p>
<p>Finally, no commas were harmed in the making of this documentary. </p>
<p>The source code: <a href="http://markerrcontrols.com/pub/words.cpp">words.cpp</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kennykerrca.wordpress.com/336/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kennykerrca.wordpress.com/336/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=336&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kennykerr.ca/2013/01/21/the-great-c-wordament-meet-windows/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/71d4efa35b3d11457c35543f9f55fba3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kennykerrca</media:title>
		</media:content>
	</item>
		<item>
		<title>Upcoming Training Material</title>
		<link>http://kennykerr.ca/2013/01/14/upcoming-training-material/</link>
		<comments>http://kennykerr.ca/2013/01/14/upcoming-training-material/#comments</comments>
		<pubDate>Mon, 14 Jan 2013 14:32:10 +0000</pubDate>
		<dc:creator>Kenny Kerr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kennykerr.ca/?p=325</guid>
		<description><![CDATA[Here’s what you can expect from me in terms of training material this year. Pluralsight Last year I published C Programming Language Fundamentals and Direct2D Fundamentals. The latter provides a comprehensive introduction to the original version of Direct2D that first shipped with Windows 7. Here&#8217;s a preview. Later this month I will release Direct2D Fundamentals [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=325&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Here’s what you can expect from me in terms of training material this year.</p>
<p><strong>Pluralsight</strong></p>
<p>Last year I published <a href="http://www.pluralsight.com/training/Courses/TableOfContents/c-lang-fundamentals">C Programming Language Fundamentals</a> and <a href="http://www.pluralsight.com/training/Courses/TableOfContents/direct2d-fundamentals">Direct2D Fundamentals</a>. The latter provides a comprehensive introduction to the original version of Direct2D that first shipped with Windows 7. <a href="http://www.youtube.com/watch?v=ATS0ANW1UxQ&amp;hd=1">Here&#8217;s a preview</a>.</p>
<p>Later this month I will release Direct2D Fundamentals – Part 2. This new course focuses on the version of Direct2D that first shipped with Windows 8. It includes the necessary information about Direct3D and DXGI that you will need to leverage Direct2D 1.1 as well as everything you need to know in order to use Direct2D on the Windows desktop, with the Windows Runtime, with XAML, on the Windows Phone, and much more.</p>
<p>If this sounds interesting then get yourself ready by working through <a href="http://www.pluralsight.com/training/Courses/TableOfContents/direct2d-fundamentals">Direct2D Fundamentals – Part 1</a>. That will give you the necessary groundwork for part 2.</p>
<p><strong>MSDN Magazine</strong></p>
<p>I first wrote about Direct2D in the magazine back when Windows 7 was in beta.</p>
<p><a href="http://msdn.microsoft.com/magazine/dd861344.aspx">Introducing Direct2D</a></p>
<p><a href="http://msdn.microsoft.com/magazine/ee413543.aspx">Drawing with Direct2D</a></p>
<p><a href="http://msdn.microsoft.com/magazine/ee819134.aspx">Layered Windows with Direct2D</a></p>
<p><a href="http://weblogs.asp.net/kennykerr/archive/2009/09/15/direct2d-and-the-desktop-window-manager.aspx">Direct2D and the Desktop Window Manager</a></p>
<p><a href="http://kennykerr.ca/2012/12/01/windows-phone-and-direct2d/">Windows Phone and Direct2D</a> (more recently)</p>
<p>I decided to spend a bit more time this year on Direct2D, both to bring some more attention to this great API as well as to introduce some of the Direct2D-related innovation in the Windows 8 family of operating systems. As such, here is an outline of topics for my Windows with C++ column over the next few months.</p>
<p>February: Creating Desktop Apps with Visual C++ 2012 – A desktop application primer</p>
<p>March: Rendering in a Desktop App with Direct2D – From USER/GDI to Direct2D 1.0</p>
<p>April: Introducing Direc2D 1.1 – Where Direct2D embraces Direct3D and DXGI (on the desktop)</p>
<p>May: Direct2D and the Windows Runtime</p>
<p>June: Direct2D and XAML</p>
<p><strong>What Next?</strong></p>
<p>I am planning to do a Pluralsight course on Classic COM and WinRT using modern C++ and WRL. I may also spend a few months later this year focusing on WRL in my Windows with C++ column.</p>
<p>I know <a href="http://www.gregcons.com/KateBlog/">Kate Gregory</a> is also planning some more <a href="http://www.pluralsight.com/training/Authors/Details/kate-gregory">excellent C++ and Visual Studio</a> material for Pluralsight.</p>
<p>Beyond that, what would you like to see in terms of training material?</p>
<p>&nbsp;</p>
<p>You can also find me on Twitter at <a href="http://twitter.com/kennykerr">@kennykerr</a></p>
<p>&nbsp;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kennykerrca.wordpress.com/325/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kennykerrca.wordpress.com/325/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=325&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kennykerr.ca/2013/01/14/upcoming-training-material/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/71d4efa35b3d11457c35543f9f55fba3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kennykerrca</media:title>
		</media:content>
	</item>
		<item>
		<title>SyncTools for Sysinternals</title>
		<link>http://kennykerr.ca/2013/01/04/synctools-for-sysinternals/</link>
		<comments>http://kennykerr.ca/2013/01/04/synctools-for-sysinternals/#comments</comments>
		<pubDate>Fri, 04 Jan 2013 16:26:58 +0000</pubDate>
		<dc:creator>Kenny Kerr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://kennykerrca.wordpress.com/?p=322</guid>
		<description><![CDATA[SyncTools is a meta-tool that keeps a folder on your computer up-to-date with all the latest tools from Sysinternals. Simply pick a folder where you would like to keep the Sysinternals tools and run SyncTools.exe in that folder. It will download all of the tools and check for updates on tools it previously downloaded. Any [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=322&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>SyncTools is a meta-tool that keeps a folder on your computer up-to-date with all the latest tools from <a href="http://www.sysinternals.com/">Sysinternals</a>. Simply pick a folder where you would like to keep the Sysinternals tools and run SyncTools.exe in that folder. It will download all of the tools and check for updates on tools it previously downloaded. Any time Mark Russinovich publishes an updated version or even a completely new tool, simply rerun SyncTools.exe to download it for you.</p>
<p><b>Using SyncTools</b></p>
<p><a href="http://markerrcontrols.com/pub/SyncTools.zip">Download SyncTools.exe</a> and copy it to a local folder such as C:\Tools.</p>
<p>The first time you run SyncTools.exe it will download all of the tools.</p>
<p><img style="background-image:none;padding-top:0;padding-left:0;margin:0 5px;display:inline;padding-right:0;border-width:0;" title="" alt="" src="http://kennykerrca.files.wordpress.com/2013/01/1_first_run.png?w=641&#038;h=458" width="641" height="458" border="0" /></p>
<p>When new tools or updates are available, simply rerun SyncTools.exe to check for updates and it will download updates as necessary.</p>
<p><img style="background-image:none;padding-top:0;padding-left:0;margin:0 5px;display:inline;padding-right:0;border-width:0;" title="" alt="" src="http://kennykerrca.files.wordpress.com/2013/01/2_update.png?w=641&#038;h=458" width="641" height="458" border="0" /></p>
<p>A <strong>*</strong> prefix means it’s a new file while a <strong>u</strong> prefix mean it’s an update. If there’s a problem with a download then a <strong>!</strong> prefix is used and a description hints at the problem.</p>
<p>In the screenshot above, I didn’t have a copy of Autoruns, LiveKd and PsKill were updated, but I left Process Explorer running and SyncTools was not able to update it. The solution is simple: simply close the tool in question and rerun SyncTools and it will try to download the update again.</p>
<p><img style="background-image:none;padding-top:0;padding-left:0;margin:0 5px;display:inline;padding-right:0;border-width:0;" title="" alt="" src="http://kennykerrca.files.wordpress.com/2013/01/3_fixed.png?w=641&#038;h=458" width="641" height="458" border="0" /></p>
<p><b>How it works</b></p>
<p>I wrote this little tool a few years back as an excuse to use <a href="http://msdn.microsoft.com/en-us/magazine/cc716528.aspx">asynchronous WinHTTP</a> and it has served me well. Last night I rewrote it from scratch as I had some new techniques I wanted to explore. This version is simpler, although not quite as fast as it only downloads one file at a time.</p>
<p>The first thing it does is download the Sysinternals directory listing. If SyncTools was previously run it will then check the file signatures from the directory listing with the information from the last run. Only new or updated files will be download.</p>
<p>By default, it ignores the following files either because the web server won’t serve them up anyway or because they just aren’t of any use.</p>
<p>*.sys<br />
*.html<br />
*.cnt<br />
*.scr<br />
*.hlp<br />
*.txt<br />
*.asp<br />
*.aspx</p>
<p>If you would like to tailor this list, simply create a text file in your tools folder called <strong>.SyncIgnore </strong>and place any file name patterns in it, each on its own line.</p>
<p>Finally, you can use the <strong>-u</strong> and <strong>-d</strong> arguments to tweak the tool’s behavior. The <strong>-u</strong> argument lets you provide an alternative URL and the <strong>-d</strong> argument lets you specify something other than the current directory to sync to.</p>
<p><span style="font-family:Consolas;">SyncTools.exe -d C:\Tools -u <a href="http://live.sysinternals.com" rel="nofollow">http://live.sysinternals.com</a></span></p>
<p>Enjoy!</p>
<a href='http://twitter.com/kennykerr' class='twitter-follow-button' data-show-count='false' data-text-color='#1c1c1c' data-link-color='#004276'>Follow @kennykerr</a>
<p>&nbsp;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kennykerrca.wordpress.com/322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kennykerrca.wordpress.com/322/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=322&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kennykerr.ca/2013/01/04/synctools-for-sysinternals/feed/</wfw:commentRss>
		<slash:comments>53</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/71d4efa35b3d11457c35543f9f55fba3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kennykerrca</media:title>
		</media:content>

		<media:content url="http://kennykerrca.files.wordpress.com/2013/01/1_first_run.png" medium="image" />

		<media:content url="http://kennykerrca.files.wordpress.com/2013/01/2_update.png" medium="image" />

		<media:content url="http://kennykerrca.files.wordpress.com/2013/01/3_fixed.png" medium="image" />
	</item>
		<item>
		<title>The Evolution of Threads and I/O in Windows</title>
		<link>http://kennykerr.ca/2013/01/03/the-evolution-of-threads-and-io-in-windows/</link>
		<comments>http://kennykerr.ca/2013/01/03/the-evolution-of-threads-and-io-in-windows/#comments</comments>
		<pubDate>Thu, 03 Jan 2013 12:50:48 +0000</pubDate>
		<dc:creator>Kenny Kerr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://kennykerrca.wordpress.com/?p=316</guid>
		<description><![CDATA[My latest column for MSDN Magazine is now available online as well as in print. When starting a new project, do you ask yourself whether your program will be compute-bound or I/O-bound? You should. I’ve found that in most cases it’s either one or the other. You might be working on an analytics library that’s [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=316&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>My latest column for MSDN Magazine <a href="http://msdn.microsoft.com/magazine/jj883951.aspx">is now available online</a> as well as in print. </p>
<blockquote><p>When starting a new project, do you ask yourself whether your program will be compute-bound or I/O-bound? You should. I’ve found that in most cases it’s either one or the other. You might be working on an analytics library that’s handed a pile of data and keeps a bunch of processors busy while crunching it all down to a set of aggregates. Alternatively, your code might spend most of its time waiting for stuff to happen, for data to arrive over the network, a user to click on something or perform some complex six-fingered gesture. In this case, the threads in your program aren’t doing very much. Sure, there are cases where programs are heavy both on I/O and computation. The SQL Server database engine comes to mind, but that’s less typical of today’s computer programming. More often than not, your program is tasked with coordinating the work of others. It might be a Web server or client communicating with a SQL database, pushing some computation to the GPU or presenting some content for the user to interact with. Given all of these different scenarios, how do you decide what threading capabilities your program requires and what concurrency building blocks are necessary or useful? Well, that’s a tough question to answer generally and something you’ll need to analyze as you approach a new project. It’s helpful, however, to understand the evolution of threading in Windows and C++ so you can make an informed decision based on the practical choices that are available.</p>
</blockquote>
<p>This article carries on where I left off in November with <a href="http://msdn.microsoft.com/magazine/jj721588.aspx">The Evolution of Synchronization in Windows and C++</a>.</p>
<p>You can find links to <a href="http://kennykerr.ca/articles/">more of my articles here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kennykerrca.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kennykerrca.wordpress.com/316/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kennykerr.ca&#038;blog=15791744&#038;post=316&#038;subd=kennykerrca&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kennykerr.ca/2013/01/03/the-evolution-of-threads-and-io-in-windows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/71d4efa35b3d11457c35543f9f55fba3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kennykerrca</media:title>
		</media:content>
	</item>
	</channel>
</rss>
