<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>davidpritchard.org</title><link>https://davidpritchard.org/</link><description>Transportation modelling, data science, and software development.</description><language>en-US</language><lastBuildDate>Wed, 07 Jan 2026 20:04:27 +0000</lastBuildDate><atom:link href="https://davidpritchard.org/index.xml" rel="self" type="application/rss+xml"/><item><title>An MMXXIV mmixx</title><link>https://davidpritchard.org/archives/an-mmxxiv-mmixx/</link><pubDate>Wed, 07 Jan 2026 20:04:27 +0000</pubDate><guid>https://davidpritchard.org/archives/an-mmxxiv-mmixx/</guid><description>&lt;p&gt;Helluva year, was 2025. Had to do something while the world burned; I elected to run a lot and curate these 2024 tracks.&lt;/p&gt;&#10;&lt;p&gt;Of these, I&amp;rsquo;ve seen a few live over the last few years: Thievery Corporation, City of the Sun, Great Lake Swimmers, Harry Manx, La Force and Milky Chance. Three of them played within a mile of my house, thanks in part to the amazing &lt;a href="https://redbirdlive.ca"&gt;Red Bird Music&lt;/a&gt;.&lt;/p&gt;&#10;&lt;iframe loading="lazy" allow="autoplay *; encrypted-media *; fullscreen *; clipboard-write" frameborder="0" height="450" style="width:100%;max-width:660px;overflow:hidden;background:transparent;" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-storage-access-by-user-activation allow-top-navigation-by-user-activation" src=" https://embed.music.apple.com/ca/playlist/mix-2024/pl.u-b3b8MpDULz7qvP"&gt;&lt;/iframe&gt;&#10;&lt;iframe loading="lazy" src="https://open.spotify.com/embed/playlist/3fWNC8bisnpSOJruRwiWQq?si=013c623786fe43ed" width="400" height="170" frameborder="0" allowtransparency="true" allow="encrypted-media"&gt;&lt;/iframe&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;new jackson&lt;/strong&gt;. si si si. electronic, 2024.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;fred again.. &amp;amp; baby keam.&lt;/strong&gt; leavemealone. drum &amp;amp; bass, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;jamie xx feat. the avalanches.&lt;/strong&gt; all you children. electronic, 2024.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;thievery corporation&lt;/strong&gt;. letter to the editor. dub, 2017.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;city of the sun.&lt;/strong&gt; sica. rock, 2023.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;daughter.&lt;/strong&gt; dandelion. rock, 2023.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;nilüfer yanya.&lt;/strong&gt; stabilise. pop, 2021.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;foals.&lt;/strong&gt; mountain at my gates. rock, 2015.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;great lake swimmers.&lt;/strong&gt; flight paths. folk, 2023.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;harry manx.&lt;/strong&gt; coat of mail. folk, 2002.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;billie eilish&lt;/strong&gt;. tv. pop, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;slowdive.&lt;/strong&gt; shanty. rock, 2023.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;la force&lt;/strong&gt;. outrun the sun. rock, 2023.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;hologramme&lt;/strong&gt;. karkoram. downtempo, 2021.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;aera.&lt;/strong&gt; line of conception. acid jazz, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;bicep feat. clara la san&lt;/strong&gt;. water. trance, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;aphex twin&lt;/strong&gt;. minipops 67 [120.2] [source field mix]. experimental, 2014.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;james blake.&lt;/strong&gt; asking to break. pop, 2023.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;the marías&lt;/strong&gt;. no one noticed. pop, 2024.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;håkon kornstadt &amp;amp; bugge wesseltoft.&lt;/strong&gt; yellow is the colour. jazz, 2001.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;milky chance &amp;amp; fatoumata diawara&lt;/strong&gt;. flicker in the dark. pop, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;marbert rocel.&lt;/strong&gt; dance slow [joakim&amp;rsquo;s blood moon remix]. acid jazz, 2019.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;lazerhawk&lt;/strong&gt;. king of the streets. synthwave, 2013.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;d4vd.&lt;/strong&gt; leave her. pop, 2024.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;sunpalace.&lt;/strong&gt; rude movements. acid jazz, 2014.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;acemo.&lt;/strong&gt; new love. ambient, 2024.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;leon vynehall&lt;/strong&gt;. midnight on the rainbow road (BAB 5). downtempo, 2015.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;else&lt;/strong&gt;. you. downtempo, 2017.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;ross from friends.&lt;/strong&gt; epiphany. house, 2019.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;squarepusher&lt;/strong&gt;. detroit people mover. ambient, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;svaneborg kardyb.&lt;/strong&gt; cycles. rock, 2024.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;</description></item><item><title>The running groove</title><link>https://davidpritchard.org/archives/the-running-groove/</link><pubDate>Sun, 26 Jan 2025 15:18:30 +0000</pubDate><guid>https://davidpritchard.org/archives/the-running-groove/</guid><description>&lt;p&gt;Bike commuting was my thing. I might do a 5k run once or twice a month, but that was it.&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/the-running-groove/images/IMG_5344-768x1024.jpeg" alt="" loading="lazy"&gt;&lt;/p&gt;&#10;&lt;p&gt;But then I moved to Old Ottawa South and took a job in Kanata. The bike commute would be 50km roundtrip - really not viable. Time was increasingly short, and running has the best &amp;ldquo;bang for the buck&amp;rdquo; in terms of the calorie burn per minute. And my neighbourhood is a running dream: between the two banks of the Rideau River and the Rideau Canal, I can pick any of eight great routes for a run, with almost zero traffic lights to manage. The canal is even ploughed down to pavement for the entire winter.&lt;/p&gt;&#10;&lt;p&gt;So: I gave running a more serious shot. I ditched my old &amp;ldquo;barefoot&amp;rdquo; shoes and the whimsical evolutionary running story that inspired them. I ramped up to a run every week, and then more.&lt;/p&gt;&#10;&lt;p&gt;Tamarack Race Day in May 2024 marked my first 10km since the high school cross-country team. It was really a blast - a very friendly, party atmosphere with a huge crowd of runners. I followed that up with a daily run in June, then pushed on to try the &amp;ldquo;Commander&amp;rsquo;s Challenge&amp;rdquo; (5k + 10k, back-to-back) at the Army Run in September.&lt;/p&gt;&#10;&lt;p&gt;And so - running is now my main hobby. At 2-3 runs per week at the moment, it&amp;rsquo;s a pretty reasonable time commitment, though that&amp;rsquo;ll rise if I go for the half marathon this spring.&lt;/p&gt;&#10;&lt;p&gt;The stress relief and sleep improvements are amazing, especially in these&amp;hellip; trying political moments. The other motivator is to &amp;ldquo;model&amp;rdquo; good exercise/anxiety/sleep management for the kids - which seems to be working; H decided to make track &amp;amp; field her main sport this year.&lt;/p&gt;&#10;&lt;p&gt;So finally - I&amp;rsquo;ve made quite a bit of progress in my total distance and pace over the year. A few fun stats:&lt;/p&gt;&#10;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;2022&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;2023&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;2024&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Total distance&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;190 km&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;360 km&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;857 km&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Best 5 km time&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;28:06&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;24:29&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;23:01&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Best 5 km pace&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;5:37/km&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;4:54/km&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;4:36/km&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Number of 10km+ runs&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;1&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;23&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;</description></item><item><title>Intervals mix</title><link>https://davidpritchard.org/archives/intervals-mix/</link><pubDate>Thu, 05 Dec 2024 23:01:00 +0000</pubDate><guid>https://davidpritchard.org/archives/intervals-mix/</guid><description>&lt;p&gt;My exercise shifted this year. To date, I&amp;rsquo;ve mostly been a cyclist, with a bit of running, swimming or cross-country skiing from time-to-time. And I&amp;rsquo;ve mostly cycled for transportation: commuting or grocery shopping, with the exercise as a side benefit but not the main purpose.&lt;/p&gt;&#10;&lt;p&gt;But the last 18 months have been more about running. More on that in a separate post. One of the triggers was an upgrade from regular AirPods - which constantly fell out of my ears if I sweated at all - to AirPods Pro that actually stay in, connected to music playing on my watch. Suddenly, regular running to less monotonous. I could imagine running further, 60 minutes. Or faster.&lt;/p&gt;&#10;&lt;p&gt;And so: this mix is a bit more upbeat. Not quite &amp;ldquo;DJ Tiësto trance rave maxxed out&amp;rdquo;, but I&amp;rsquo;ll admit I&amp;rsquo;ve run to that too.&lt;/p&gt;&#10;&lt;h2 id="part-1"&gt;Part 1&lt;/h2&gt;&#10;&lt;iframe loading="lazy" allow="autoplay *; encrypted-media *; fullscreen *; clipboard-write" frameborder="0" height="450" style="width:100%;max-width:660px;overflow:hidden;background:transparent;" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-storage-access-by-user-activation allow-top-navigation-by-user-activation" src="https://embed.music.apple.com/ca/playlist/mix-2023-part-1/pl.u-LdbqqJ4IkmPeda"&gt;&lt;/iframe&gt;&#10;&lt;iframe loading="lazy" src="https://open.spotify.com/embed/playlist/3UJFdECxBT6zN30MILm2ZE?si=013c623786fe43ed" width="300" height="170" frameborder="0" allowtransparency="true" allow="encrypted-media"&gt;&lt;/iframe&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;hologramme&lt;/strong&gt;. aphrodite. electronic, 2023.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;justice.&lt;/strong&gt; pleasure x newjack x civilization (www). house, 2018.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;the brothers macklovitch &amp;amp; a-trak feat. leven kali.&lt;/strong&gt; give love to get some. r&amp;amp;b, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;elle varner feat. j. cole&lt;/strong&gt;. only want to give it to you. r&amp;amp;b, 2012.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;the strike boys.&lt;/strong&gt; everybody. dub, 2004.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;il:lo feat. brain blake.&lt;/strong&gt; reine. downtempo, 2015.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;squarepusher.&lt;/strong&gt; oberlove. experimental, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;lanark artefax.&lt;/strong&gt; touch absence. ambient, 2017.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;caroline polachek.&lt;/strong&gt; welcome to my island. pop, 2023.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;jordana &amp;amp; tv girl.&lt;/strong&gt; summer&amp;rsquo;s over. pop, 2021.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;the smile&lt;/strong&gt;. thin thing. rock, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;hemlocke springs.&lt;/strong&gt; sever the blight. pop, 2023.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;jennylee&lt;/strong&gt;. tickles. pop, 2021.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;sufjan stevens&lt;/strong&gt;. the owl and the tanager. folk, 2010.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;jeremy olander.&lt;/strong&gt; steps. techno, 2007.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;tsha feat. clementine douglas&lt;/strong&gt;. dancing in the shadows. house, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;sub focus&lt;/strong&gt;. calling for a sign. drum &amp;amp; bass, 2023.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;pola &amp;amp; bryson, goddard. &amp;amp; lens feat. javeon.&lt;/strong&gt; way up. drum &amp;amp; bass, 2023.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h2 id="part-2"&gt;Part 2&lt;/h2&gt;&#10;&lt;iframe loading="lazy" allow="autoplay *; encrypted-media *; fullscreen *; clipboard-write" frameborder="0" height="450" style="width:100%;max-width:660px;overflow:hidden;background:transparent;" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-storage-access-by-user-activation allow-top-navigation-by-user-activation" src="https://embed.music.apple.com/ca/playlist/mix-2023-part-2/pl.u-PDb44aEIkVR92Z"&gt;&lt;/iframe&gt;&#10;&lt;iframe loading="lazy" src="https://open.spotify.com/embed/playlist/5VWXoBenSwdd5lajC8qGTT?si=013c623786fe43ed" width="300" height="170" frameborder="0" allowtransparency="true" allow="encrypted-media"&gt;&lt;/iframe&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;the chemical brothers&lt;/strong&gt;. goodbye. big beat, 2023.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;charli xcx.&lt;/strong&gt; lightning. dance, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;machinedrum feat. kučka&lt;/strong&gt;. violet. experimental, 2023.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;beastie boys.&lt;/strong&gt; ok. hip hop, 2011.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;wax motif &amp;amp; riordan.&lt;/strong&gt; la samba. house, 2023.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;lionel boy.&lt;/strong&gt; loop. r&amp;amp;b, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;peaking lights.&lt;/strong&gt; soft escape [moonman mix]. dub, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;dj minx&lt;/strong&gt;. let it ride. house, 2023.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;ver:west&lt;/strong&gt;. 5 seconds before sunrise. trance, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;jex opolis.&lt;/strong&gt; mt. belzoni. downtempo, 2017.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;otik&lt;/strong&gt;. subaru dreams. downtempo, 2023.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;shigeto.&lt;/strong&gt; detroit, pt. 1. ambient, 2013.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;slowdive.&lt;/strong&gt; kisses. pop, 2023.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;sally shapiro&lt;/strong&gt;. rent. pop, 2023.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;bootleg rascal.&lt;/strong&gt; head in the clouds. pop, 2016.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;asian dub foundation.&lt;/strong&gt; culture move. dub, 1998.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;orbital feat. the little pest&lt;/strong&gt;. you are the frequency. techno, 2023.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;roland tings.&lt;/strong&gt; lights on the headland. techno, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;quantic&lt;/strong&gt;. time is the enemy. acid jazz, 2001.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;gotan project.&lt;/strong&gt; época. downtempo, 2001.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;taylor swift&lt;/strong&gt;. safe &amp;amp; sound [taylor&amp;rsquo;s version]. folk, 2012.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;</description></item><item><title>The commuter listen</title><link>https://davidpritchard.org/archives/the-commuter-listen/</link><pubDate>Sat, 10 Jun 2023 22:01:19 +0000</pubDate><guid>https://davidpritchard.org/archives/the-commuter-listen/</guid><description>&lt;p&gt;As of May 2022, I started driving to my job at Apple, three days a week. Initially, I was using our ancient beige Camry (2001), which I&amp;rsquo;d delayed repairing and had a godawful loud engine noise. I was holding out on repairs, expecting we&amp;rsquo;d get an electric vehicle (Chevy Bolt) we&amp;rsquo;d queued for a few months earlier. Eventually, I gave up on the Bolt, discovered the Camry repairs were trivial and I finally got to listen to music while driving. And then in February, a Bolt finally arrived and I could hear music on a genuinely nice audio system.&lt;/p&gt;&#10;&lt;p&gt;So this was my commuter soundtrack. I&amp;rsquo;ve grown a little tired of music for the time being - moved on to podcasts (like this movie show) and audiobooks for a bit. But I&amp;rsquo;ll be back to the music again before long.&lt;/p&gt;&#10;&lt;h2 id="part-1"&gt;Part 1&lt;/h2&gt;&#10;&lt;iframe loading="lazy" allow="autoplay *; encrypted-media *; fullscreen *; clipboard-write" frameborder="0" height="450" style="width:100%;max-width:660px;overflow:hidden;background:transparent;" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-storage-access-by-user-activation allow-top-navigation-by-user-activation" src="https://embed.music.apple.com/ca/playlist/mix-2022-part-1/pl.u-Ymb00RquxbN9Gz"&gt;&lt;/iframe&gt;&#10;&lt;iframe loading="lazy" src="https://open.spotify.com/embed/playlist/1lZmoFIVW57iYS5GLY0fuL?si=013c623786fe43ed" width="300" height="170" frameborder="0" allowtransparency="true" allow="encrypted-media"&gt;&lt;/iframe&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;yeah yeah yeahs&lt;/strong&gt;. burning. rock, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;catfish &amp;amp; the bottlemen&lt;/strong&gt;. kathleen. rock, 2014.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;friendly fires.&lt;/strong&gt; kiss of life. rock, 2009.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;eight and a half.&lt;/strong&gt; wait up. rock, 2012.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;maddie zahm.&lt;/strong&gt; blind spot. folk, 2021.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;luna li.&lt;/strong&gt; what you&amp;rsquo;re thinking. pop, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;cut copy.&lt;/strong&gt; running in the grass. downtempo, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;simian mobile disco&lt;/strong&gt;. i believe. electronic, 2007.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;cannons.&lt;/strong&gt; bad dream. pop, 2021.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;kyle dixon &amp;amp; michael stein&lt;/strong&gt;. escape. synthwave, 2017.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;waveshaper.&lt;/strong&gt; future vision. synthwave, 2016.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;the rapture.&lt;/strong&gt; i need your love. pop, 2003.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;bonobo.&lt;/strong&gt; ketto. downtempo, 2006.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;beach house.&lt;/strong&gt; only you know. rock, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;khruangbin &amp;amp; leon bridges.&lt;/strong&gt; mariella. rock, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;jeremy steig.&lt;/strong&gt; howling for judy. jazz, 1969.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;cotonete.&lt;/strong&gt; super vilain wants love [dj deep &amp;amp; romain poncet remix]. acid jazz, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;the avalanches&lt;/strong&gt;. take care in your dreaming. downtempo, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;prince innocence.&lt;/strong&gt; forever &amp;amp; ever. rock, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;cigarettes after sex.&lt;/strong&gt; pistol. downtempo, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h2 id="part-2"&gt;Part 2&lt;/h2&gt;&#10;&lt;iframe loading="lazy" allow="autoplay *; encrypted-media *; fullscreen *; clipboard-write" frameborder="0" height="450" style="width:100%;max-width:660px;overflow:hidden;background:transparent;" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-storage-access-by-user-activation allow-top-navigation-by-user-activation" src="https://embed.music.apple.com/ca/playlist/mix-2022-part-2/pl.u-vxy66D5I1B3A6v"&gt;&lt;/iframe&gt;&#10;&lt;iframe loading="lazy" src="https://open.spotify.com/embed/playlist/33gUbFOdfuQNtMvFsKyGSh?si=efb6345dbd3f48f7" width="300" height="170" frameborder="0" allowtransparency="true" allow="encrypted-media"&gt;&lt;/iframe&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;widowspeak&lt;/strong&gt;. the good ones. folk, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;lord huron.&lt;/strong&gt; meet me in the woods. rock, 2015.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;johnny marr.&lt;/strong&gt; spirit power and soul. rock, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;solange.&lt;/strong&gt; losing you. r&amp;amp;b, 2012.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;empress of.&lt;/strong&gt; save me. pop, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;warpaint.&lt;/strong&gt; champion. rock, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;billie eilish.&lt;/strong&gt; happier than ever. rock, 2021.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;yung lean &amp;amp; fka twigs.&lt;/strong&gt; bliss. alternative, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;childish gambino.&lt;/strong&gt; algorhythm. alternative, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;beyoncé.&lt;/strong&gt; break my soul. pop, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;coma&lt;/strong&gt; feat. dillon. beyond you and me. house, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;odesza&lt;/strong&gt; feat. bettye lavette. the last goodbye. electronic, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;plaid&lt;/strong&gt;. clock. ambient, 2016.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;com truise.&lt;/strong&gt; &amp;hellip; of your fake dimension. synthwave, 2017.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;robert parker.&lt;/strong&gt; sweet nothings. synthwave, 2016.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;youngblood.&lt;/strong&gt; 5 seconds of summer. pop, 2018.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;the weeknd.&lt;/strong&gt; out of time. r&amp;amp;b, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;troublemakers.&lt;/strong&gt; chez roger boite funk. acid jazz, 2001.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;</description></item><item><title>Soundtrack to Capstone a Pandemic</title><link>https://davidpritchard.org/archives/soundtrack-to-capstone-a-pandemic/</link><pubDate>Tue, 07 Mar 2023 20:48:25 +0000</pubDate><guid>https://davidpritchard.org/archives/soundtrack-to-capstone-a-pandemic/</guid><description>&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/soundtrack-to-capstone-a-pandemic/images/mix-2021-disc-1-300x300.jpg" alt="" loading="lazy"&gt;&lt;/p&gt;&#10;&lt;p&gt;So there was a pandemic. During which I listened to my &lt;a href="https://davidpritchard.org/archives/keep-calm-and-mix-on/"&gt;last mix&lt;/a&gt;, over and over again; as I jogged the exact same riverside route, and my misfitted left-ear AirPod kept falling out.&lt;/p&gt;&#10;&lt;p&gt;And I continued listening, and accumulating tracks. For various reasons™, I couldn&amp;rsquo;t listen to my accumulating list until recently; so the music here wasn&amp;rsquo;t exactly a soundtrack to the pandemic for me. But now, I&amp;rsquo;ve finally assembled it.&lt;/p&gt;&#10;&lt;p&gt;This one&amp;rsquo;s in three parts: a two-disc 2020-21 set, and a 2022 set coming up next. Traditionally, I&amp;rsquo;ve limited myself to the ~100 minutes of a CD; but whatever, let&amp;rsquo;s go with three full hours, and so we get digressively long tracks like Venice Bitch, Beau Mot Plage and America.&lt;/p&gt;&#10;&lt;p&gt;The primary consumer of these mixes is&amp;hellip; me, let&amp;rsquo;s be honest. And one of the main benefits is a bunch of links I can click to find the artists&amp;rsquo; full albums in Apple Music. So&amp;hellip; no more obsessive MP3ing, relevelling and mixing with ancient 2004 Audacity builds.&lt;/p&gt;&#10;&lt;h2 id="part-1"&gt;part 1&lt;/h2&gt;&#10;&lt;iframe loading="lazy" allow="autoplay *; encrypted-media *; fullscreen *; clipboard-write" frameborder="0" height="450" style="width:100%;max-width:660px;overflow:hidden;background:transparent;" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-storage-access-by-user-activation allow-top-navigation-by-user-activation" src="https://embed.music.apple.com/ca/playlist/mix-2021-part-1/pl.u-Ymb09N2IxbN9Gz"&gt;&lt;/iframe&gt;&#10;&lt;iframe loading="lazy" src="https://open.spotify.com/embed/playlist/57NaadS9CP1IlRCExiMzFX?si=d645187b49ae435c" width="300" height="170" frameborder="0" allowtransparency="true" allow="encrypted-media"&gt;&lt;/iframe&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;hologramme&lt;/strong&gt; feat. fakear. felicity [fakear remix]. downtempo, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;kilder &amp;amp; jamal bucanon&lt;/strong&gt;. what i won&amp;rsquo;t do. pop, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;alfie templeman&lt;/strong&gt;. shady. pop, 2021&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;grandson&lt;/strong&gt;. dirty. pop, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;347aidan&lt;/strong&gt;. dancing in my room. pop, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;frank ocean&lt;/strong&gt;. pink + white. r&amp;amp;b, 2016.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;washed out&lt;/strong&gt;. too late. downtempo, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;caribou&lt;/strong&gt;. lime. pop, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;mount kimbie&lt;/strong&gt;. four years and one day. electronic, 2017.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;moderat&lt;/strong&gt;. animal trails. experimental, 2016.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;luke howard&lt;/strong&gt;. bear story i [ben lukas boysen rework]. techno, 2019.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;billie eilish&lt;/strong&gt;. everything i wanted. pop, 2019.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;allan rayman&lt;/strong&gt;. rose. pop, 2018.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;massive attack&lt;/strong&gt;. paradise circus [gui boratto remix]. downtempo, 2010.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;donna missal&lt;/strong&gt;. (to me) your face is love. pop, 2021.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;art school girlfriend.&lt;/strong&gt; diving. rock, 2019.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;lana del rey.&lt;/strong&gt; venice bitch. pop, 2019.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;flume&lt;/strong&gt; feat. toro y moi. the difference. rock, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;new jackson&lt;/strong&gt;. romancecar. downtempo, 2019.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;aurora&lt;/strong&gt;. cure for me. house, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;shire t&lt;/strong&gt;. burnin&amp;rsquo; jungle. pop, 2021.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h2 id="part-2"&gt;part 2&lt;/h2&gt;&#10;&lt;iframe loading="lazy" allow="autoplay *; encrypted-media *; fullscreen *; clipboard-write" frameborder="0" height="450" style="width:100%;max-width:660px;overflow:hidden;background:transparent;" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-storage-access-by-user-activation allow-top-navigation-by-user-activation" src="https://embed.music.apple.com/ca/playlist/mix-2021-part-2/pl.u-vxy6kEMT1B3A6v"&gt;&lt;/iframe&gt;&#10;&lt;iframe style="border-radius:12px" src="https://open.spotify.com/embed/playlist/5huTHuHGfDflWvyq2PB49b?utm_source=generator" width="100%" height="152" frameborder="0" allowfullscreen allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading="lazy"&gt;&lt;/iframe&gt;&#10;&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/soundtrack-to-capstone-a-pandemic/images/mix-2021-disc-2-300x300.jpg" alt="" loading="lazy"&gt;&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;e.vax&lt;/strong&gt;. manila. acid jazz, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;against all logic&lt;/strong&gt;. you (forever). experimental, 2022.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;the midnight&lt;/strong&gt;. neon medusa. synthwave, 2021.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;gorgon city &amp;amp; drama.&lt;/strong&gt; you&amp;rsquo;ve done enough. house, 2021.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;jordan rakei&lt;/strong&gt;. family. r&amp;amp;b, 2021.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;james blake&lt;/strong&gt;. retrograde. r&amp;amp;b, 2013.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;tsha&lt;/strong&gt;. me you. techno, 2019.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;major lazer &amp;amp; nucleya&lt;/strong&gt; feat. rashmeet kaur. jadi buti. dub, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;rae &amp;amp; christian&lt;/strong&gt;. ready to roll [bushy remix]. acid jazz, 2002.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;soul square&lt;/strong&gt; feat. pfel &amp;amp; atom. live and uncut. hip hop, 2010.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;isolée&lt;/strong&gt;. beau mot plage [freedom reform, pt. 1 &amp;amp; 2]. acid jazz, 1999.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;plants and animals&lt;/strong&gt;. sacrifice. rock, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;i break horses&lt;/strong&gt;. silence. rock, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;clairo&lt;/strong&gt;. zinnias. folk, 2021.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;arlo parks&lt;/strong&gt;. sophie. pop, 2019.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;thievery corporation&lt;/strong&gt; feat. gunjan. doors of perception. dub, 2005.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;mac ayres&lt;/strong&gt;. jumping off the moon. r&amp;amp;b, 2019.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;fink&lt;/strong&gt;. hard believer. blues, 2014.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;handsome boy modeling school&lt;/strong&gt; feat. roisin &amp;amp; j-live. the truth. hip hop, 1999.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;girl in red&lt;/strong&gt;. midnight love. pop, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;booka shade &amp;amp; kaktus einarsson&lt;/strong&gt;. perfect in a way. techno, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;sufjan stevens&lt;/strong&gt;. america. rock, 2020.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;</description></item><item><title>C++ Stack Traces on UWP</title><link>https://davidpritchard.org/archives/907/</link><pubDate>Wed, 21 Jul 2021 09:36:11 +0000</pubDate><guid>https://davidpritchard.org/archives/907/</guid><description>&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/907/images/image-2-300x277.png" alt="" loading="lazy"&gt;&lt;/p&gt;&#10;&lt;p&gt;In early 2019, I was working on an app (Mental Canvas Draw) that was in private beta on the Windows Store. There was a nice, robust system to show you which crashes were most common your app. Then suddenly - it disappeared, and instead there was a big stream of “Unknown” class crashes.&lt;/p&gt;&#10;&lt;p&gt;This caused much &lt;a href="https://github.com/microsoft/appcenter/issues/436"&gt;wailing&lt;/a&gt; and gnashing of teeth. I assumed that it would be fixed. Microsoft had recently acquired HockeyApp and was revamping it into Visual Studio AppCenter. Surely that would provide a replacement! Six months passed, a year passed, two years passed, and… nothing for C++ users. (I understand that AppCenter provides a solution for C# users.)&lt;/p&gt;&#10;&lt;figure&gt;&#10;&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/907/images/image-1-882x1024.png" alt="" loading="lazy"&gt;&lt;/p&gt;&#10;&lt;figcaption&gt;&#10;&lt;p&gt;Some wailing&lt;/p&gt;&#10;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&lt;figure&gt;&#10;&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/907/images/image-1-1-1024x261.png" alt="" loading="lazy"&gt;&lt;/p&gt;&#10;&lt;figcaption&gt;&#10;&lt;p&gt;Some gnashing of teeth&lt;/p&gt;&#10;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&lt;p&gt;That app was released widely in March 2021, and I realized an important fact: if you have a large volume of users and/or crashes, you get a valid list of stack traces! There seems to be something about the level of &lt;em&gt;usage per package version&lt;/em&gt; that&amp;rsquo;s important. For &amp;lt;100 crashes per version, I get 0% stack traces; for &amp;lt;250 I get very few; and for the version with 750 crashes I get good (64%) stack traces.&lt;/p&gt;&#10;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Sample size&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Filter Type&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;% Unknown&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;% Stack Traces&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;1,051&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;None&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;50%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;50%&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;750&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Package version&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;36%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;64%&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;485&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;OS version&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;54%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;46%&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;316&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;OS version&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;34%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;66%&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;234&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Package version&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;86%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;14%&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;110&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;OS version&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;50%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;50%&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;100&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;OS version&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;99%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;1%&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;66&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Package version&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;100%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0%&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;p&gt;Even for the &amp;ldquo;higher usage&amp;rdquo; package version, however, I&amp;rsquo;m still not getting solid data on the &amp;ldquo;rare&amp;rdquo; crash types - just the frequent crashes.&lt;/p&gt;&#10;&lt;p&gt;So… what’s to be done? We don’t have access to any off-the-shelf Win32 crash reporting tools, as the necessary APIs are all blocked off from UWP apps. There’s no easy way to get a minidump. There might be some really elaborate solution involving Project Reunion APIs to &lt;a href="https://stefanwick.com/2018/04/06/uwp-with-desktop-extension-part-1/"&gt;monitor the UWP process from Win32-land&lt;/a&gt;, but I haven’t explored those technologies much, and I’m not confident that the Win32 code gets sufficient privilege to dig deep on the UWP process’ behaviour.&lt;/p&gt;&#10;&lt;p&gt;But… UWP apps do have access to one key API: &lt;code&gt;CaptureStackBackTrace&lt;/code&gt;.&lt;/p&gt;&#10;&lt;h2 id="a-bare-bones-solution"&gt;A bare bones solution&lt;/h2&gt;&#10;&lt;p&gt;A full-fledged crash handling and stack trace solution is quite complicated: running in a &lt;a href="https://stackoverflow.com/a/1964556"&gt;separate guard process&lt;/a&gt; to sidestep any memory/stack corruption; multiple threads; full analysis of both the app portion of the stack trace and the system (Windows) portion. My main needs were more narrow, and I built a solution focused on that&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Single thread: only show stack of active (crashing) thread&lt;/li&gt;&#10;&lt;li&gt;App portion of stack is critical; system/Windows portion is desirable, but less important&lt;/li&gt;&#10;&lt;li&gt;Severe memory corruption doesn’t need to be handled initially&lt;/li&gt;&#10;&lt;li&gt;Only handle C++/CX and x64. It probably works for other situations, but that’s all I’ve tested to date.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;With those criteria, I came up with a five-part solution:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Pre-crash: log system info to disk&lt;/li&gt;&#10;&lt;li&gt;Crash time: set a callback to get notified, and capture stack to disk&lt;/li&gt;&#10;&lt;li&gt;Next app startup: transmit log+stack trace to server&lt;/li&gt;&#10;&lt;li&gt;[OPTIONAL] Server: symbolify stack trace&lt;/li&gt;&#10;&lt;li&gt;[OPTIONAL] Server: aggregate results&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Let’s take a quick look at each piece.&lt;/p&gt;&#10;&lt;h3 id="log-system-info"&gt;Log System Info&lt;/h3&gt;&#10;&lt;p&gt;When the crash happens, we want to do the bare minimum necessary. Memory may be corrupted and we want to avoid crashing while trying to log the crash. So: we log system information to disk at startup, prior to any crash happening. We particularly need the app version and the operating system version in order to be able to symbolify the stack trace, and the date/time of the session; but of course other data (screen size, RAM available, graphics driver versions, etc.) may also be relevant.&lt;/p&gt;&#10;&lt;h3 id="crash-capture-callback-on-crash"&gt;Crash Capture: Callback on Crash&lt;/h3&gt;&#10;&lt;p&gt;There are several potential mechanisms to get notified on crashes:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Assertion failures (if enabled in production): it’s easy enough to write a custom assertion macro that captures __LINE__ and__FILE__ to get the source location of a failure, and it can then also trigger capture of a stack trace.&lt;/li&gt;&#10;&lt;li&gt;Invalid memory access: the best bet seems to be &lt;code&gt;__try&lt;/code&gt; / &lt;code&gt;__catch&lt;/code&gt; / &lt;code&gt;_set_se_translator&lt;/code&gt; for Windows Structured Exception Handling (SEH)&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;These were also options, but didn’t seem to actually get called for relevant situations like null pointer dereferences:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;A different SEH Win32 C API: &lt;code&gt;SetUnhandledExceptionFilter&lt;/code&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;UWP event handlers: &lt;code&gt;Windows::ApplicationModel::Core::CoreApplication::UnhandledErrorDetected&lt;/code&gt; and &lt;code&gt;Windows::UI::Xaml::UnhandledException&lt;/code&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;What is SEH? It’s a C API that handles &lt;strong&gt;&lt;em&gt;hardware&lt;/em&gt;&lt;/strong&gt; exceptions on x86/x64 chips; while it uses the &lt;code&gt;__try&lt;/code&gt; and &lt;code&gt;__catch&lt;/code&gt; keywords, it’s &lt;strong&gt;&lt;em&gt;not&lt;/em&gt;&lt;/strong&gt; about software exceptions (either C++ or managed code). There&amp;rsquo;s some good discussion of it in the &lt;a href="http://crashrpt.sourceforge.net/docs/html/exception_handling.html"&gt;CrashRpt documentation&lt;/a&gt;. It’s a key part of Microsoft’s own error reporting mechanism, from the Dr. Watson era through to today. (I believe the rough data path is SEH → Windows Error Reporting (WER) → Microsoft-hosted database → Windows Store stack traces.) There are all sorts of complicated nuances to how it works, and I’ve understood… relatively few of them. I just use the _set_se_translator function as a hook to:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Get called when a crash happens, on the crashing thread.&lt;/li&gt;&#10;&lt;li&gt;Grab a stack trace and record it (typically skipping the first 8 frames that are in VCRUNTIME and NTDLL)&lt;/li&gt;&#10;&lt;li&gt;Also grab and record the exception type (EXCEPTION_ACCESS_VIOLATION, EXCEPTION_STACK_OVERFLOW, etc.)&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;A single call to _set_se_translator appears to be sufficient to get a callback when any thread crashes. (Which is the main reason I use it instead of __try / __catch or _set_terminate, which have to be applied on a per-thread basis.)&lt;/p&gt;&#10;&lt;p&gt;The actual implementation is pretty straightforward:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;#include &amp;lt;eh.h&amp;gt;&#10;&#10;void se_trans_func( unsigned int u, _EXCEPTION_POINTERS *pExp) {&#10; // Skip 8 stack frames - we usually see the ones below.&#10; // 00:MyApp.exe+0x009a7b98&#9;&#9;(this function)&#10; // 01 .. 05:VCRUNTIME140D_APP.dll&#10; // 06 .. 08:ntdll.dll&#10; Char *backTrace =GetBackTrace(8);&#10; Str desc;&#10; switch (u) {&#10; case EXCEPTION_ACCESS_VIOLATION:&#10; desc = L&amp;#34;ACCESS_VIOLATION&amp;#34;;&#9; break;&#10; case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:&#10; desc = L&amp;#34;ARRAY_BOUNDS_EXCEEDED&amp;#34;; break;&#10; // ... etc. ...&#10; case EXCEPTION_STACK_OVERFLOW:&#10; desc = L&amp;#34;STACK_OVERFLOW&amp;#34;; break;&#10; default:&#10; desc = StrPrintf(TEXT(&amp;#34;%d&amp;#34;), u); break;&#10; }&#10;&#10; LogPrintf(L&amp;#34;Structured Exception Handling failure type %s. Stack trace:\n%s\n&amp;#34;),&#10; desc.Data(), backTrace);&#10; delete[] backTrace;&#10;&#10; // A bit irrelevant... we mostly just want the stack trace.&#10; throw L”Hardware exception”;&#10;}&#10;&#10;App::App() {&#10; // ...&#10; _set_se_translator(se_trans_func);&#10; // ...&#10;}&#10;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="crash-capture-record-stack-trace"&gt;Crash Capture: Record Stack Trace&lt;/h3&gt;&#10;&lt;p&gt;From a UWP app, we don’t have access to many Win32 APIs - but we can call &lt;code&gt;CaptureStackBackTrace.&lt;/code&gt; If we combine that with calls to &lt;code&gt;RtlPcToFileHeader&lt;/code&gt; and &lt;code&gt;GetModuleName&lt;/code&gt;, then we can get the module name (EXE and DLL filename) and the offset within that module for each entry in the stack trace. Unlike conventional Win32 crash handlers, we cannot symbolify at crash time. We get a module-relative offset:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;myapp.exe+0x00001234&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Rather than the symbolified (actual function name and signature) with a function-relative offset:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;myapp.exe crashyFunction(int, const std::string &amp;amp;) + 0x0000001a&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Or even better, with source filenames and line numbers:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;myapp.exe crashyFunction(int, const std::string &amp;amp;) + 0x0000001a&#10;myapp.cpp:465&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In theory, you &lt;em&gt;could&lt;/em&gt; try to walk the module’s PE header data manually with an IMAGE_DOS_HEADER to get the IMAGE_EXPORT_DIRECTORY for the exported symbols from each module. But in practice, we need non-exported private function names, for both trace entries in our code and in Windows DLLs.&lt;/p&gt;&#10;&lt;p&gt;So - there’s no API call to do the symbolification locally in-app. We’ll have to do it out-of-app with the help of a server. Given that situation, we just dump the CaptureStackBackTrace outputs to disk, and let the app merrily crash.&lt;/p&gt;&#10;&lt;p&gt;The actual capture code looks like this:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;Str GetBackTrace(int SkipFrames)&#10;{&#10; constexpr uint TRACE_MAX_STACK_FRAMES = 99;&#10; void *stack[TRACE_MAX_STACK_FRAMES];&#10; ULONG hash;&#10; const int numFrames = CaptureStackBackTrace(SkipFrames + 1, TRACE_MAX_STACK_FRAMES, stack, &amp;amp;hash);&#10; Str result = StrPrintf(L&amp;#34;Stack hash: 0x%08lx\n&amp;#34;, hash);&#10; for (int i = 0; i &amp;lt; numFrames; ++i) {&#10; void *moduleBaseVoid = nullptr;&#10; RtlPcToFileHeader(stack[i], &amp;amp;moduleBaseVoid);&#10; auto moduleBase = (const unsigned char *)moduleBaseVoid;&#10; constexpr auto MODULE_BUF_SIZE = 4096U;&#10; wchar_t modulePath[MODULE_BUF_SIZE];&#10; const wchar_t *moduleFilename = modulePath;&#10; if (moduleBase != nullptr) {&#10; GetModuleFileName((HMODULE)moduleBase, modulePath, MODULE_BUF_SIZE);&#10; int moduleFilenamePos = Str(modulePath).FindLastOf(L&amp;#34;\\&amp;#34;);&#10; if (moduleFilenamePos &amp;gt;= 0)&#10; moduleFilename += moduleFilenamePos + 1;&#10; result += StrPrintf(L&amp;#34;%02d:%s+0x%08lx\n&amp;#34;), i, moduleFilename, &#10; (uint32)((unsigned char *)stack[i] - moduleBase));&#10; }&#10; else&#10; result += StrPrintf(L&amp;#34;%02d:%s+0x%016llx\n&amp;#34;), i, moduleFilename, &#10; (uint64)stack[i]);&#10; }&#10; return result;&#10;}&#10;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="next-app-startup-transmit-log"&gt;Next App Startup: Transmit Log&lt;/h3&gt;&#10;&lt;p&gt;The UWP lifecycle APIs let us detect whether the last run of the app had a clean exit. We can use that to detect a crash, and transmit the recorded log to our server for symbolification. It does mean that we don’t get crashes immediately, and we may miss crashes if the user doesn’t restart the app. But in practice, this is largely acceptable.&lt;/p&gt;&#10;&lt;p&gt;We do try to capture the app version and crash date/time in our log, as the version and date may be different by the time the log is transmitted.&lt;/p&gt;&#10;&lt;h3 id="optional-symbolify-stack-trace"&gt;[OPTIONAL] Symbolify Stack Trace&lt;/h3&gt;&#10;&lt;p&gt;The server is - by necessity - a Windows VM listening to HTTPS requests.&lt;/p&gt;&#10;&lt;p&gt;On the server, we maintain a set of PDB files for each app version (extracted from the &lt;code&gt;.msixpackage&lt;/code&gt; zip file). At present, we only handle x64 and not ARM64 crashes.&lt;/p&gt;&#10;&lt;p&gt;We also maintain a set of key system DLLs for each major (semiannual) Windows release. We don’t try to keep the DLLs for each minor release, as that’s basically a Herculean task.&lt;/p&gt;&#10;&lt;p&gt;When a log file comes in, we:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Parse the log to retrieve the app version and Windows version&lt;/li&gt;&#10;&lt;li&gt;Match up the relevant PDB file and directory of Windows system DLLs&lt;/li&gt;&#10;&lt;li&gt;For each line of the stack trace, detect whether it’s in our app or a system DLL&lt;/li&gt;&#10;&lt;li&gt;Run CDB.EXE to convert the module name + offset to a function name, offset, source filename and line number. The inspiration for this came from &lt;a href="http://bytepointer.com/resources/old_new_thing/20131115_302_restoring_symbols_to_a_stack_trace_originally_generated_without_symbols.htm"&gt;Raymond Chen’s oldnewthing blog&lt;/a&gt;.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;The heart of this is the call to CDB, which looks like this:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;export CDB=/mnt/c/Progra\~2/Windows\ Kits/10/Debuggers/x64/cdb.exe&#10;# Typical raw output, prior to sed expressions:&#10;# MyApp!MyApp::App::CrashyFunction+0x25fb&#10;# [C:\Users\MyUser\source\myapp\MyApp.cpp @ 757]:&#10;# Sed expressions simplify it to:&#10;# App::CrashyFunction+0x25fb [MyApp.cpp @ 757]&#10;&amp;#34;$CDB&amp;#34; -lines -z myapp.exe -c &amp;#34;u $entry; q&amp;#34; -y \&#10; &amp;#34;cache*c:\\Symbols;srv*https://msdl.microsoft.com/download/symbols&amp;#34;\&#10; | grep -m 1 -A 1 &amp;#34;^0:000&amp;gt;&amp;#34; \&#10; | tail -1 \&#10; | sed &amp;#39;s/:$//;&amp;#39; \&#10; | sed &amp;#39;s/MyApp:://g;&amp;#39; \&#10; | sed &amp;#39;s/\[[^@[]*\\\([A-Za-z0-9_.]*.\(h\|cpp\) @ \)/\[\1/g&amp;#39;&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For Windows system DLL entries, we can choose a $SYS32DIR to match the client&amp;rsquo;s Windows version, and then change the -z argument to&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;-z $SYS32DIR\\$DLLFILENAME&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This ensures that CDB uses the &lt;em&gt;client’s&lt;/em&gt; Windows version to decide which symbols to retrieve for the DLL, rather than the normal behaviour, which would use the &lt;em&gt;server VM&lt;/em&gt;’s Windows version. We’ll still get a fair bit of mismatch - as we usually don’t have the DLLs that precisely match the client’s Windows version.&lt;/p&gt;&#10;&lt;p&gt;If there’s some way to specify a Windows version when telling CDB about the symbol server - let me know! That would be a huge timesaver.&lt;/p&gt;&#10;&lt;h3 id="optional-aggregate-results"&gt;[OPTIONAL] Aggregate results&lt;/h3&gt;&#10;&lt;p&gt;Ideally, we want to build a database of all stack traces, merge duplicates, and present a ranked list of the most common crashes over (say) a month, and a graph of the crash frequency of any given bug per day.&lt;/p&gt;&#10;&lt;p&gt;But I haven’t actually implemented this yet. For this purpose, an off-the-shelf tool will probably suffice - a service like Sentry or Visual Studio App Center would do the trick, and both accept submission of crash data via an API.&lt;/p&gt;&#10;&lt;h3 id="what-if-we-dont-symbolify"&gt;What if we don’t symbolify?&lt;/h3&gt;&#10;&lt;p&gt;If you want a simpler solution: you can easily use the module name + module-relative offset in a Visual Studio debugger session to manually find the source location. This makes it painstaking to analyze each individual crash, but might be acceptable if you maintain a very low crash rate in your app. I took this route through 2019, but added stack trace capture by 2020 and finally built out the symbolification code in 2021 when it became obvious that Microsoft wasn’t going to fix this themselves.&lt;/p&gt;&#10;&lt;h3 id="conclusion"&gt;Conclusion&lt;/h3&gt;&#10;&lt;p&gt;So - this is a very bare bones process for detecting crashes, capturing a C++ stack trace, transmitting to a server and determining filename + line numbers for each entry in the stack trace.&lt;/p&gt;&#10;&lt;p&gt;I … still really wish Microsoft would just re-enable this code in the Microsoft Store. For the life of me, I can’t understand why they turned it off. They must still be collecting all this information in the WER database for their own use, and just not exposing it to developers.&lt;/p&gt;&#10;</description></item><item><title>Pitfalls mixing PPL+await</title><link>https://davidpritchard.org/archives/902/</link><pubDate>Wed, 31 Mar 2021 16:08:57 +0000</pubDate><guid>https://davidpritchard.org/archives/902/</guid><description>&lt;p&gt;Imagine you have a C++/CX codebase with a lot of asynchronous logic written using the classic &lt;a href="https://docs.microsoft.com/en-us/cpp/parallel/concrt/parallel-patterns-library-ppl?view=msvc-160"&gt;Parallel Patterns Library&lt;/a&gt; (PPL) &lt;a href="https://docs.microsoft.com/en-us/cpp/parallel/concrt/parallel-patterns-library-ppl?view=msvc-160"&gt;tasks&lt;/a&gt;:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;task&amp;lt;void&amp;gt; FooAsync(StorageFolder ^folder) {&#10; return create_task(folder-&amp;gt;TryGetItemAsync(L&amp;#34;foo.txt&amp;#34;))&#10; .then([](IStorageItem ^item) {&#10; // do some stuff, cast to a file, etc.&#10; });&#10;}&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It&amp;rsquo;s all running on the UI thread, so it&amp;rsquo;s in a Single Threaded Apartment, and each task in your chain of &lt;code&gt;.then()&lt;/code&gt; continuations is guaranteed to also be on the UI thread.&lt;/p&gt;&#10;&lt;p&gt;Now, you learn about Microsoft&amp;rsquo;s relatively new &lt;code&gt;co_await&lt;/code&gt; feature, recently accepted in an adapted form in the C++20 spec. So you start using it:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;task&amp;lt;void&amp;gt; FooAsyncCo(StorageFolder ^folder) {&#10; IStorageItem ^item =&#10; co_await folder-&amp;gt;TryGetItemAsync(L&amp;#34;foo.txt&amp;#34;);&#10; // do some stuff, cast to a file, etc.&#10;}&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So far so good: all code inside &lt;code&gt;FooAsyncCo()&lt;/code&gt; also runs on the UI thread, nice and clean.&lt;/p&gt;&#10;&lt;p&gt;Finally, you integrate a little of this &lt;code&gt;co_await&lt;/code&gt; code into your heavily PPL codebase. Helpfully, &lt;code&gt;co_await&lt;/code&gt; returns a task, just like your existing async PPL method. Another developer sees that you have a bunch of methods returning task&lt;void&gt; and decides to start building on that API using PPL:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;FooAsyncCo().then([]() {&#10; // more stuff, after coroutine.&#10; // KABOOM. Running on a threadpool thread, *not* the UI thread.&#10; // We accidentally busted out of the STA.&#10;});&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Uh-oh. When you mix the two async approaches&amp;hellip; things break. And none of the documentation - or honestly anything I&amp;rsquo;ve read on the web to date - gives any hint of this issue.&lt;/p&gt;&#10;&lt;h2 id="not-a-fix"&gt;Not A Fix&lt;/h2&gt;&#10;&lt;p&gt;Well&amp;hellip; let&amp;rsquo;s see. The docs do give us one tip: a task chain is only guaranteed to remain inside the Single Threaded Apartment &lt;strong&gt;&lt;em&gt;if&lt;/em&gt;&lt;/strong&gt; the chain starts with an &lt;code&gt;IAsyncAction&lt;/code&gt; or &lt;code&gt;IAsyncOperation&lt;/code&gt;&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;The UI of a UWP app runs in a single-threaded apartment (STA). A task whose lambda returns either an IAsyncAction or IAsyncOperation is apartment-aware. If the task is created in the STA, then all of its continuations will run also run in it by default, unless you specify otherwise. In other words, the entire task chain inherits apartment-awareness from the parent task. This behavior helps simplify interactions with UI controls, which can only be accessed from the STA.&lt;/p&gt;&#10;&lt;p&gt;&lt;a href="https://docs.microsoft.com/en-us/windows/uwp/threading-async/asynchronous-programming-in-cpp-universal-windows-platform-apps#managing-the-thread-context"&gt;Asynchronous programming in C++/CX: Managing the Thread Context&lt;/a&gt;&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;Well&amp;hellip; &lt;code&gt;TryGetItemAsync&lt;/code&gt; does return an &lt;code&gt;IAsyncOperation&lt;/code&gt;. That would appear to be the root task in the chain&amp;hellip; so coroutines must be treated differently, with the coroutine itself being the root.&lt;/p&gt;&#10;&lt;p&gt;Well, what if we tried making the coroutine return an &lt;code&gt;IAsyncAction&lt;/code&gt;?&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;IAsyncAction ^FooAsyncCoAction(StorageFolder ^folder) {&#10; return create_async([folder]() -&amp;gt; task&amp;lt;void&amp;gt; {&#10; // KABOOM - this is now out of the apartment.&#10; IStorageItem ^item =&#10; co_await folder-&amp;gt;TryGetItemAsync(L&amp;#34;foo.txt&amp;#34;);&#10; // do some stuff, cast to a file, etc.&#10; });&#10;}&#10;create_task(FooAsyncCo())&#10;.then([]() {&#10; // more stuff, after coroutine.&#10; // ok now!&#10;});&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;No dice. Now the coroutine runs off-thread, while the continuation runs correctly on the UI thread.&lt;/p&gt;&#10;&lt;h2 id="two-actual-fixes"&gt;Two Actual Fixes&lt;/h2&gt;&#10;&lt;p&gt;With a little more reading, I found Raymond Chen&amp;rsquo;s &lt;a href="https://devblogs.microsoft.com/oldnewthing/20180801-00/?p=99385"&gt;blog post&lt;/a&gt; about PPL and apartment-aware tasks. That led to this successful solution:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;task&amp;lt;void&amp;gt; completed_apartment_aware_task() {&#10; return create_task(create_async([](){}));&#10;}&#10;&#10;completed_apartment_aware_task()&#10;.then(FooAsyncCo)&#10;.then([]() {&#10; // Ok!&#10;});&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This one actually works. By rooting the PPL chain in an &lt;code&gt;IAsyncAction&lt;/code&gt;, the rest of the chain retains apartment awareness, and everything stays on the UI thread.&lt;/p&gt;&#10;&lt;p&gt;And I&amp;rsquo;d earlier found a different but more fragile solution:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;create_task(FooAsyncCo, task_continuation_context::use_current())&#10;.then([]() {&#10; // Ok!&#10;});&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;While this works, it&amp;rsquo;s&amp;hellip; a bit hard to tell whether the root task, or the continuation, or what-all needs &lt;code&gt;use_current()&lt;/code&gt;, and it&amp;rsquo;s always felt fragile to me. And if it gets called &lt;strong&gt;&lt;em&gt;from&lt;/em&gt;&lt;/strong&gt; a threadpool thread, it&amp;rsquo;s unclear to the caller what happens with &lt;code&gt;use_current().&lt;/code&gt;&lt;/p&gt;&#10;&lt;h2 id="conclusion"&gt;Conclusion&lt;/h2&gt;&#10;&lt;p&gt;I&amp;rsquo;ve got to say&amp;hellip; this is a brutal pitfall. Any existing codebase is going to have a lot of PPL tasks in it, and wholesale migration to co_await isn&amp;rsquo;t going to happen all in one go, at least if there&amp;rsquo;s any conditional/loop/exception logic in the PPL-based code. Anyone who&amp;rsquo;s tried to migrate to &lt;code&gt;co_await&lt;/code&gt; has probably run into this pitfall.&lt;/p&gt;&#10;&lt;p&gt;We haven&amp;rsquo;t adopted the &lt;code&gt;completed_apartment_aware_task()&lt;/code&gt; as a root task throughout our codebase yet, but I&amp;rsquo;m hopeful that will at least offer a path forward. Just&amp;hellip; still quite error-prone.&lt;/p&gt;&#10;</description></item><item><title>Keep Calm and Mix On</title><link>https://davidpritchard.org/archives/keep-calm-and-mix-on/</link><pubDate>Tue, 23 Jun 2020 22:33:02 +0000</pubDate><guid>https://davidpritchard.org/archives/keep-calm-and-mix-on/</guid><description>&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/keep-calm-and-mix-on/images/cover-300x300.jpg" alt="" loading="lazy"&gt;&lt;/p&gt;&#10;&lt;p&gt;I listen to music while I work. I listen without lyrics. I mostly just consume what the Apple algorithm feeds me.&lt;/p&gt;&#10;&lt;p&gt;So, I&amp;rsquo;m not going to pretend any ownership over this list. It&amp;rsquo;s really just a &amp;ldquo;best of Apple music, 2018/19.&amp;rdquo; For the record, the stock playlists I prefer are: Untitled, Loops, Headspace, Living in the Library and Vibes.&lt;/p&gt;&#10;&lt;p&gt;But: at least I&amp;rsquo;m listening mostly to music from the past decade, and finding rock and r&amp;amp;b tracks that I like. The flavour of this mix is very low key, suitable to my mood when I put it together in February. It doesn&amp;rsquo;t really fit with the &amp;ldquo;brief relief from a coronavirus&amp;rdquo; that we&amp;rsquo;re going through now, but whatever.&lt;/p&gt;&#10;&lt;p&gt;I&amp;rsquo;ll be honest: no one else listens to these mixes. I make them mostly for myself, as a way to sift through the best things I&amp;rsquo;ve heard, take note of the artists I liked best, and branch out to their full albums and other work. The raw material for this mix is a solid 200 tracks that caught my eye, and it was fun to cull them down to this highlights list.&lt;/p&gt;&#10;&lt;p&gt;Download a zip of MP3s here, or listen with your favourite streaming service below.&lt;/p&gt;&#10;&lt;iframe loading="lazy" allow="autoplay *; encrypted-media *; fullscreen *; clipboard-write" frameborder="0" height="450" style="width:100%;max-width:660px;overflow:hidden;background:transparent;" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-storage-access-by-user-activation allow-top-navigation-by-user-activation" src="https://embed.music.apple.com/ca/playlist/2020-mix/pl.u-ZmbllpatzP2oxl"&gt;&lt;/iframe&gt;&#10;&lt;iframe loading="lazy" src="https://open.spotify.com/embed/playlist/09Ll1PjXOMCAmuGtIEdssn?si=pRDHBw6lS5yzgBuPFraWYA" width="300" height="170" frameborder="0" allowtransparency="true" allow="encrypted-media"&gt;&lt;/iframe&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;james blake&lt;/strong&gt; feat. rosalía. barefoot in the park. pop, 2019.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;nicolas jaar&lt;/strong&gt;. mi mujer. dub, 2010.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;maribou state&lt;/strong&gt;. turnmills [edit]. downtempo, 2018.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;pantha du prince&lt;/strong&gt;. bohemian forest. techno, 2010.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;chet faker&lt;/strong&gt;. no diggity. r&amp;amp;b, 2012.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;calvin harris&lt;/strong&gt; feat. frank ocean &amp;amp; migos. slide. r&amp;amp;b, 2017.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;allan rayman&lt;/strong&gt; feat. jessie reyez. repeat. r&amp;amp;b, 2017.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;art school girlfriend.&lt;/strong&gt; distance (blank). rock, 2018.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;alex lahey.&lt;/strong&gt; am i doing it right? rock, 2019.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;cherry glazerr&lt;/strong&gt;. daddi. rock, 2019.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;two people&lt;/strong&gt;. something to talk about. trip hop, 2018.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;barr brothers&lt;/strong&gt;. you would have to lose your mind. rock, 2017.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;ben howard&lt;/strong&gt;. nica libres at dusk. rock, 2018.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;reuben bidez&lt;/strong&gt; feat. molly parden. what you really wanted. folk, 2018.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;kimya dawson&lt;/strong&gt;. tire swing. folk, 2006.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;wildes&lt;/strong&gt;. illuminate. rock, 2016.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;dusted&lt;/strong&gt;. all i am. rock, 2018.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;bearcubs&lt;/strong&gt;. do you feel. electronic, 2018.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;velvet negroni&lt;/strong&gt;. crybaby. electronic, 2018.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;manila killa &amp;amp; mansionair&lt;/strong&gt;. wake up call. pop, 2018.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;bicep&lt;/strong&gt;. vale. techno, 2017.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;major lazer&lt;/strong&gt; feat. amber coffman. get free. dancehall, 2012.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;floating points&lt;/strong&gt;. anasickmodular. acid jazz, 2019.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;martin nonstatic&lt;/strong&gt;. innermost structure. ambient, 2017.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;orbital&lt;/strong&gt;. tiny foldable cities [edit]. techno, 2018.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;dining rooms&lt;/strong&gt;. you [quantic soul orchestra version]. acid jazz, 2004.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;alex lustig&lt;/strong&gt; feat. akacia. in the end. pop, 2018.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;</description></item><item><title>A Windows to iOS Port</title><link>https://davidpritchard.org/archives/871/</link><pubDate>Mon, 15 Jun 2020 21:37:48 +0000</pubDate><guid>https://davidpritchard.org/archives/871/</guid><description>&lt;p&gt;My main activity of 2019 was a port of the &lt;a href="https://www.mentalcanvas.com"&gt;Mental Canvas&lt;/a&gt; 3D drawing tool from Windows to iOS. About 75% of my effort was user interface code, maybe 15% on graphics and 10% other platform issues.&lt;/p&gt;&#10;&lt;p&gt;What&amp;rsquo;s interesting about our porting approach? Well, it&amp;rsquo;s just an unusual mix:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;It&amp;rsquo;s a UWP (Universal Windows Platform) app. UWP was Microsoft&amp;rsquo;s effort to modernize Win32 and add iOS mobile paradigms, so it is already touch-centric, relatively sandboxed and has a more mobile-like lifecycle&lt;/li&gt;&#10;&lt;li&gt;We didn&amp;rsquo;t use React Native, Flutter, Electron, Xamarin, Qt or even Fuschia&amp;rsquo;s namesake &lt;a href="https://en.wikipedia.org/wiki/Taligent#Pink_systemhttps://en.wikipedia.org/wiki/Taligent#Pink_system"&gt;Pink&lt;/a&gt;. We just&amp;hellip; wrote some native code. And a lightweight abstraction layer.&lt;/li&gt;&#10;&lt;li&gt;We completed the porting effort in seven months, with three developers.&lt;/li&gt;&#10;&lt;li&gt;We stayed with the MVVM architecture that is the norm on Windows. Many iOS developers would call that&amp;hellip; &lt;a href="https://ashfurrow.com/blog/mvvm-is-exceptionally-ok/"&gt;exceptionally ok&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;We didn&amp;rsquo;t use the latest and greatest Swift tools, just plain old C++11 and Objective-C for the most part. We didn&amp;rsquo;t switch to SwiftUI when it was released half way through our port.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;What combination of circumstances led to this approach? Mostly just a real life constraint: a desire to rewrite/redesign as little working code as possible, and keep the codebase&amp;rsquo;s size down.&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;a href="#StartingPoint"&gt;Starting Point&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="#ui-framework-choice"&gt;UI Framework Choice&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="#restructuring-ui"&gt;Restructuring the UI Thread&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="#ios-bindings"&gt;Lightweight iOS Bindings&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="#what-about-swiftui"&gt;What About SwiftUI?&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="#incremental-porting"&gt;Incremental Porting&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="#upshot-code-reuse"&gt;The Upshot: Code Reuse&lt;/a&gt;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h2 id="starting-point"&gt;Starting Point&lt;/h2&gt;&#10;&lt;p&gt;We architected our app to be portable from day one, using a C++ games engine that had previously shipped for Windows, Linux, iOS and Android. It&amp;rsquo;s a professional 3D illustration tool and native user interface elements are expected, so an architecture was chosen to keep much of the application portable while still allowing quick development of a native UI.&lt;/p&gt;&#10;&lt;p&gt;At the outset of the port, our app was only running on Windows. From a portability standpoint there were three main considerations:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;User interface&lt;/strong&gt;: the Windows UI logic was sound, but&amp;mdash;as it was written in C++/CX&amp;mdash;was not immediately portable to iOS&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Graphics API&lt;/strong&gt;: Apple deprecated OpenGL 18 months prior to the start of our port, although they ultimately elected to allow it to survive 2019&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Platform&lt;/strong&gt;: threading, file I/O, app lifecycle, networking and async primitives/APIs would all need some level of revamping. While a C++ app would normally be able to easily migrate file I/O, early UWP versions forced us to use platform-specific async I/O APIs&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;I&amp;rsquo;m going to focus on the user interface in this article, as that was the single largest hurdle that we faced, and the bulk of my effort. Before I get there, it&amp;rsquo;s worth a quick glance at each portion of our user interface structure:&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/871/images/MC-arch-1024x1024.png" alt="" loading="lazy"&gt;&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;Model:&lt;/strong&gt; the render thread holds the model (i.e., the contents of a 3D scene)&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;ViewModel:&lt;/strong&gt; the UI thread maintains a mirror copy of relevant parts of the model as a C++/CX object, plus some UI state. This allows use of simple binding between the View and ViewModel properties.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;View:&lt;/strong&gt; native UI controls and their responsive layout are defined declaratively in XAML, with bindings to ViewModel properties established at compile-time&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;There is almost no shared memory across threads. Communication between threads is done using a dedicated incoming message queue for each thread.&lt;/p&gt;&#10;&lt;h2 id="ui-framework-choice"&gt;UI Framework Choice&lt;/h2&gt;&#10;&lt;p&gt;Should we use a cross-platform framework? We had a few unusual considerations&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;Platforms:&lt;/strong&gt; the combination of desktop (UWP) and mobile (iOS) narrows our choices, and we would like to protect for a future MacOS port (with Wacom tablet for input)&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Input devices:&lt;/strong&gt; first-class pen support is unusual, particularly on mobile-focused frameworks&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;And some more common considerations in games or other apps:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;Look and feel:&lt;/strong&gt; a &amp;ldquo;fun&amp;rdquo; feel is key to our app, and benefits from an &amp;ldquo;easy, responsive&amp;rdquo; user interface and native feel.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Graphics:&lt;/strong&gt; UI framework must be lightweight, and co-operate and overlay cleanly over a 60 fps (or even 120 fps) graphics thread&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Memory:&lt;/strong&gt; our app is quite memory intensive, especially for mobile devices with only 3GB of RAM&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;We evaluated a number of frameworks before proceeding. The main frameworks under consideration were React Native, Xamarin and Unity. React Native was the strongest contender, with a Microsoft-maintained UWP port, pen support feasible (with a little effort), and a declarative/reactive UI language that was nicer than native UIKit&amp;rsquo;s imperative design. We built a quick, working prototype of our app in React Native to get a feel for the technology&amp;rsquo;s strengths and weaknesses.&lt;/p&gt;&#10;&lt;p&gt;In the end, though, we chose a native port. The main downsides that we saw in React Native were:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;no MacOS port&lt;/li&gt;&#10;&lt;li&gt;a less &amp;ldquo;native&amp;rdquo; feel&lt;/li&gt;&#10;&lt;li&gt;risk from one additional vendor dependency (Facebook) and one new technology dependency (Microsoft UWP port)&lt;/li&gt;&#10;&lt;li&gt;complex toolchain&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;And as a more short-term issue, it was worth considering our staff&amp;rsquo;s lack of React experience. There&amp;rsquo;s always a fair bit of risk when you can&amp;rsquo;t &amp;ldquo;ease in&amp;rdquo; to a technology, and have to go straight to architecture without much concrete experience.&lt;/p&gt;&#10;&lt;h2 id="restructuring-ui"&gt;Restructuring the UI thread&lt;/h2&gt;&#10;&lt;p&gt;As shown in the earlier figure, the non-portable UI code consisted of both ViewModel (~15%) and View code (~10%). The View code is unavoidably native, but what about the ViewModel code? ViewModels manage communication with the Model, which should be entirely portable; and they expose properties for the View to bind against&amp;hellip; which depends on the platform binding mechanism.&lt;/p&gt;&#10;&lt;p&gt;So&amp;hellip; let&amp;rsquo;s think about those bindings. Each ViewModel has a set of member variables that are &amp;ldquo;bindable properties&amp;rdquo;, and a notification mechanism when a change happens. In our app, we almost always use one-way bindings, and we use commands used to modify any ViewModel properties. Our UI is relatively simple, with limited animation and only a modest amount of content. We had already adopted a declarative mindset for the UI, with the aim of ensuring a single source of truth for the UI state.&lt;/p&gt;&#10;&lt;p&gt;The main issues we faced in making this portable were:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Windows: the &lt;code&gt;{x:Bind}&lt;/code&gt; mechanism requires the ViewModel to be a native object (in C++/CX, a &lt;a href="https://docs.microsoft.com/en-us/cpp/cppcx/ref-classes-and-structs-c-cx?view=vs-2019"&gt;ref class&lt;/a&gt;), and each bindable property has to be a special &lt;a href="https://docs.microsoft.com/en-us/cpp/cppcx/properties-c-cx?view=vs-2019"&gt;special native type&lt;/a&gt;.&lt;/li&gt;&#10;&lt;li&gt;iOS: at the time of port, there was no native binding mechanism. (SwiftUI was released halfway through our port effort.) We would have to roll our own.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;None of this was insurmountable, and we were ultimately successful in making the ViewModel entirely portable, leaving only the View code platform-dependent.&lt;/p&gt;&#10;&lt;h2 id="ios-bindings"&gt;Lightweight iOS Bindings&lt;/h2&gt;&#10;&lt;p&gt;As a newcomer to Apple development, I found it curious that Cocoa Bindings existed on MacOS, but were never ported to iOS. Apparently they were &lt;a href="https://mjtsai.com/blog/2014/03/16/the-siren-call-of-kvo-and-cocoa-bindings/"&gt;slow and fragile&lt;/a&gt;, but&amp;hellip; so are all of the awful hacks people seem to do in UIKit to keep their UI in sync.&lt;/p&gt;&#10;&lt;p&gt;At any rate, it proved fairly simple to write a few methods that simply track a list of source-target pair:&lt;/p&gt;&#10;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Source&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;TARGET&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;ViewModel + Property name&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;UIView + Keypath (KVO)&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;e.g., Layer ViewModel + Opacity&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;e.g., UIStackView + @&amp;ldquo;opacitySlider.value&amp;rdquo;&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;p&gt;With some kind of notification/pub-sub system, we can watch for VM property changes, and call the UIView&amp;rsquo;s &lt;code&gt;setValue:forKeyPath:&lt;/code&gt; selector to update the UI accordingly. This hand-rolled binding system is not as robust or anywhere near as full-featured as a real binding system, but it is portable and does the trick for our simple user interface.&lt;/p&gt;&#10;&lt;p&gt;There are two little niceties that I may elaborate in a separate post:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Compile-time keypath validity checks: borrowed from &lt;a href="https://web.archive.org/web/20190408182555/http://forgecode.net/2011/11/compile-time-checking-of-kvc-keys/"&gt;Nick Forge&amp;rsquo;s post&lt;/a&gt;, to catch typos earlier and avoid a compile + run cycle.&lt;/li&gt;&#10;&lt;li&gt;Multi-bindings, boolean ops, and type conversions: various ways to combine/convert multiple ViewModel properties prior to binding to a UIView property.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Suffice it to say that the final syntax is something like this:&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;m_bindings-&amp;gt;add&lt;/code&gt;(&lt;br&gt;&#10;&lt;code&gt;BIND_CHECK2(modeSegmented, setSelectedSegmentTintColor),&lt;/code&gt;&lt;br&gt;&#10;&lt;code&gt;VmFn::IfElse(m_vm-&amp;gt;IsNavMode, SNavModeColor, SDrawModeColor));&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;which is a continuously updating version of&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;self.modeSegmented.selectedSegmentTintColor = m_vm-&amp;gt;IsNavMode ? SNavModeColor : SDrawModeColor;&lt;/code&gt;&lt;/p&gt;&#10;&lt;h2 id="what-about-swiftui"&gt;What About SwiftUI?&lt;/h2&gt;&#10;&lt;p&gt;While I agree that &lt;a href="https://ashfurrow.com/blog/mvvm-is-exceptionally-ok/"&gt;MVVM is exceptionally ok&lt;/a&gt;, SwiftUI is clearly the &amp;ldquo;next stop&amp;rdquo; that Ash Furrow imagined. SwiftUI is a great fit for our architecture: bindings, declarative, relatively low-state, and the ability to use Combine to maintain the dependencies between different properties.&lt;/p&gt;&#10;&lt;p&gt;However, we haven&amp;rsquo;t pursued SwiftUI yet for a few reasons:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;strong&gt;Swift interoperability with C++ remains painful.&lt;/strong&gt; We would need to maintain a lot of Objective C code to bridge our C++ data structures over to Swift&amp;hellip; unless &lt;a href="https://forums.swift.org/t/c-objective-c-interop/9989"&gt;things change some day&lt;/a&gt;.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Ship Timing:&lt;/strong&gt; our ship date was September 2019 with a target of iOS11 and up. It&amp;rsquo;ll be a little while before we can limit the software to iOS 13 only.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Development Timing:&lt;/strong&gt; we had already ported 60% of the app by the time SwiftUI was announced. We&amp;rsquo;d heard the Catalyst rumours and &lt;em&gt;very faint&lt;/em&gt; mentions of Amber ahead of WWDC, but there was nothing we could rely on.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;I&amp;rsquo;m sure that we &lt;em&gt;could&lt;/em&gt; use Objective C++ to bridge our C++ ViewModel data over to a SwiftUI-friendly data structure. Once enough of the market is on iOS13, we&amp;rsquo;ll face the following trade-off:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;Pro:&lt;/strong&gt; Accelerated UI iteration using SwiftUI vs. UIKit. Less AutoLayout agony.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Con:&lt;/strong&gt; Pain of maintaining an Objective C++ bridge between C++11 ViewModels and Swift Views&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Once Apple inevitably starts making some UI controls Swift-only, the decision will probably tip in favour of SwiftUI.&lt;/p&gt;&#10;&lt;p&gt;At the end of the day&amp;hellip; if you&amp;rsquo;re going for portable, you need to use portable languages. Swift deliberately ain&amp;rsquo;t that; it&amp;rsquo;s Apple&amp;rsquo;s ecosystem accelerator and lock-in mechanism.&lt;/p&gt;&#10;&lt;h2 id="incremental-porting"&gt;Incremental Porting&lt;/h2&gt;&#10;&lt;p&gt;We had the good fortune of having a relatively well-composed set of ViewModels. While it was once a single massive ViewModel, steady refactors broke it into about 10 major ViewModels corresponding to each of the major on-screen UI elements.&lt;/p&gt;&#10;&lt;p&gt;One of the most pleasing benefits of that factoring was that we could port them over one at a time, in priority order. At any stage during the port, we could have:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;an iOS version that was demoable with a subset of UI&lt;/li&gt;&#10;&lt;li&gt;a Windows version that still compiled and worked 100%&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Towards the end of the port, business demands forced us to put all resources into getting the iOS version ready, at the cost of maintaining a fully working Windows build, but we paid that technical debt down promptly.&lt;/p&gt;&#10;&lt;h2 id="upshot-code-reuse"&gt;The Upshot: Code Reuse&lt;/h2&gt;&#10;&lt;p&gt;Here are the architecture diagrams after completing the port.&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/871/images/MC-port-both-1024x922.png" alt="" loading="lazy"&gt;&lt;/p&gt;&#10;&lt;p&gt;And here&amp;rsquo;s an analysis of the source code size before and after the port:&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/871/images/Porting-MentalCanvas.png" alt="" loading="lazy"&gt;&lt;/p&gt;&#10;&lt;p&gt;Not too shabby. And note that a lot of that portable ViewModel code was not really a rewrite: it was more of a line-by-line translation from one dialect to another.&lt;/p&gt;&#10;&lt;p&gt;A few statistics:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;The size of the Windows build grew by 5% due to the port.&lt;/li&gt;&#10;&lt;li&gt;Windows-specific code was 25,000 lines before the port. Only 14,000 lines of iOS-specific code was required with this platform-independent framework.&lt;/li&gt;&#10;&lt;li&gt;If we hadn&amp;rsquo;t ported, and instead had 30,000 lines of iOS-specific code (including Metal backend), we&amp;rsquo;d probably be looking at a 110,000 line app. So we likely reduced our codebase size by about 10% using this approach.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;In the iOS app, we can run with either an OpenGL ES 2.0 backend or a Metal backend. Given the much better Metal performance, we don&amp;rsquo;t intend to ship the OpenGL ES version, but it can be useful to have it occasionally to isolate bugs or to test in the iOS Simulator without MacOS Catalina.&lt;/p&gt;&#10;&lt;p&gt;So there you have it. Sometimes, doing a port the old way is a reasonable choice. No bragging rights for using the latest technologies, but&amp;hellip; relatively few new bugs.&lt;/p&gt;</description></item><item><title>Metal Memory Debugging</title><link>https://davidpritchard.org/archives/838/</link><pubDate>Wed, 27 Nov 2019 18:03:52 +0000</pubDate><guid>https://davidpritchard.org/archives/838/</guid><description>&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/838/images/spike.png" alt="" loading="lazy"&gt;At &lt;a href="https://www.mentalcanvas.com"&gt;Mental Canvas&lt;/a&gt;, &lt;a href="https://github.com/mealworms"&gt;Dave Burke&lt;/a&gt; and I recently resolved a memory leak bug in our iOS app using the Metal graphics libraries. Memory management on Metal has had a few good WWDC talks but has surprisingly little chatter on the web, so I thought I&amp;rsquo;d share our process and findings.&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;a href="#issue"&gt;The issue: a transient memory spike&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://davidpritchard.org/archives/838#memorylimits"&gt;iOS / iPadOS memory limits&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://davidpritchard.org/archives/838#hypotheses"&gt;Forming hypotheses&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://davidpritchard.org/archives/838#tools"&gt;Tools at hand&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://davidpritchard.org/archives/838#findings"&gt;Findings&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://davidpritchard.org/archives/838#conclusion"&gt;Conclusion&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;References&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h3 id="issue"&gt;The issue: a transient memory spike&lt;/h3&gt;&#10;&lt;p&gt;Our sketching app does a lot of rendering to intermediate, offscreen textures. In the process, we generate (and quickly discard) a lot of intermediate textures. A lot of that rendering happens in a continuous batch on a dedicated thread, without yielding execution back to the operating system.&lt;/p&gt;&#10;&lt;p&gt;A consequence of that behavior: our memory footprint grows rapidly until we hit the critical threshold — on a 3 GB iPad Air, about 1.8 GB — and then iOS summarily kills the app. From our app&amp;rsquo;s perspective, memory footprint is only growing gradually, as most of the memory usage is temporary intermediate rendertargets; they should be freed and available for reuse. But from the operating system&amp;rsquo;s perspective, it considers all of those temporaries to still be in use.&lt;/p&gt;&#10;&lt;p&gt;Which led us to the question: why? And how do we figure out which temporaries are leaking, and which Metal flag / call will allow us to return that data to the operating system?&lt;/p&gt;&#10;&lt;h3 id="memorylimits"&gt;iOS / iPadOS memory limits&lt;/h3&gt;&#10;&lt;p&gt;From our empirical reviews of the various tools, we can say the following:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;The XCode Memory Gauge precisely reflects what the operating system thinks your app&amp;rsquo;s size is (as advertised in many WWDC talks).&lt;/li&gt;&#10;&lt;li&gt;The great new &lt;code&gt;[os_proc_available_memory()](https://developer.apple.com/documentation/os/3191911-os_proc_available_memory?language=objc)&lt;/code&gt; call in iOS13 appears to precisely match the XCode Memory Gauge. A delta between &lt;code&gt;os_proc_available_memory()&lt;/code&gt; at app startup and later in the app represents memory consumption in the same manner that the OS sees it.&lt;/li&gt;&#10;&lt;li&gt;The operating system seems to kill an app once its usage hits roughly system memory minus 1GB&#10;&lt;ul&gt;&#10;&lt;li&gt;Test systems: a 3GB iPad Air (2019) and a 4GB iPad Pro (2018), running both iOS12 and iOS13&lt;/li&gt;&#10;&lt;li&gt;API reported RAM: 2.8GB and 3.6GB respectively&lt;/li&gt;&#10;&lt;li&gt;Crashes at 1.8GB and 2.7GB memory usage&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;Be cautious of other memory metrics: neither XCode&amp;rsquo;s &lt;em&gt;GPU Frame Capture&lt;/em&gt; memory view, the &lt;em&gt;Metal System Trace&lt;/em&gt; Instrument nor the &lt;em&gt;Memory Graph&lt;/em&gt; Instrument appear to give totals that line up cleanly with the XCode Memory Gauge.&lt;/li&gt;&#10;&lt;li&gt;Ultimately, I got &lt;em&gt;GPU Frame Capture&lt;/em&gt; to give me great data — but with an awareness of &lt;em&gt;when&lt;/em&gt; its snapshot is taken, which is typically at the end of rendering a visible frame, and &lt;em&gt;after&lt;/em&gt; some operating system activity has happened.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h3 id="hypotheses"&gt;Forming hypotheses&lt;/h3&gt;&#10;&lt;p&gt;One thing that caught my eye immediately: when working with a smaller batch of data (which didn&amp;rsquo;t hit the memory limit), the XCode memory graph showed an &amp;ldquo;overshoot.&amp;rdquo; During processing in the graph here, we hit a peak of 1.1GB memory used, but then subsided back down to a steady state of about 840MB once processing completed.&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/838/images/image.png" alt="" loading="lazy"&gt;&lt;/p&gt;&#10;&lt;p&gt;This suggested that there was not a true memory leak — clearly our code only referenced 840MB of memory, and once the operating system / GPU / drivers had time to &amp;ldquo;clean up&amp;rdquo; any unreferenced memory, 260MB was being released. If we could avoid that spike, our peak memory usage would be 25% lower.&lt;/p&gt;&#10;&lt;p&gt;So, why would that happen? Where are those large blocks of data being held?&lt;/p&gt;&#10;&lt;p&gt;The large data objects are virtually all MTLTexture objects. They&amp;rsquo;re being written (i.e., used as rendertargets) in one phase of our processing, then being read (rendered to another rendertarget) in a later phase. Mechanically in Metal, that means a reference is being held by one or more MTLCommandBuffers that hasn&amp;rsquo;t completed executing yet.&lt;/p&gt;&#10;&lt;p&gt;As pseudocode on the CPU side, that looks like this:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;1 start visible frame&#10;2 for each item in batch {&#10;3 allocate MTLCommandBuffer&#10;4 allocate temporary MTLTexture&#10;5 add &amp;#34;Write to temporary MTLTexture&amp;#34; commands to MTLCommandBuffer&#10;6 add &amp;#34;Read from temporary MTLTexture&amp;#34; command to MTLCommandBuffer&#10;7 send list of commands to GPU (&amp;#34;commit&amp;#34;)&#10; // Optional. This throttles throughput in favour of reducing memory&#10; // footprint.&#10;8 CPU wait until GPU completes execution of MTLCommandBuffer&#10;9 }&#10;10 render batch results to visible frame&#10;11 present visible frame to user&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Those MTLTexture objects are therefore held in several places, via reference counting. This allows us to formulate several hypotheses:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;strong&gt;CPU-side reference counting of MTLTexture&lt;/strong&gt; in line 4. When a loop iteration completes in line 9, the last CPU pointer to the MTLTexture (and MTLCommandBuffer) goes out of scope, and the CPU will not hold any active references. However, Objective C / Swift may not free the MTLTexture memory immediately, as it relies on autorelease pools for reference-counted objects.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Reference to MTLTexture held by MTLCommandBuffer.&lt;/strong&gt; Even if there are no CPU-side MTLTexture references outstanding, there&amp;rsquo;s obviously still several references to the MTLTexture held by the command buffer. (i.e., &amp;ldquo;Write to here&amp;hellip; now, read from here&amp;rdquo;.) Presumably, once the GPU executes all commands referencing a given MTLTexture, it will release the references to those MTLTextures. But — what is the timing of that release? How does it interact with autorelease pools, given that there&amp;rsquo;s potential for GPU/CPU contention?&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Requirement to use MTLPurgeable flags&lt;/strong&gt;. There are various Metal &lt;a href="https://developer.apple.com/documentation/metal/mtlresource/1515898-setpurgeablestate"&gt;MTLPurgeable&lt;/a&gt; flags that can be set on a texture/buffer that might be required for more immediate reuse of a buffer. Metal is currently a little thin on documentation, and it&amp;rsquo;s hard to tell what&amp;rsquo;s required.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Incorrect GPU parallelism&lt;/strong&gt;. In theory, the GPU will execute the &amp;ldquo;write to temporary&amp;rdquo; commands (line 4) before &amp;ldquo;read from temporary&amp;rdquo; (line 5). But what if the parallelism there isn&amp;rsquo;t specified correctly, and the reading task happens in parallel with the writing, and maybe even completes before writing has completed? Then, the wait in line 8 will still leave &amp;ldquo;writing&amp;rdquo; tasks executing, and their temporary memory still in use. Multiple loop iterations might execute in parallel, with higher memory usage.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Requirement for more advanced Metal data structures.&lt;/strong&gt; Metal does offer the &lt;a href="https://developer.apple.com/documentation/metal/mtlheap"&gt;MTLHeap&lt;/a&gt; data structure for more manual control over memory management. This structure has an explicit setAliasable call to reuse a texture/buffer&amp;rsquo;s memory (i.e., free it). It&amp;rsquo;s possible that the behaviour we want is only possible via an MTLHeap.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h3 id="tools"&gt;Tools at hand&lt;/h3&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;The &lt;strong&gt;&lt;em&gt;Metal System Trace&lt;/em&gt; instrument&lt;/strong&gt; gives a good &lt;em&gt;history&lt;/em&gt; of app execution across both CPU and GPU and was helpful for confirming that GPU parallelism was correct. While it shows the timing of mid-frame memory allocations, it &lt;em&gt;doesn&amp;rsquo;t&lt;/em&gt; tell us anything useful about memory release timing.&lt;/li&gt;&#10;&lt;li&gt;The &lt;strong&gt;&lt;em&gt;Memory Graph&lt;/em&gt; instrument&lt;/strong&gt; wasn&amp;rsquo;t useful. It&amp;rsquo;s not really helpful for GPU-side information, and it didn&amp;rsquo;t give any clear way to understand autorelease pool behaviour on the CPU side.&lt;/li&gt;&#10;&lt;li&gt;XCode&amp;rsquo;s &lt;strong&gt;&lt;a href="https://developer.apple.com/documentation/metal/frame_capture_debugging_tools?language=objc"&gt;&lt;em&gt;GPU Frame Capture&lt;/em&gt;&lt;/a&gt; feature&lt;/strong&gt; provides a great &lt;em&gt;snapshot&lt;/em&gt; of memory usage, including a browseable list of all allocated textures, their sizes and their flags. It&amp;rsquo;s designed to work on a single visible frame of animation, and the snapshot of memory that it normally displays is taken after line 11 of my pseudocode — i.e., the &amp;ldquo;steady state&amp;rdquo; memory usage, but not the intermediate &amp;ldquo;peak&amp;rdquo; mid-loop. However, if you use the manual MTLCaptureScope class, you can control the timing of the memory snapshot, and get a view of the peak memory usage. As an alternative, you can actually &lt;a href="https://developer.apple.com/documentation/metal/frame_capture_debugging_tools/capturing_a_frame_using_a_breakpoint?language=objc"&gt;use breakpoints&lt;/a&gt; to control GPU frame capture.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;figure&gt;&#10;&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/838/images/Screen-Shot-2019-12-09-at-12.28.06-PM-1024x518.png" alt="" loading="lazy"&gt;&lt;/p&gt;&#10;&lt;figcaption&gt;&#10;&lt;p&gt;The GPU Frame Capture tool gives a snapshot of allocated memory, at the moment the scope ends. Note that totals do not align with XCode Memory Gauge, missing over 100MB.&lt;/p&gt;&#10;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&lt;h3 id="findings"&gt;Findings&lt;/h3&gt;&#10;&lt;p&gt;Of the five hypotheses I presented, it turned out that #1 was the key. We had CPU-side references that lasted longer than we wanted — because of the way that autorelease pools operate.&lt;/p&gt;&#10;&lt;p&gt;Let&amp;rsquo;s talk through each hypothesis:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;CPU-side references:&lt;/strong&gt; at the end of each loop iteration, we indeed had no CPU-side or GPU-side references remaining. But — the CPU-side references do not get freed until the end of an autorelease pool block is reached. That didn&amp;rsquo;t happen until we returned control to the operating system, after presenting our visible frame.&lt;/p&gt;&#10;&lt;p&gt;We found this using a GPU Frame Capture memory snapshot at the end of a loop iteration. In that snapshot, MTLTextures were still showing up in the list of allocated objects. We added an @autoreleasepool block around each loop iteration, and took a new memory snapshot after the autorelease pool drained — and lo and behold, the memory was now freed promptly.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;References held by MTLCommandBuffers&lt;/strong&gt;: it appears that MTLCommandBuffers are quite smart about tracking their data dependencies. We didn&amp;rsquo;t confirm whether textures references are released as commands execute, but clearly references are fully released when the MTLCommandBuffer completes.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Documentation on topics such as &lt;a href="https://developer.apple.com/documentation/metal/mtlhazardtrackingmode"&gt;MTLHazardTrackingMode&lt;/a&gt; suggests that you can &lt;em&gt;turn off&lt;/em&gt; the data dependency tracking, implying that intelligent dependency tracking is the default.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;GPU flags like MTLPurgeableState:&lt;/strong&gt; at WWDC, we got a tip from an Apple engineer to use an MTLCommandBuffer completionHandler and use that to set the purgeable state on the MTLTextures to make them freeable. That might be helpful with an MTLHeap, but was unnecessary for us — with CPU side references released via @autoreleasepool, the purgeable state didn&amp;rsquo;t need to be set.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Incorrect GPU parallelism:&lt;/strong&gt; using MTLCommandBuffer and regular MTLTextures, this was not an issue; it understood our data dependencies pretty well and ensured sensible parallelism. This was confirmed with the Metal System Trace instrument. When we tried using MTLHeap — with more manual memory management — it was certainly possible to get incorrect GPU parallelism.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;figure&gt;&#10;&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/838/images/Screen-Shot-2019-12-09-at-1.02.28-PM-1024x410.png" alt="" loading="lazy"&gt;&lt;/p&gt;&#10;&lt;figcaption&gt;&#10;&lt;p&gt;Metal System Trace instrument showing mixed CPU and GPU (A12) activity and parallelism for a singe item in the batch. Many temporaries are generated and then freed near the end. Note the deceptive &amp;ldquo;Metal Resource Allocations&amp;rdquo; graph: the drop roughly halfway through represents the point where the CPU released its last reference to the temporary data, but the GPU was still reading from the temporary data until roughly 80% of the way through this operation.&lt;/p&gt;&#10;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&lt;ol start="5"&gt;&#10;&lt;li&gt;&lt;strong&gt;Advanced data structures (MTLHeap):&lt;/strong&gt; we explored this more manual memory management system extensively, but it didn&amp;rsquo;t prove necessary for our situation, and didn&amp;rsquo;t address the underlying CPU-side autorelease pool issue.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h3 id="conclusion"&gt;Conclusion&lt;/h3&gt;&#10;&lt;p&gt;With a newfound respect for autorelease pools, we&amp;rsquo;d solved the issue. The revised pseudocode is shown below, including debugging capture after processing item #10. New lines are in bold.&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;1 start visible frame&#10;2 start GPU Frame Capture&#10;3 for each item in batch {&#10;4 @autoreleasepool {&#10;5 allocate MTLCommandBuffer&#10;6 allocate temporary MTLTexture&#10;7 add &amp;#34;Write to temporary MTLTexture&amp;#34; commands to MTLCommandBuffer&#10;8 add &amp;#34;Read from temporary MTLTexture&amp;#34; command to MTLCommandBuffer&#10;9 send list of commands to GPU (&amp;#34;commit&amp;#34;)&#10;10 CPU wait until GPU completes execution of MTLCommandBuffer &#10;11 print &amp;#34;Before drain: &amp;#34; + os_proc_memory_available()&#10;12 }&#10;13 print &amp;#34;After drain: &amp;#34; + os_proc_memory_available()&#10;14 if (index == 10)&#10;15 end GPU Frame Capture&#10;16 }&#10;10 render batch results to visible frame&#10;11 present visible frame to user &#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And indeed, the transient memory spike was gone. The memory available after draining the autoreleasepool appeared to match the size of the temporary buffers allocated. We had a nice smooth rise to the steady state, and a 25% reduction in peak memory usage. We still had to work some magic to operate on big content — 1.7GB will still only get you so far — but predictable transient memory behaviour was a big step forward.&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/838/images/image-1.png" alt="" loading="lazy"&gt;&lt;/p&gt;&#10;&lt;h2 id="references"&gt;References&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="https://developer.apple.com/documentation/metal/reducing_the_memory_footprint_of_metal_apps?language=objc"&gt;Reducing the Memory Footprint of Metal Apps&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;Developing Optimized Metal Apps and Games video &lt;a href="https://developer.apple.com/videos/play/wwdc2019/606/?time=1589"&gt;Part 3: Memory Footprint&lt;/a&gt;. (Do use the searchable, hyperlinked transcript!)&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://developer.apple.com/documentation/metal/heaps/image_filter_graph_with_heaps_and_fences?language=objc"&gt;Image Filter Graph with heaps and fences&lt;/a&gt; sample code&lt;/li&gt;&#10;&lt;li&gt;Archived documentation on &lt;a href="https://developer.apple.com/library/archive/documentation/Miscellaneous/Conceptual/MetalProgrammingGuide/ResourceHeaps/ResourceHeaps.html"&gt;MTLHeap and fences&lt;/a&gt; adds useful supplemental detail beyond the current documentation.&lt;/li&gt;&#10;&lt;/ul&gt;</description></item><item><title>A Year of Apple Watch</title><link>https://davidpritchard.org/archives/a-year-of-apple-watch/</link><pubDate>Sat, 09 Mar 2019 22:31:00 +0000</pubDate><guid>https://davidpritchard.org/archives/a-year-of-apple-watch/</guid><description>&lt;p&gt;Last January, I bought an Apple Watch series 3. A week later, I bought a second watch.&lt;/p&gt;&#10;&lt;p&gt;In between, I&amp;rsquo;d learned a lesson about trying to buy used goods on Kijiji. I learned from Apple staff that the first one was stolen from an Apple Store&amp;hellip; and Apple decides to put the watches that failed waterproofing tests in the store. I wore the watch for a swim within a few hours of buying it&amp;hellip; and poof, it was gone.&lt;/p&gt;&#10;&lt;p&gt;But I&amp;rsquo;m quite pleased with the watch. I got it for three reasons: to track/improve my exercise, to reliably receive phone/text notifications, and to keep up with current technology / user interface trends. All of these have proven useful.&lt;/p&gt;&#10;&lt;p&gt;Let me talk about the last two first: I appreciate that the watch is less addictive than a phone, and helps you &lt;a href="https://daringfireball.net/linked/2015/03/06/panzarino-time"&gt;reduce phone use&lt;/a&gt;. I&amp;rsquo;ll go a step further: the watch&amp;rsquo;s best feature is that the screen is too small. It&amp;rsquo;s so small that I don&amp;rsquo;t want to use it for an extended period of time&amp;hellip; so I don&amp;rsquo;t. No facebook, no twitter, no instagram, no addictions. I will happily pay extra to have a device that I don&amp;rsquo;t want to use much&amp;hellip; which I guess is &amp;ldquo;win-win&amp;rdquo; for Apple, isn&amp;rsquo;t it?&lt;/p&gt;&#10;&lt;p&gt;I appreciate that it&amp;rsquo;s better adapted to human social interactions: you can turn your phone ringer off and just rely on vibrations, you can keep your phone in a bag and still get all notifications, and it&amp;rsquo;s much easier to quickly glance at a text during a meeting without looking like a jerk.&lt;/p&gt;&#10;&lt;p&gt;On exercise: I&amp;rsquo;m primarily a commuter cyclist and swimmer, and to a lesser extent a bit of an omnivore. Tracking all of the types of sports I do was very appealing. I was also attracted by a company that uses the addictive &amp;ldquo;gamification&amp;rdquo; approach that usually keeps us glued to our smartphones &lt;em&gt;for good -&lt;/em&gt; to actually keep me healthier and happier.&lt;/p&gt;&#10;&lt;p&gt;And it&amp;rsquo;s worked: my calorie goal has risen from 400 active calories per day to 650 over the course of the year, despite a change in work situation removing my reliable cycle commute. The data&amp;rsquo;s also fascinating; while what&amp;rsquo;s captured by &amp;ldquo;Workouts&amp;rdquo; is only part of my total exercise, it&amp;rsquo;s neat to see:&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/a-year-of-apple-watch/images/chart-2.png" alt="" loading="lazy"&gt;&lt;/p&gt;&#10;&lt;figure&gt;&#10;&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/a-year-of-apple-watch/images/Total.png" alt="" loading="lazy"&gt;&lt;/p&gt;&#10;&lt;figcaption&gt;&#10;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&lt;p&gt;Random trivia in these charts:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;My worst month (March 2017) was the one where I spent two weeks on an urban Mexico vacation. With high heat, no bike, and two kids to manage, I got very, very little exercise. This is one reason why I like camping vacations!&lt;/li&gt;&#10;&lt;li&gt;The &amp;ldquo;seasonal&amp;rdquo; category is mostly skating and cross-country skiing, plus a little snowboarding and canoeing. Now that I live in Ottawa, I even skate to work regularly. I&amp;rsquo;m a little annoyed that Apple doesn&amp;rsquo;t track distance or provide maps for skating and skiing, though. And the icon for &amp;ldquo;skating&amp;rdquo; is a skateboard, so I&amp;rsquo;ve started classifying it as &amp;ldquo;hockey&amp;rdquo;.&lt;/li&gt;&#10;&lt;li&gt;In October, a new WatchOS version started autodetecting &amp;ldquo;walking&amp;rdquo; workouts for me.&lt;/li&gt;&#10;&lt;li&gt;The comparison of February 2018 (Toronto) and 2019 (Ottawa) is pretty striking. Winter cycling just wasn&amp;rsquo;t feasible. (And yes, I did cycle 155km in February in Toronto.) Even walking quickly enough to be classified as &amp;ldquo;exercise&amp;rdquo; was tricky on the icy sidewalks.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;</description></item><item><title>Adventures in Vintage Emme</title><link>https://davidpritchard.org/archives/786/</link><pubDate>Tue, 19 Feb 2019 21:32:26 +0000</pubDate><guid>https://davidpritchard.org/archives/786/</guid><description>&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/786/images/emmelogo.png" alt="" loading="lazy"&gt;Imagine there are no variable names. Imagine working - in 2016 - with registers. Imagine one minute file load times. Imagine that all commands are just numbers. Imagine there&amp;rsquo;s no usable string processing.&lt;/p&gt;&#10;&lt;p&gt;Welcome to Emme 3. During the years that I worked in travel demand forecasting, this was the main tool available to me.&lt;/p&gt;&#10;&lt;p&gt;Emme was undoubtedly a trailblazing innovator when it first came out in 1982 and remained a power user&amp;rsquo;s dream through to the early 90s. But it clearly missed the Windows boat; the software seems to have stagnated until beginning a revival in the late 00s.&lt;/p&gt;&#10;&lt;figure&gt;&#10;&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/786/images/emme2.gif" alt="" loading="lazy"&gt;&lt;/p&gt;&#10;&lt;figcaption&gt;&#10;&lt;p&gt;Emme 2&amp;rsquo;s graphical capabilities&lt;/p&gt;&#10;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&lt;p&gt;Like any early 80s software, the &lt;a href="https://pdxscholar.library.pdx.edu/cgi/viewcontent.cgi?referer=https://www.google.com/&amp;amp;httpsredir=1&amp;amp;article=1077&amp;amp;context=cus_pubs"&gt;original version&lt;/a&gt; is old enough that I&amp;rsquo;ve never even heard of the hardware: it was implemented on the Pixel 100/AP Supermicro, a 32-bit 68000 based UNIX workstation. The later shift to DOS was in the pre-486 era when CPUs didn&amp;rsquo;t have floating-point capabilities and required a separate co-processor chip or board, as this &lt;a href="http://www.spiess.ch/emme2/e2news/news02/node2.html"&gt;1986 press release&lt;/a&gt; hypes:&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;&amp;ldquo;The first computational tests show that the [16 MHz co-processor board] DSI-780 beats even our most optimistic expectations. In raw computing power, it is more powerful than a full size VAX 11/780 and is 3 times faster than the already astonishing DSI-32.&amp;rdquo;&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;By the early 2000s, the vendor had misguidedly put energy into a basic Unix GUI, just as the final users abandoned Unix. The vendor belatedly added a real interactive GUI with 2007&amp;rsquo;s Emme 3, offering a Windows interface on top of an unchanged command-line core. The revival gained steam with 2012&amp;rsquo;s Emme 4 - which began to repackage the core algorithms with both a Python API and GUI, including IPython Notebook. It&amp;rsquo;s now a pleasure to use - a shiny, modern and easy-to-use front-end with a very powerful core under the hood.&lt;/p&gt;&#10;&lt;figure&gt;&#10;&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/786/images/emme_notebook-1024x588.png" alt="" loading="lazy"&gt;&lt;/p&gt;&#10;&lt;figcaption&gt;&#10;&lt;p&gt;Emme fully modernized: an Emme 4 session with the IPython Notebook&lt;/p&gt;&#10;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&lt;p&gt;But I&amp;rsquo;m going to talk today about the earlier intermediate version, Emme 3. This article is about masochistic techno archaeology, not productivity. Emme 3 was the awkward teenage years, when the transformation to full adulthood wasn&amp;rsquo;t quite complete yet.&lt;/p&gt;&#10;&lt;p&gt;Next sections:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="#ThePain"&gt;The Pain&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="#ThePromise"&gt;The Promise&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="#MyOwnPath"&gt;My Own Path&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="#Conclusion"&gt;Conclusion&lt;/a&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="ThePain"&gt;The Pain&lt;/h2&gt;&#10;&lt;h2 id="menus--macros"&gt;Menus &amp;amp; Macros&lt;/h2&gt;&#10;&lt;p&gt;Analysis and database management in Emme 3 was still entirely a 1980s experience, driven by a console menu system. And - the kicker - the programming/macro language is actually just a recording of a menu session. If your interactive session looked like this, with user input shown in red:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;3. M A T R I X E D I T O R3.01 Input / modify / output zone groups3.11 Input matrices using batch entry3.12 Input / modify matrices interactively3.13 Plot matrices3.14 Output matrices3.16 Plot histogram of matrices3.21 Matrix calculations3.22 Matrix balancing3.23 Triple-index OperationsEnter: Next module=3.123.12 Input / modify matrices interactively1= initialize a matrix2= delete a matrix3= modify a matrix4= copy a matrix5= list a matrix6= list matrix directory7= initialize matrix directory8= change matrix protection flagsEnter: next=2Enter: Matrix=mf13mf13 diftrt transit time difference (12-09-13 10:31:21)Delete this matrix?yDeleted3.12 Input / modify matrices interactively1= initialize a matrix2= delete a matrix3= modify a matrix4= copy a matrix5= list a matrix6= list matrix directory7= initialize matrix directory8= change matrix protection flagsEnter: next= q&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then your &amp;ldquo;source code&amp;rdquo; would look like this:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;3.122mf13yq&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Or, if you took advantage of the shorthand notation, you could instead say&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;~+|3.12|2|mf13|y|q&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Perfectly legible - a nice, succinct way to say &amp;ldquo;I deleted a matrix!&amp;rdquo;, right? The menus themselves are &lt;em&gt;mostly&lt;/em&gt; in the manual, and they&amp;rsquo;re &lt;em&gt;mostly&lt;/em&gt; consistent between software versions. A particular delight is that the sequence of menu questions is state-dependent, with no easy way to predict whether an extra confirmation will pop up in any given situation. If matrix mf13 doesn&amp;rsquo;t exist, you&amp;rsquo;ll get a different series of questions.&lt;/p&gt;&#10;&lt;h3 id="variably-painful"&gt;Variably Painful&lt;/h3&gt;&#10;&lt;p&gt;The data model is pretty straightforward, mostly:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;a map represented as a graph of nodes and directed edges, with per-node and per-edge custom data&lt;/li&gt;&#10;&lt;li&gt;transit routes&lt;/li&gt;&#10;&lt;li&gt;origin-destination data such as matrices&lt;/li&gt;&#10;&lt;li&gt;global variables&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;The pain is in working with this data. Most of these were accessed by numerical IDs, not by name - mf123 is an origin-destination matrix with ID 123. The macro language offered no arithmetic operators to let you work with numerical IDs. Matrices and per-node/edge data were the rare exception - you can refer to them using a six-character name. Unfortunately, mf&amp;quot;ttawq6&amp;quot; wasn&amp;rsquo;t really enough to clearly define &amp;ldquo;am weighted travel time for the home-station leg of commuter rail trips&amp;rdquo;.&lt;/p&gt;&#10;&lt;p&gt;The global variables were also painful: a small set of floating-point registers, no scope management when calling between macros, and painful string operations. The whole experience felt a lot like programming DOS batch files, or working in assembly language. You can do it, but roll up your sleeves and write a lot of comments.&lt;/p&gt;&#10;&lt;h2 id="ThePromise"&gt;The Promise&lt;/h2&gt;&#10;&lt;p&gt;So why did people put up with this, instead of just using a normal GIS system - say a Python+PostGIS combination? Like with any domain-specific language, you have to consider the trade-offs against a general-purpose system, and I&amp;rsquo;d describe four main reasons:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;A data model that includes transit lines.&lt;/li&gt;&#10;&lt;li&gt;Ready-made implementations of most of the standard operations you&amp;rsquo;ll need, like traffic and transit assignment, management of park &amp;amp; ride demand, etc.&lt;/li&gt;&#10;&lt;li&gt;Base of existing trained users&lt;/li&gt;&#10;&lt;li&gt;A domain-specific GUI and graphing system. (While I&amp;rsquo;ve complained here about the &lt;em&gt;unmodernized&lt;/em&gt; parts of Emme 3, the GUI front-end was modernized and was on an equal footing with peers of its era like ArcGIS.)&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;I have little experience with peer software like TransCAD or CUBE, but I&amp;rsquo;ve heard that Emme&amp;rsquo;s advantages over those include:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;High-quality, battle-tested algorithms&lt;/li&gt;&#10;&lt;li&gt;Customizability&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;While Emme 2 was still an ugly DOS application in 1996, it made good use of the 1980s and 1990s, building a truly powerful database system for the era, with a sound data model and good algorithms that were well-tailored to their problem domain. I&amp;rsquo;m sure that if you were an Emme expert in the late 90s, you could do some truly mind-blowing things compared to your peers. (For the Emme veterans in the crowd, which 1986 tool would you choose: the Emme &lt;a href="http://www.spiess.ch/emme2/e2news/news01/node4.html"&gt;network calculator&lt;/a&gt; or&amp;hellip; uh&amp;hellip; ArcINFO and its network tools?)&lt;/p&gt;&#10;&lt;p&gt;Even in the mid-2000s, that tradeoff still tipped in favour of Emme and its competitors like TransCAD or CUBE. Recreating the Emme tools in a general-purpose platform would far too big an investment for a Metropolitan Planning Organization or other transportation agency.&lt;/p&gt;&#10;&lt;h2 id="MyOwnPath"&gt;My Own Path&lt;/h2&gt;&#10;&lt;p&gt;Despite the obstacles, I got a lot done in Emme 3, and found some good patterns to work around its pain points. Perhaps the most surprising is that Windows filenames proved the best abstraction around the agonizing internals. If I needed to delete three matrices, I didn&amp;rsquo;t have to just repeat the earlier 3.12 incantation three times; instead, I had a nice file called &lt;a href="https://davidpritchard.org/wp-content/uploads/2019/02/matrix-delete.mac"&gt;matrix-delete.mac&lt;/a&gt; that allowed me to just write:&lt;/p&gt;&#10;&lt;p&gt;~&amp;lt;matrix-delete mf3 mf4 mf15&lt;/p&gt;&#10;&lt;p&gt;The windows filenames aren&amp;rsquo;t limited by the awful 6-character limits that pervade everything else, and it became possible to make my code vastly more legible. (And no, I never imagined that I&amp;rsquo;d praise the virtues of Microsoft&amp;rsquo;s 6~n.3 file naming.)&lt;/p&gt;&#10;&lt;p&gt;I&amp;rsquo;m sure that if I&amp;rsquo;d chosen some standard auxiliary tool as my second home - like shelling out to Python to generate custom macros on demand - I could have gone a lot further. But&amp;hellip; taking on external dependencies is also a challenge, particularly when working in a team environment with a lot of non-software staff who can&amp;rsquo;t readily install/configure Python themselves. Not to mention having to fight the government IT staff every step of the way.&lt;/p&gt;&#10;&lt;p&gt;Good software practices can still emerge in such an environment - some day, ask me about how we implemented version control on our network data.&lt;/p&gt;&#10;&lt;h2 id="Conclusion"&gt;Conclusion&lt;/h2&gt;&#10;&lt;p&gt;I&amp;rsquo;m being deliberately snarky about a legacy piece of software in this article, but Emme was utterly invaluable to me as a modeller, and it has advanced by leaps and bounds in the last decade. I&amp;rsquo;m writing about it today mostly as an entertaining anecdote: a glance back at computing/data science history, and a snapshot in time as Emme&amp;rsquo;s vendor took the painful steps necessary to modernize 1980s-era software. I honestly agree with the vendor&amp;rsquo;s decisions - I&amp;rsquo;m sure when they planned Emme 3, it was painful to limit the scope and leave the macro language untouched, but it was the right choice. That version kept their software alive, and gave them the breathing room to make Emme 4. I remain a staunch supporter of evolutionary software improvement rather than the &amp;ldquo;clean slate rewrite.&amp;rdquo;&lt;/p&gt;&#10;&lt;h2 id="references"&gt;References&lt;/h2&gt;&#10;&lt;p&gt;There&amp;rsquo;s a great article on the &lt;a href="https://www.cirrelt.ca/DocumentsTravail/CIRRELT-2008-11.pdf"&gt;history of Emme&lt;/a&gt; from Michael Florian. And there&amp;rsquo;s clearly some drama in the backstory - it&amp;rsquo;s clear that long-time contributor Heinz Spiess had a major falling out with the company in 2005, when the &lt;a href="http://www.spiess.ch/emme2/archive/postscript/enifpaper.pdf"&gt;Unix GUI &amp;ldquo;Enif&amp;rdquo;&lt;/a&gt; was abandoned in favour of the Emme 3&amp;rsquo;s Windows GUI. Speiss also keeps a &lt;a href="http://emme2.enif.ch/e2news/e2news.html"&gt;great archive&lt;/a&gt; of the 80s and 90s Emme 2 newsletters.&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/786/images/emme_email.png" alt="" loading="lazy"&gt;&lt;/p&gt;</description></item><item><title>Working with XAML Designer 2 (UwpSurface)</title><link>https://davidpritchard.org/archives/736/</link><pubDate>Fri, 02 Nov 2018 15:19:57 +0000</pubDate><guid>https://davidpritchard.org/archives/736/</guid><description>&lt;p&gt;&lt;a href="https://davidpritchard.org/wp-content/uploads/2018/11/designercrash.png"&gt;&lt;img src="https://davidpritchard.org/archives/736/images/designercrash-300x161.png" alt="" loading="lazy"&gt;&lt;/a&gt;&lt;/p&gt;&#10;&lt;p&gt;In September 2017, Microsoft released a &lt;a href="https://blogs.msdn.microsoft.com/visualstudio/2017/09/11/a-significant-update-to-the-xaml-designer/"&gt;rewritten XAML Designer program&lt;/a&gt; within Visual Studio. It&amp;rsquo;s only enabled for a tiny fraction of apps and got a very quiet launch, so almost no one knows about it. The new version runs as UwpSurface.exe instead of XDesProc.exe and is only enabled for UWP apps targetting Fall Creators Update SDK or newer.&lt;br&gt;&#10;For my app, the new Designer simply broke everything initially. Why? Presumably for technical reasons, it only works with {Binding} and not {x:Bind} - but this was not made at all clear in the launch announcements. UWP developers have been encouraged to use {x:Bind}: it&amp;rsquo;s compiled, type-safe and faster, and x:Bind functions are the only way to do multibinding. For my 64-bit app, I never got design data or design instances working under XAML Designer (xdesproc), so I relied entirely upon {x:Bind}&amp;rsquo;s FallbackValue parameter to preview different modes of my UI - but {Binding} has no equivalent mechanism.&lt;br&gt;&#10;After a &lt;em&gt;&lt;strong&gt;lot&lt;/strong&gt;&lt;/em&gt; of tinkering, I&amp;rsquo;ve finally learned a few important things about the new XAML Designer, and got a usable workflow.&lt;/p&gt;&#10;&lt;h2 id="top-takeaways"&gt;Top Takeaways&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;UwpSurface is much faster and stabler than XDesProc. The dialog above (&amp;ldquo;Click here to reload the designer&amp;rdquo;) is largely history.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;It works for both 32-bit and 64-bit apps (x86 or x64), which is a big step forward&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Only {Binding} is evaluated; {x:Bind} is completely ignored.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;DesignInstances (a live ViewModel) can be attached via DataContext or d:DataContext, although the DesignInstance parameter doesn&amp;rsquo;t seem to work&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;ViewModel code executes, but I don&amp;rsquo;t see any indication that View code executes, despite discussion to the contrary in Microsoft&amp;rsquo;s launch blog post&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;For C++/CX apps: due to a bug, only single-class ViewModels (without any C++/CX base classes) work as of Visual Studio 15.8.&lt;em&gt;x&lt;/em&gt;. They&amp;rsquo;re fine in C#.&lt;br&gt;&#10;(&lt;em&gt;&lt;strong&gt;Update Jan. 2019:&lt;/strong&gt;&lt;/em&gt; Visual Studio 15.9 &lt;a href="https://developercommunity.visualstudio.com/content/problem/374489/uwpsurface-and-ccx-design-instance.html"&gt;fixed this bug&lt;/a&gt;, but ViewModels that implement property change notifications &lt;a href="https://developercommunity.visualstudio.com/content/problem/396264/uwpsurface-and-ccx-design-instance-implementing-ic.html"&gt;still do not work&lt;/a&gt;.)&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;You can &lt;a href="https://github.com/MicrosoftDocs/visualstudio-docs/blob/master/docs/debugger/walkthrough-debugging-at-design-time.md"&gt;attach a debugger&lt;/a&gt; and see debug output from your ViewModel, or any exceptions. I haven&amp;rsquo;t been able to set breakpoints.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;em&gt;&lt;strong&gt;Update Jan. 2019:&lt;/strong&gt;&lt;/em&gt; Visual Studio 15.9 added support for something I requested in mid-2018: the FallbackValue parameter was added to {Binding}, which makes that an equally viable way to work with XAML Designer. FallbackValue doesn&amp;rsquo;t work for {x:Bind} in the new Designer.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="my-strategy-for-a-ccx-app"&gt;My strategy for a C++/CX app&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Stick with {x:Bind} and its functions in most of my code&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;For the few properties that are central to a clean layout (usually about 2-5), use {Binding} and type converters&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;For those properties, build a duplicate &amp;ldquo;DesignTimeMock&amp;rdquo; ViewModel class - with no C++/CX base classes - and return the design-time property values there. Most properties can be safely omitted.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;In the XAML code, define two different DataContexts like this:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;&amp;lt;UserControl.DataContext&amp;gt; &amp;lt;local:MyViewModel /&amp;gt;&amp;lt;/UserControl.DataContext&amp;gt;&amp;lt;d:UserControl.DataContext&amp;gt; &amp;lt;local:MyViewModel_DesignTimeMock/&amp;gt;&amp;lt;/d:UserControl.DataContext&amp;gt;&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This attaches one ViewModel for runtime, and the mock for design time.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;em&gt;&lt;strong&gt;Update Jan. 2019:&lt;/strong&gt;&lt;/em&gt; Visual Studio 15.9 now allows a different approach: instead of a DesignTimeMock, you can just have a single DataContext using the &amp;ldquo;real&amp;rdquo; view model, but instead change {x:Bind} to {Binding} in the few places where it matters, and then add a FallbackValue parameter to choose the desired value in XAML Designer. The biggest advantage of FallbackValue: you can edit it in place and see it immediately update, without recompiling and relinking the DesignTimeMock view model.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;It&amp;rsquo;s not ideal. But&amp;hellip; it&amp;rsquo;s better than nothing.&lt;/p&gt;&#10;&lt;h2 id="closing-thoughts"&gt;Closing Thoughts&lt;/h2&gt;&#10;&lt;p&gt;Despite the difficulties, I &lt;em&gt;do&lt;/em&gt; look forward to further improvements in the XAML Designer. The old version was dated and crash-prone, and a clean slate rewrite was the only reasonable path forward. It&amp;rsquo;s just going to take some time to reach feature parity with the old version, which will mean some teething pain for &amp;ldquo;guinea pig&amp;rdquo; developers like myself.&lt;/p&gt;&#10;</description></item><item><title>Asynchronous Best Practices in C++/CX (Part 2)</title><link>https://davidpritchard.org/archives/722/</link><pubDate>Tue, 12 Jun 2018 13:50:02 +0000</pubDate><guid>https://davidpritchard.org/archives/722/</guid><description>&lt;p&gt;&lt;a href="https://davidpritchard.org/wp-content/uploads/2018/11/title-1.png"&gt;&lt;img src="https://davidpritchard.org/archives/722/images/title-1.png" alt="" loading="lazy"&gt;&lt;/a&gt;&lt;/p&gt;&#10;&lt;p&gt;This is part two in a series of articles on asynchronous coding in C++/CX. See the &lt;a href="https://davidpritchard.org/archives/712"&gt;introduction here&lt;/a&gt;.&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;a href="https://davidpritchard.org/archives/712#tip1"&gt;Prefer a task chain to nested tasks&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;a href="#tip2"&gt;Be aware of thread scheduling rules&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;a href="#tip3"&gt;Be aware of object and parameter lifetimes&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Consider the effect of OS suspend/resume&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Style: never put a try/catch block around a task chain.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;References&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h2 id="tip2"&gt;2. Be Aware of Thread Scheduling Rules&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;When working with non-threadsafe code, you will usually prefer to continue &lt;em&gt;on the same thread&lt;/em&gt; and not have to deal with multiple threads contending for the same data.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Helpfully, Universal Windows Platform (UWP) apps have a user interface thread inside a Single Threaded Apartment. On that thread, continuations (either a task .then() statement or code after a co_await statement) will &lt;em&gt;&lt;strong&gt;almost always&lt;/strong&gt;&lt;/em&gt; be scheduled to continue on the same thread.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Exception #1:&lt;/strong&gt; if the initial task in a chain is &lt;em&gt;not&lt;/em&gt; an IAsyncAction or IAsyncOperation (or their WithProgress variants), then the continuation may &amp;ldquo;break out&amp;rdquo; of the apartment to a different thread.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Exception #2:&lt;/strong&gt; in my tests, I found that mixing coroutines and task-based continuations could still break out of the apartment. Calling a coroutine and then following it with a .then() task-based continuation seemed to cause it, although I haven&amp;rsquo;t dug very deep into the exact conditions.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;If a continuation is &lt;em&gt;not&lt;/em&gt; on the UI thread, then the guarantees go out the window. For threadpool threads, a continuation can be scheduled on any thread.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;So, partway through a coroutine, you may find yourself on an entirely different thread.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;I imagine this is to prevent &amp;ldquo;starvation&amp;rdquo; of asynchronous chains when running on a compute-intensive thread, for example. But it can be quite a gotcha.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;The concurrency::create_task() method can take a task_continuation_context::use_current() parameter to (try) to force execution to stay on the same thread, but there&amp;rsquo;s no equivalent for coroutines. It&amp;rsquo;s not clear to me that this always works, however; I need to test further and explore.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;When in doubt: use std::this_thread::get_id() to check the thread id over the course of an asynchronous chain of operation.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;When on a threadpool thread, my preference is to write a &lt;em&gt;standalone coroutine&lt;/em&gt; in a &lt;em&gt;functional style&lt;/em&gt; rather than as a traditional method within a class. I avoid using the this pointer, pass inputs to the coroutine by value and return a result from the asynchronous chain. If I want to modify an object, I&amp;rsquo;ll write an accompanying method that calls the coroutine, then schedules a final task &lt;em&gt;on the original thread&lt;/em&gt; to mutate the object. Here&amp;rsquo;s an example:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;class MyClass {private: static concurrency::task&amp;lt;std::wstring&amp;gt; Coroutine(wstring filename) { // ... do some stuff, open file with co_await, read data, etc... co_return result; }public: void MemberFnAsync() { concurrency::create_task(Coroutine(m_filename), task_continuation_context::use_current()) .then([this](wstring result) { this-&amp;gt;SetTextFromFile(result); }); }};&#10;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="tip3"&gt;3. Be Aware of Object and Parameter Lifetimes&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;When passing data into asynchronous code, you want to &lt;em&gt;pass by value.&lt;/em&gt; That applies to both lambda captures (for continuation tasks) and to coroutine parameters.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Why? If you pass do a traditional C++ calling convention like this:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;concurrency::task&amp;lt;void&amp;gt; MyCoroutineAsync (const wstring &amp;amp;str) {// do some stuff.co_await OtherFn();AnotherFn(str);}&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&amp;hellip; the str parameter will typically be valid until the first co_await statement, but at that point execution will return to the caller until OtherFn() completes. The str parameter may well then be destroyed—especially if it was an rvalue or a stack variable. By the time the coroutine resumes and AnotherFn() is called, str will be a dangling pointer.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;The underlying issue is that the coroutine task chain &lt;em&gt;outlives its caller&lt;/em&gt; and therefore can&amp;rsquo;t count on references to data from its caller.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;This applies to reference and pointer data. Pass-by-value is usually preferred, but of course smart pointer types are also valid.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Just be sure to pass smart pointers in a manner that creates a separate reference count: std::shared_ptr&lt;T&gt;, not const std::shared_ptr&lt;T&gt;&amp;amp;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;</description></item><item><title>Asynchronous Best Practices in C++/CX (Part 1)</title><link>https://davidpritchard.org/archives/712/</link><pubDate>Thu, 07 Jun 2018 11:18:29 +0000</pubDate><guid>https://davidpritchard.org/archives/712/</guid><description>&lt;p&gt;&lt;a href="https://davidpritchard.org/wp-content/uploads/2018/11/title-1.png"&gt;&lt;img src="https://davidpritchard.org/archives/712/images/title-1-300x107.png" alt="" loading="lazy"&gt;&lt;/a&gt;&lt;/p&gt;&#10;&lt;p&gt;For me, the steepest learning curves with the Universal Windows Platform (UWP) was the use of asynchronous APIs and the various libraries for dealing with them. Any operation that may take more than 50ms is now asynchronous, and in many cases you can&amp;rsquo;t even call the synchronous equivalent from Win32 or C. This includes networking operations, file I/O, picker dialogs, hardware device enumeration and more. While these APIs are pretty natural when writing C# code, in C++/CX it tends to be a pretty ugly affair. After two years of use, I now have a few &amp;ldquo;best practices&amp;rdquo; to share.&lt;br&gt;&#10;C++/CX offers two different approaches for dealing with asynchronous operations:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;task continuations using the Parallel Patterns Library (PPL)&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;coroutines (as of early 2016)&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Personally, I vastly prefer coroutines; having &lt;code&gt;co_await&lt;/code&gt; gives C++/CX a distinctly C# flavour, and the entire API starts to feel &amp;ldquo;natural.&amp;rdquo; However, at my current job we have not yet standardized on coroutines, and have a mix of both approaches instead. And to be fair - despite Microsoft&amp;rsquo;s &lt;a href="https://blogs.msdn.microsoft.com/vcblog/2016/04/04/using-c-coroutines-to-simplify-async-uwp-code/"&gt;assurances&lt;/a&gt; that they are &amp;ldquo;production ready&amp;rdquo;, I&amp;rsquo;ve personally hit a few coroutine &lt;a href="https://developercommunity.visualstudio.com/content/problem/5849/co-await-trashes-lambda-captures.html"&gt;bugs&lt;/a&gt; and they do occasionally &lt;a href="https://developercommunity.visualstudio.com/content/problem/263131/co-await-optimization-bugs-in-visual-studio-157-x6.html"&gt;completely break&lt;/a&gt; with compiler updates.&lt;br&gt;&#10;I&amp;rsquo;m going to write up my advice in a series of posts, as the examples can be pretty lengthy.&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;a href="#tip1"&gt;Prefer a task chain to nested tasks&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;a href="https://davidpritchard.org/archives/722#tip2"&gt;Be aware of thread scheduling rules&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;a href="https://davidpritchard.org/archives/722#tip3"&gt;Stay aware of object and parameter lifetimes&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Consider the effect of OS suspend/resume&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Style: never put a try/catch block around a task chain.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;References&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h2 id="1-prefer-a-task-chain-to-nested-tasks"&gt;1. Prefer a Task Chain to Nested Tasks&lt;/h2&gt;&#10;&lt;p&gt;When writing a series of API calls that need local variables, conditional logic, or loops, it&amp;rsquo;s tempting to write it as a nest of tasks. But a nest will:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;hurts legibility:&lt;/strong&gt; an extra indent level for every sequential API call, and very wordy&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;painful/missing exception handling:&lt;/strong&gt; every level of the nest needs its own exception handler. Exceptions will &lt;em&gt;not&lt;/em&gt; propagate automatically from an inner task to the outer exception handler, but will instead raise an unobserved exception error.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;makes it hard to return a waitable/gettable task&lt;/strong&gt; that can track when the entire chain completes.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Consider this example nested code:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;concurrency::create_task(folder-&amp;gt;GetFileAsync())&#10;.then([](StorageFile ^file) {&#10; if(file != nullptr) {&#10; concurrency::create_task(file-&amp;gt;ReadAsync())&#10; .then([](IRandomAccessStream ^stream) {&#10; /* do something with stream */&#10; })&#10; .then([](concurrency::task&amp;lt;void&amp;gt; t) {&#10; try { t.get(); }&#10; catch (COMException ^) { /* do something */}&#10; catch (...) { /* do something */ }&#10; });&#10; }&#10;})&#10;.then([](concurrency::task&amp;lt;void&amp;gt; t) {&#10; try { t.get(); }&#10; catch (COMException ^) { /* do something */}&#10; catch (...) { /* do something */ }&#10;});&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here&amp;rsquo;s the equivalent implemented as a task chain:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;concurrency::create_task(folder-&amp;gt;GetFileAsync())&#10;.then([](StorageFile ^file) {&#10; if(file == nullptr)&#10; concurrency::cancel_current_task();&#10; return file-&amp;gt;ReadAsync();&#10;}).then([](IRandomAccessStream ^stream) {&#10; /* do something with stream */&#10;}).then([](concurrency::task&amp;lt;void&amp;gt; t) {&#10;  try { t.get(); }&#10; catch (std::task_cancelled ) { }&#10; catch (COMException ^) { /* do something */}&#10; catch (...) { /* do something */ }&#10;});&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Let&amp;rsquo;s look at a few other situations and the best way to make a chain:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Need access to local variable in earlier stage&lt;/strong&gt; of chain: the best solution is to define a custom struct with the local variables that are needed throughout the chain, heap-allocate it with a std::sharedptr, and pass it in via lambda-capture:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;struct StackFrame { String ^filename; }auto stack = std::make_shared&amp;lt;StackFrame&amp;gt;();concurrency::create_task(folder-&amp;gt;GetFileAsync()).then([stack](StorageFile ^file) { stack-&amp;gt;filename = file-&amp;gt;Name; return file-&amp;gt;ReadAsync();}).then([stack](IRandomAccessStream ^stream) { /* do something with stream and stack-&amp;gt;filename */})&#10;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Need a loop with asynchronous calls on each item&lt;/strong&gt;: collect the tasks for each item in the loop into a std::vector and return when_all on that vector.&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;concurrency::create_task(folder-&amp;gt;GetFilesAsync()).then([stack](IVector&amp;lt;StorageFile&amp;gt; ^files) { std::vector&amp;lt;concurrency::task&amp;lt;void&amp;gt; &amp;gt; tasks; for (auto file : files) { tasks.push_back(concurrency::create_task(file-&amp;gt;DeleteAsync())); return concurrency::when_all(tasks.begin(), tasks.end());});&#10;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Of course—just in case you&amp;rsquo;re curious—as a coroutine this is all trivial and highly readable.&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;try {&#10; StorageFile ^file = co_await folder-&amp;gt;GetFileAsync();&#10; if (file != nullptr) {&#10; IRandomAccessStream ^stream = co_await file-&amp;gt;ReadAsync();&#10; /* do something with stream and file-&amp;gt;Name */&#10; }&#10;}&#10;catch (COMException ^) { /* do something */}&#10;catch (...) { /* do something */ }&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Continues in &lt;a href="https://davidpritchard.org/archives/722"&gt;Part 2&lt;/a&gt;&lt;/p&gt;&#10;</description></item><item><title>This One Weird Grid Trick in XAML</title><link>https://davidpritchard.org/archives/706/</link><pubDate>Fri, 01 Jun 2018 12:32:29 +0000</pubDate><guid>https://davidpritchard.org/archives/706/</guid><description>&lt;p&gt;&lt;img src="https://davidpritchard.org/archives/706/images/rows-300x237.png" alt="" loading="lazy"&gt;&lt;/p&gt;&#10;&lt;p&gt;I recently found a neat XAML user interface trick that I hadn&amp;rsquo;t seen in my &lt;a href="https://wpf.2000things.com/tag/grid/"&gt;usual resources&lt;/a&gt;. Suppose you have:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;a grid-based responsive user interface that you want to grow/shrink to fit the window&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;suppose it has a fixed width, and each row has a &amp;ldquo;core&amp;rdquo; minimum height it needs.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;then there&amp;rsquo;s some extra vertical space that you want to sprinkle around&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;you have some &lt;em&gt;priorities&lt;/em&gt; - first give row #1 extra space, then row #2 any extra.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;XAML makes it easy to do &lt;em&gt;proportional&lt;/em&gt; space allocation - e.g., give row #1 two-thirds and row #2 one-third by giving them &amp;ldquo;2*&amp;rdquo; and &amp;ldquo;*&amp;rdquo; height respectively. But it doesn&amp;rsquo;t do priorities.&lt;br&gt;&#10;The trick: combine a massive star size with a MaxHeight. That looks like this:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;&amp;lt;Grid&amp;gt;&#10; &amp;lt;Grid.RowDefinitions&amp;gt;&#10; &amp;lt;RowDefinition Height=&amp;#34;1000*&amp;#34; MaxHeight=&amp;#34;200&amp;#34; /&amp;gt;&#10; &amp;lt;RowDefinition Height=&amp;#34;*&amp;#34; /&amp;gt;&#10; &amp;lt;/Grid.RowDefinitions&amp;gt;&#10;&amp;lt;/Grid&amp;gt;&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Essentially, row #1 gets &amp;ldquo;first claim&amp;rdquo; on any extra space, up to a limit of 200 pixels. Any extra space beyond 200 pixels falls over to row #2.&lt;/p&gt;&#10;</description></item><item><title>Return of the Mix</title><link>https://davidpritchard.org/archives/return-of-the-mix/</link><pubDate>Tue, 28 Nov 2017 08:16:48 +0000</pubDate><guid>https://davidpritchard.org/archives/return-of-the-mix/</guid><description>&lt;p&gt;&lt;a href="http://35.238.155.21/wp-content/uploads/2017/11/cover.jpg"&gt;&lt;img src="images/cover-300x300.jpg" alt="" loading="lazy"&gt;&lt;/a&gt;&lt;/p&gt;&#10;&lt;p&gt;It&amp;rsquo;s been a while. My daytimes were 80% meetings until last September, when I returned to the coding world. At that point, my job is compatible with listening to (lyrically simple) music&amp;hellip; but I&amp;rsquo;m basically 10 years out-of-date with the scene.&lt;/p&gt;&#10;&lt;p&gt;So I&amp;rsquo;m slowly trying out &amp;ldquo;music discovery&amp;rdquo; in the modern age. If you&amp;rsquo;ve got any good tricks, pass &amp;rsquo;em on! Meanwhile, here&amp;rsquo;s an extended mix — 40 mins. longer than usual — of some of the best stuff I&amp;rsquo;ve dug up.&lt;/p&gt;&#10;&lt;p&gt;As you can see, I&amp;rsquo;ve made more headway in electronic genres than the others. Lyricless music is definitely the best for my line of work. The slowest tune of the bunch (Loscil) is not a &amp;ldquo;listen at work&amp;rdquo; artist, but a different place: we have a sauna in our basement now, and I find slow ambient music is amazing there. Albums I&amp;rsquo;d had for a while — like Loscil&amp;rsquo;s Endless Falls disc — suddenly clicked, with the slow drone fitting perfectly with calm breathing rhythms. That said, I can&amp;rsquo;t yet justify dropping such an introspective track into a mix&amp;hellip; so the Loscil track here has a little more tension in it.&lt;/p&gt;&#10;&lt;p&gt;Download here, for a few weeks only, or stream from Mixcloud.&lt;/p&gt;&#10;&lt;iframe loading="lazy" src="https://www.mixcloud.com/widget/iframe/?hide_cover=1&amp;amp;mini=1&amp;amp;feed=%2Fdrpritch%2Fdavid-pritchard-mix-201711%2F" width="100%" height="60" frameborder="0"&gt;&lt;/iframe&gt;&#10;&lt;p&gt;&lt;a href="https://www.mixcloud.com/drpritch/david-pritchard-mix-201711/?utm_source=widget&amp;amp;utm_medium=web&amp;amp;utm_campaign=base_links&amp;amp;utm_term=resource_link"&gt;David Pritchard - Mix 2017.11&lt;/a&gt; by &lt;a href="https://www.mixcloud.com/drpritch/?utm_source=widget&amp;amp;utm_medium=web&amp;amp;utm_campaign=base_links&amp;amp;utm_term=profile_link"&gt;Drpritch&lt;/a&gt; on &lt;a href="https://www.mixcloud.com/?utm_source=widget&amp;amp;utm_medium=web&amp;amp;utm_campaign=base_links&amp;amp;utm_term=homepage_link"&gt;Mixcloud&lt;/a&gt;&lt;/p&gt;&#10;&lt;p&gt;In this era of easy streaming music&amp;hellip; I feel pretty bad for the artists. If you like these tracks, please buy them individually, or buy a few of the albums. Think of it like a tip jar. The Mixcloud widget will let you buy many tracks from Juno with very little effort, and iTunes or Bandcamp searches can find the rest for you.&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;strong&gt;eight and a half.&lt;/strong&gt; go ego. rock, from &lt;em&gt;eight and a half&lt;/em&gt;, 2012.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;the weeknd.&lt;/strong&gt; rolling stone. r&amp;amp;b, from &lt;em&gt;thursday&lt;/em&gt;, 2011.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;robin thicke.&lt;/strong&gt; everything i can&amp;rsquo;t have. r&amp;amp;b, from &lt;em&gt;the evolution of robin thicke&lt;/em&gt;, 2006.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;hypnolove.&lt;/strong&gt; midnight cruising [mickey moonlight dub]. acid jazz, from &lt;em&gt;future sounds of jazz 12&lt;/em&gt; compilation, 2010.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;the cinematic orchestra.&lt;/strong&gt; everyday. acid jazz, from &lt;em&gt;every day&lt;/em&gt;, 2002.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;baden powell &amp;amp; vinicius de moraes.&lt;/strong&gt; canto de ossanha [m.rux edit]. acid jazz, from &lt;em&gt;m.rux - edits &amp;amp; cuts&lt;/em&gt;, 2014.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;radiohead.&lt;/strong&gt; identikit. rock, from &lt;em&gt;a moon shaped pool&lt;/em&gt;, 2016.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;sufjan stevens.&lt;/strong&gt; drawn to the blood. folk, from &lt;em&gt;carrie &amp;amp; lowell&lt;/em&gt;, 2015.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;dope lemon.&lt;/strong&gt; neon lights. rock, from &lt;em&gt;houndstooth&lt;/em&gt; ep, 2017.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;caribou.&lt;/strong&gt; your love will set you free. techno, from &lt;em&gt;our love&lt;/em&gt;, 2014.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;headshotboyz.&lt;/strong&gt; calypso. dubstep, from &lt;em&gt;project mooncircle 10th anniversary&lt;/em&gt; compilation, 2012.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;com truise&lt;/strong&gt; &lt;strong&gt;feat. joel ford&lt;/strong&gt;. declination. synthwave, from &lt;em&gt;wave 1&lt;/em&gt;, 2014.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;jamie xx.&lt;/strong&gt; girl. electronic, from &lt;em&gt;in colour&lt;/em&gt;, 2015.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;daft punk.&lt;/strong&gt; contact. house, from &lt;em&gt;random access memories&lt;/em&gt;, 2013.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;emperors new clothes.&lt;/strong&gt; darklight [underdog version]. downtempo, from &lt;em&gt;kid loco: dj-kicks&lt;/em&gt;, 1999.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;loscil.&lt;/strong&gt; drained lake. ambient, from &lt;em&gt;monument builders&lt;/em&gt;, 2016.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;lapalux.&lt;/strong&gt; rotted arp. experimental, from &lt;em&gt;ruinism&lt;/em&gt;, 2017.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;gorillaz.&lt;/strong&gt; el mañana. downtempo, from &lt;em&gt;demon days&lt;/em&gt;, 2005.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;yppah.&lt;/strong&gt; never mess with sunday. downtempo, from &lt;em&gt;ninja tunes xx&lt;/em&gt; compilation, 2010.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;plaid.&lt;/strong&gt; scoobs. techno, from &lt;em&gt;peel session&lt;/em&gt; ep, 1999.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;isolée.&lt;/strong&gt; floripa. techno, from &lt;em&gt;floripa&lt;/em&gt; ep, 2015.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;diplo.&lt;/strong&gt; into the sun. downtempo, from &lt;em&gt;florida&lt;/em&gt;, 2000.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;afrolicious.&lt;/strong&gt; a dub for mali. downtempo, from &lt;em&gt;st. germaine-des-prés café 15&lt;/em&gt; compilation, 2013.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;the xx&lt;/strong&gt;. on hold. rock, from &lt;em&gt;i see you&lt;/em&gt;, 2017.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;A few tips outwards: thanks to Hendrik for reminding that Radiohead is amazing, to Tyson for helping me notice that Sufjan Steven&amp;rsquo;s new one was back to his old form, to Dave B for an immersion in synthwave, to the Aussie staff at the Only Cafe for playing that Dope Lemon track, to Paul for getting me on to the XX, to Matt T&amp;rsquo;s friends for pointing me at M.Rux, to Eddy for putting Loscil more firmly on my radar, to Soundcloud for actually making a good &amp;ldquo;you might like&amp;rdquo; recommendation, and to David T for giving me a streaming membership.&lt;/p&gt;&#10;</description></item><item><title>My First XAML Tips for the Universal Windows Platform</title><link>https://davidpritchard.org/archives/678/</link><pubDate>Wed, 01 Nov 2017 21:56:48 +0000</pubDate><guid>https://davidpritchard.org/archives/678/</guid><description>&lt;p&gt;&lt;a href="https://davidpritchard.org/wp-content/uploads/2018/11/xaml-e1509587351270.png"&gt;&lt;img src="https://davidpritchard.org/archives/678/images/xaml-e1509587351270.png" alt="" loading="lazy"&gt;&lt;/a&gt;&lt;/p&gt;&#10;&lt;p&gt;I&amp;rsquo;m a latecomer to Microsoft&amp;rsquo;s user interface technologies. I never used Windows Framework (WPF) on top of .net and I never used Silverlight on the web. The last year was my first taste of these tools through the XAML framework that is part of the &amp;ldquo;Universal Windows Platform&amp;rdquo; (UWP) - that is, the Windows 10 user interface layer (and Win8).&lt;br&gt;&#10;XAML has steadily evolved since the WPF days, and it took a little while to really understand the different major eras of the technology, especially since the UWP flavour of XAML strips out some of the older syntaxes in the name of efficiency on mobile platforms, better error checking at compile-time and code readability and ease-of-use. The technology&amp;rsquo;s old enough that much of the Google search hits and StackOverflow results are not applicable on the modern UWP platform.&lt;br&gt;&#10;&lt;a href="https://davidpritchard.org/wp-content/uploads/2018/11/xaml-1.png"&gt;&lt;img src="https://davidpritchard.org/archives/678/images/xaml-1.png" alt="" loading="lazy"&gt;&lt;/a&gt;&lt;/p&gt;&#10;&lt;h2 id="my-tips"&gt;My Tips&lt;/h2&gt;&#10;&lt;p&gt;So what were a few of my first lessons when using XAML on UWP?&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;a href="#prefer"&gt;Prefer x:Bind to Binding&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;a href="#responsive"&gt;Responsive Design&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;a href="#expressions"&gt;Expressions in x:Bind calls&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;a href="#nocodebehind"&gt;Prefer XAML or ViewModels over code-behind&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;a href="#designer"&gt;Use XAML Designer&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;a href="#commands"&gt;Use Commands, get disabling for free&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;a href="#more"&gt;More to read&lt;/a&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="prefer"&gt;Prefer x:Bind to Binding&lt;/h2&gt;&#10;&lt;p&gt;The main reason: x:Bind gives you compile-time checks and better performance. Be aware of the small differences:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;OneTime binding by default instead of OneWay binding&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Binding path is the framework element (control) not the DataContext (view model)&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="responsive"&gt;Responsive Design&lt;/h2&gt;&#10;&lt;p&gt;Combining VisualState and AdaptiveTriggers is quite a powerful way to hide/show, relocate&lt;br&gt;&#10;or resize different parts of a user interface. The only pain point with this is that you can&amp;rsquo;t alter a style in response to a window size change (or other trigger), only modify the properties of each individual control explicitly. I tried one workaround: making a dummy ViewModel DependencyProperty that gets updated by the trigger, and then binding the styles to that property. This works, but can&amp;rsquo;t be previewed in XAML Designer, which I found useful. Instead, I created a dummy invisible control, set the triggers to modify that control,&lt;br&gt;&#10;and then bound the style to the invisible control&amp;rsquo;s properties. This works both &amp;ldquo;live&amp;rdquo; and in XAML Designer, even updating live as the XAML Designer target device resolution is changed.&lt;br&gt;&#10;I found &lt;a href="https://www.codeproject.com/Articles/896974/Advanced-View-States-for-Windows-apps"&gt;this&lt;/a&gt; reference useful on this subject.&lt;/p&gt;&#10;&lt;h2 id="expressions"&gt;Expressions in x:Bind calls&lt;/h2&gt;&#10;&lt;p&gt;In the Windows Anniversary Edition (Fall 2016), Microsoft added some &lt;em&gt;great&lt;/em&gt; new&lt;br&gt;&#10;capabilities that allow some really nice, clean syntax in XAML, without the use of converters. You can now do the following, provided that your ViewModel class implements the xAnd and xMult functions:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;&amp;lt;Button Visibility=&amp;#34;{x:Bind Vm.xAnd(Vm.IsVisible, Vm.IsStateOn)}&amp;#34;&#10; Width={x:Bind Vm.xMult(Vm.Width, 0.5)}&amp;#34;&amp;gt;&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Notice a few things here:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;despite having only boolean ViewModel properties, we can do a boolean operation on them and auto-convert the boolean result to a Visibility type, without any converters. &lt;strong&gt;Always prefer x:Bind expressions to convertors.&lt;/strong&gt;&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;you can write single or multi-operand functions. (In Visual Studio 2015, XAML Designer didn&amp;rsquo;t support multi-operand, but Visual Studio 2017 added full support.)&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;you can embed constants and work with integer or double types&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;you cannot nest functions or overload function names&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;This opens up a huge number of ways of writing cleaner and more readable XAML code.&lt;/p&gt;&#10;&lt;h2 id="nocodebehind"&gt;Prefer XAML or ViewModels over code-behind&lt;/h2&gt;&#10;&lt;p&gt;Code-behind (the C++ or C# source for the XAML View) should be a last resort. It&amp;rsquo;s already challenging enough to keep track of a XAML file and a ViewModel; burying logic in the View divides the reader&amp;rsquo;s attention even further.&lt;br&gt;&#10;There are situations where something is part of the &amp;ldquo;View&amp;rdquo; rather than the ViewModel: for example, a property that has nothing to do with the Model and only exists to enable the XAML implementation - such as a control spacing property or some such. This is perhaps the one case where View code-behind might make sense. But even there - if you can do it in XAML, that&amp;rsquo;s usually better.&lt;/p&gt;&#10;&lt;h2 id="designer"&gt;Use XAML Designer&lt;/h2&gt;&#10;&lt;p&gt;XAML Designer - the Visual Studio built-in visual editor for XAML - hasn&amp;rsquo;t had much love in a long time. Many things are broken, and it can&amp;rsquo;t handle some useful things. And if you don&amp;rsquo;t put effort into setting up the XAML correctly, a page/control can be totally illegible.&lt;br&gt;&#10;Why prefer XAML Designer?&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Compile times for XAML - especially include C++ code generations - are brutal. You can iterate much faster if you avoid the compile.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;XAML Designer can do a lot of responsive design without even doing a build&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;It&amp;rsquo;s a much quicker way to learn XAML, and even explore events/properties that you weren&amp;rsquo;t aware of.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;How to make XAML Designer more useable:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Ensure your UserControl has d:DesignWidth and d:DesignHeight settings&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Ensure ViewModel has a default zero-parameter constructor&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Bind the ViewModel to the DataContext in XAML, not in code-behind. e.g.,&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;&amp;lt;Page.DataContext&amp;gt;&amp;lt;local:MyViewModel/&amp;gt;&amp;lt;/Page.DataContext&amp;gt;&#10;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Use AdaptiveTriggers for responsive design, rather than ViewModel or View logic&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Add FallbackValues for x:Binds that XAML Designer can&amp;rsquo;t understand. They&amp;rsquo;ll only be used at design-time.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Don&amp;rsquo;t bother trying to get &amp;ldquo;real&amp;rdquo; design-time data working via d:DesignInstance; it doesn&amp;rsquo;t work with a 64-bit application or with x:Bind.&lt;/p&gt;&#10;&lt;p&gt;(In theory, you&amp;rsquo;re supposed to be able to write&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;d:DataContext=&amp;#34;{d:DesignInstance local:MyViewModel, IsDesignTimeCreatable=True}&amp;#34;&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;and get XAML Designer to actually construct an instance of your real ViewModel, and query its values to get the &amp;ldquo;right&amp;rdquo; default values when you&amp;rsquo;re designing. But&amp;hellip; it clearly hasn&amp;rsquo;t been maintained in a long time, it only works in 32-bit and with Binding.)&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="commands"&gt;Use Commands, get disabling for free&lt;/h2&gt;&#10;&lt;p&gt;Why use Commands as the mechanism for the XAML/View to modify the view model&amp;rsquo;s state?&lt;br&gt;&#10;For one, it&amp;rsquo;s often less code: XAML can directly bind to the viewmodel&amp;rsquo;s command property, instead of XAML having a click event, and the View&amp;rsquo;s click event codebehind then modifies the viewmodel.&lt;br&gt;&#10;But also: you can set it up so that when the command &amp;ldquo;cannot execute&amp;rdquo;, then the user interface will automatically grey out and prevent clicking. The details:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;the command should be a ViewModel property, it should implement a CanExecute method, and that CanExecute method should issue a PropertyChanged notification&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;the XAML control should be bound to the command&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;when the command cannot be executed, the XAML control will then enter the &amp;ldquo;Disabled&amp;rdquo; visual state.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="more"&gt;More to Read&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/futurice/windows-app-development-best-practices"&gt;Best Practices for Developing UWP Applications&lt;/a&gt;: a bit C#-centric, but some good tips&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Refer to the Right documentation: surprisingly hard. Google tends to bring up .NET docs rather than UWP, and Visual Studio 2013/15 over the latest 2017 docs.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Use the Live Visual Tree and Live Property Explorer&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Use templated controls over user controls&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;If you have troubles with Visual Studio stability (or speed), try disabling XAML Designer&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;</description></item><item><title>Passive Portfolio 2016</title><link>https://davidpritchard.org/archives/passive-portfolio-2016/</link><pubDate>Mon, 20 Feb 2017 04:58:11 +0000</pubDate><guid>https://davidpritchard.org/archives/passive-portfolio-2016/</guid><description>&lt;p&gt;I haven&amp;rsquo;t updated in a while; the story gets a little boring after a while. But, as I was asked for advice a few times this year, I think it&amp;rsquo;s finally time to do another update.&lt;/p&gt;&#10;&lt;p&gt;Past editions: &lt;a href="https://davidpritchard.org/archives/investing/"&gt;2009&lt;/a&gt;, &lt;a href="https://davidpritchard.org/archives/passive-portfolio-performance-2010/"&gt;2010&lt;/a&gt;, &lt;a href="https://davidpritchard.org/archives/passive-portfolio-tweaks-2011/"&gt;2011&lt;/a&gt;, &lt;a href="https://davidpritchard.org/archives/passive-portfolio-2012/"&gt;2012&lt;/a&gt;, &lt;a href="https://davidpritchard.org/archives/passive-portfolio-2013/"&gt;2013&lt;/a&gt;&lt;/p&gt;&#10;&lt;p&gt;The last few years have been a little slower: +11% in 2014, +9.6% in 2015 and +6.3% in 2016. The difference is largely due to bonds: 2014 was a boom year and 2016 was a slow year. For me personally, the performance over this period was not a big deal; I bought a house in 2014 and now have a real estate-heavy portfolio.&lt;/p&gt;&#10;&lt;h3 id="performance"&gt;Performance&lt;/h3&gt;&#10;&lt;p&gt;Here&amp;rsquo;s the performance of my portfolio over the past several years, using the latest 2016 country weights. The table below shows the annual returns of each component of the portfolio, giving the &amp;ldquo;sequence of returns&amp;rdquo; for each piece.&lt;/p&gt;&#10;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;weight&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;2007- 2011 annual average&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;2012&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;2013&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;2014&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;2015&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;2016&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Equities&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;   U.S.A.&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;VTI&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;33.6%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;-2%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;16%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;43%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;23%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;19%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;9%&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;   Europe / Pacific&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;VEA&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;21.1%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;-7%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;18%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;30%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;3%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;19%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;-1%&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;   Emerging&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;VWO&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;5.3%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;19%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;2%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;9%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;9%&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;   Canada&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;XIC&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;15.0%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;1%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;7%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;12%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;11%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;-8%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;20%&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Subtotal&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;75%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;-2.3%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;15.2%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;28.5%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;12.4%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;11.8%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;8.1%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Fixed income&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;   Mixed bonds&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;VAB&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;20%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;6%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;3%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;-2%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;9%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;4%&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;1%&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Subtotal&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;20%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;5.8%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;3.0%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;-1.8%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;8.6%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;3.6%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;1.2%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Cash&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;5%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;0%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;0%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;0%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;0%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;0%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;0%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Total&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;100%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;-0.3%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;12.0%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;21.1%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;11.0%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;9.6%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;6.3%&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;p&gt;Same assumptions as usual:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Expressed in Canadian dollar terms (i.e., including all currency shift effects and using no currency hedging)&lt;/li&gt;&#10;&lt;li&gt;Includes all distributions/dividends&lt;/li&gt;&#10;&lt;li&gt;Rebalanced annually&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Cumulative returns:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;From Jan. 2007 to Dec. 2016: 76.9% (5.9% annually over 10 years)&lt;/li&gt;&#10;&lt;li&gt;From Jan. 2008 to Dec. 2016: 75.0% (6.4% annually over 9 years)&lt;/li&gt;&#10;&lt;li&gt;From Jan. 2009 to Dec. 2016: 117.9% (10.2% annually over 8 years)&lt;/li&gt;&#10;&lt;li&gt;From Jan. 2012 to Dec. 2016: 75.4% (11.9% annually over 5 years)&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;It&amp;rsquo;s always good to look at the longer term. While the 5-year performance looks quite good at 11.9% growth per year, as soon as the time window includes a major negative event — like the 2008 crash — the 10-year performance shows a more modest 5.9% growth per year: a more realistic expectation for the long run. It&amp;rsquo;s also interesting to see the benefits of a mix of stocks and bonds: during the first five years, bonds outperformed stocks and delivered a +6% annual return (+30% over 5 years) while stocks lost value. It&amp;rsquo;s a little hard to remember now, when bonds have ben so underwhelming recently, but great to see a reminder why a mixed stocks/bonds portfolio is worth while.  (As it happens, I started investing in Jan. 2009, just after the crash, so I haven&amp;rsquo;t actually yet been through a period where bonds seriously paid off.)&lt;/p&gt;&#10;&lt;h3 id="other-notes"&gt;Other Notes&lt;/h3&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Currency exchange: I&amp;rsquo;m earning income in US dollars this year, and no have a sudden interest in better ways to exchange money. I did my first few Norbert&amp;rsquo;s gambit recently, and was pleased by the results. I&amp;rsquo;ll be doing that going forward. For those not willing to make the jump, I did also learn that TD Waterhouse offers a significantly better exchange rate (~1%) when swapping US$25,000 in a single transaction; but that&amp;rsquo;s still a $250 charge while Norbert&amp;rsquo;s gambit is closer to $20.&lt;/li&gt;&#10;&lt;li&gt;I&amp;rsquo;m still curious to know better strategies for managing a portfolio that includes real estate. I feel vastly overexposed to interest rate risk, not to mention the vagaries of the real estate market these days, and it feels to me that bonds may be a bit redundant when holding real estate. Oddly, I haven&amp;rsquo;t found anything that treats real estate as part of a normal portfolio.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;</description></item><item><title>From Data Science &amp; Transportation to Software</title><link>https://davidpritchard.org/archives/671/</link><pubDate>Tue, 20 Sep 2016 20:45:32 +0000</pubDate><guid>https://davidpritchard.org/archives/671/</guid><description>&lt;p&gt;&lt;a href="https://davidpritchard.org/wp-content/uploads/2016/09/mds.png"&gt;&lt;img src="https://davidpritchard.org/archives/671/images/mds-212x300.png" alt="mds" loading="lazy"&gt;&lt;/a&gt;&lt;/p&gt;&#10;&lt;p&gt;As of early September, I&amp;rsquo;ve shifted to a new job in software development / 3D graphics with &lt;a href="http://www.mentalcanvas.com/"&gt;Mental Canvas&lt;/a&gt;. This comes after eight years as a data scientist / transport modeller at Metrolinx. I&amp;rsquo;m very excited to take on this new position, and I&amp;rsquo;m particularly keen to shift to a job where the product is &lt;em&gt;working software&lt;/em&gt; instead of &lt;em&gt;analysis and advice&lt;/em&gt;.&lt;br&gt;&#10;While the transportation modellers I&amp;rsquo;ve talked understand the close relationship between software and modelling, for others in transportation this seems like a drastic shift in direction. It&amp;rsquo;s not; the two jobs have quite a bit in common, and I&amp;rsquo;d like to explain the shared ground. To me, there are three basic stories here:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Similar Data:&lt;/strong&gt; both transportation and graphics have a lot of similarly structured data. Both jobs involve a lot of time organizing, structuring and automating the flow of data.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Data Science &amp;amp; Software:&lt;/strong&gt; a software person would use the term &amp;ldquo;data science&amp;rdquo; to describe transport modelling; it&amp;rsquo;s a hybrid of software and statistics, applied to the problems in the transportation field. Data Science and Software Development are closely related career paths.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Software and Transportation are Converging&lt;/strong&gt;: or perhaps more boldly, software is in the early stages of disrupting the transportation sector.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="similar-data"&gt;Similar Data&lt;/h2&gt;&#10;&lt;p&gt;What&amp;rsquo;s the primary data I worked with as a transportation modeller?&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;2D Networks:&lt;/strong&gt; nodes, links, intersections/turns, transit lines.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Matrices&lt;/strong&gt; (origin-destination): flows of demand, benefits, fares, costs, etc.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;2D Polygons:&lt;/strong&gt; zone boundaries (nodes, edges, faces)&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;What&amp;rsquo;s the primary data I used as a computer graphics programmer?&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;3D Polygons:&lt;/strong&gt; vertices (nodes/points), edges (links), faces&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;2D Rasters:&lt;/strong&gt; bitmaps, textures&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Matrices:&lt;/strong&gt; transformations/projections, Jacobians, Laplacian operator&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Most transportation planners do not look &amp;ldquo;under the hood&amp;rdquo; of transportation models. But for those who do, the models borrow a lot of ideas from computer science, and from Geographic Information Systems (GIS).  If you read the EMME manual from cover-to-cover (and who doesn&amp;rsquo;t enjoy that?), you can&amp;rsquo;t help but see the fundamental connections with computer science.&lt;/p&gt;&#10;&lt;h2 id="software--data-science"&gt;Software &amp;amp; Data Science&lt;/h2&gt;&#10;&lt;p&gt;A software person would use the term &amp;ldquo;data science&amp;rdquo; to describe transportation modelling. That label is utterly foreign to the transportation world, so allow me to quote a bit of &lt;a href="https://en.wikipedia.org/wiki/Data_science"&gt;Wikipedia&lt;/a&gt;:&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;Data scientists use their data and analytical ability to find and interpret rich data sources; manage large amounts of data despite hardware, software, and bandwidth constraints; merge data sources; ensure consistency of datasets; create visualizations to aid in understanding data; build mathematical models using the data; and present and communicate the data insights/findings.&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;Does that sound a bit like transportation modelling? Sure, but without any explicit understanding of transportation. In the software world, every jobs combines a certain amount of software knowledge and details of the particular &amp;ldquo;domain&amp;rdquo; the software is applied to (like &amp;ldquo;transportation&amp;rdquo;). Software jobs are usually 90% software and maybe 10% domain. To do data science well requires a much greater immersion in the domain: maybe 70% software/stats and 30% domain knowledge.&lt;br&gt;&#10;I&amp;rsquo;d say that&amp;rsquo;s not far from what makes a good, transportation modelling specialist: 70% technical skills, 30% understanding of transportation such as traffic theory, transit demand, etc.  Some choose to go further and have more domain knowledge - that is, to be part &amp;ldquo;modeller&amp;rdquo; and part &amp;ldquo;transportation planner&amp;rdquo;, but that&amp;rsquo;s a very rare mix of skills.&lt;br&gt;&#10;Data science is one of the hot areas within computer science. You see headlines like &amp;ldquo;Data Scientist: the Sexiest Job of the 21st Century&amp;rdquo; (&lt;a href="https://hbr.org/2012/10/data-scientist-the-sexiest-job-of-the-21st-century"&gt;Harvard Business Review&lt;/a&gt;), questions like &amp;ldquo;Which career is more promising: data scientist or software developer?&amp;rdquo; (&lt;a href="https://www.quora.com/Which-career-is-more-promising-data-scientist-or-software-developer-1"&gt;Quora&lt;/a&gt;), and salary surveys showing Data Scientist of the #4 best-paid category within software (&lt;a href="http://stackoverflow.com/research/developer-survey-2016#work-salary"&gt;Stack Overflow&lt;/a&gt;).  Again, it can be seen as an area related to software development.&lt;br&gt;&#10;I&amp;rsquo;m originally a software developer, I did several years of work in data science / transportation modelling, and now I&amp;rsquo;m returning to regular software development.&lt;/p&gt;&#10;&lt;h2 id="software-and-transportation-are-converging"&gt;Software and Transportation are Converging&lt;/h2&gt;&#10;&lt;p&gt;The software industry has clearly set its sights on the transportation sector. To me, one of the recent signs was in &lt;a href="http://www.recode.net/2016/5/8/11617328/introducing-the-new-recode"&gt;May 2016&lt;/a&gt; when tech news site Re/Code dedicated one of its nine major topic areas to &lt;a href="http://www.recode.net/transportation"&gt;transportation&lt;/a&gt;. Uber and its Chinese competitor Didi Chuxing are the first breakout multibillion dollar hits (unless you count Google Maps), and Tesla is Silicon Valley&amp;rsquo;s first real car company. Many industry giants are investing heavily, particularly in the self-driving car and ridesharing spaces: Google, Tesla, Apple and Uber in particular, with substantial competition from the traditional car companies. In-city goods movement and retailing patterns are also starting to shift due to the growth of e-commerce and especially the growth of Amazon as a real competitor to the &amp;ldquo;big box&amp;rdquo; / &amp;ldquo;power centre&amp;rdquo; format of the 1990s. Long-haul goods movement hasn&amp;rsquo;t seen any real change yet, but self-driving truck companies like Otto are emerging.&lt;br&gt;&#10;Of course, the Silicon Valley hype machine is also in full effect. I don&amp;rsquo;t think software is going to change everything instantly. But I do think that having both skillsets will be a valuable combination, and that a future job may bring me back to tackling transportation problems with my software skills.&lt;br&gt;&#10;We shall see.&lt;/p&gt;</description></item></channel></rss>