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

<channel>
	<title>davidpritchard.org</title>
	<atom:link href="https://davidpritchard.org/feed" rel="self" type="application/rss+xml" />
	<link>https://davidpritchard.org</link>
	<description></description>
	<lastBuildDate>Fri, 26 Dec 2025 16:21:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>C++ Stack Traces on UWP</title>
		<link>https://davidpritchard.org/archives/907</link>
					<comments>https://davidpritchard.org/archives/907#comments</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Wed, 21 Jul 2021 13:36:11 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[windows]]></category>
		<guid isPermaLink="false">http://davidpritchard.org/?p=907</guid>

					<description><![CDATA[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 &#8211; it disappeared, and instead there was a big stream of “Unknown” class crashes. This caused &#8230; <a href="https://davidpritchard.org/archives/907" class="more-link">Continue reading <span class="screen-reader-text">C++ Stack Traces on UWP</span> <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-image size-medium is-style-default float-right"><img fetchpriority="high" decoding="async" width="300" height="277" src="https://davidpritchard.org/wp-content/uploads/2021/07/image-2-300x277.png" alt="" class="wp-image-917" srcset="https://davidpritchard.org/wp-content/uploads/2021/07/image-2-300x277.png 300w, https://davidpritchard.org/wp-content/uploads/2021/07/image-2.png 494w" sizes="(max-width: 300px) 100vw, 300px" /></figure>



<p>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 &#8211; it disappeared, and instead there was a big stream of “Unknown” class crashes.</p>



<p>This caused much <a href="https://github.com/microsoft/appcenter/issues/436">wailing</a> 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.)</p>



<p></p>



<p></p>



<div class="wp-block-image is-style-default"><figure class="aligncenter size-large is-resized"><img decoding="async" src="https://davidpritchard.org/wp-content/uploads/2021/07/image-1-882x1024.png" alt="" class="wp-image-913" width="295" height="342" srcset="https://davidpritchard.org/wp-content/uploads/2021/07/image-1-882x1024.png 882w, https://davidpritchard.org/wp-content/uploads/2021/07/image-1-259x300.png 259w, https://davidpritchard.org/wp-content/uploads/2021/07/image-1-768x891.png 768w, https://davidpritchard.org/wp-content/uploads/2021/07/image-1.png 972w" sizes="(max-width: 295px) 100vw, 295px" /><figcaption>Some wailing</figcaption></figure></div>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="261" src="https://davidpritchard.org/wp-content/uploads/2021/07/image-1-1-1024x261.png" alt="" class="wp-image-914" srcset="https://davidpritchard.org/wp-content/uploads/2021/07/image-1-1-1024x261.png 1024w, https://davidpritchard.org/wp-content/uploads/2021/07/image-1-1-300x76.png 300w, https://davidpritchard.org/wp-content/uploads/2021/07/image-1-1-768x196.png 768w, https://davidpritchard.org/wp-content/uploads/2021/07/image-1-1-1536x391.png 1536w, https://davidpritchard.org/wp-content/uploads/2021/07/image-1-1-2048x522.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>Some gnashing of teeth</figcaption></figure>



<p>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 <em>usage per package version </em>that&#8217;s important. For &lt;100 crashes per version, I get 0% stack traces; for &lt;250 I get very few; and for the version with 750 crashes I get good (64%) stack traces.</p>



<figure class="wp-block-table"><table><tbody><tr><td>Sample size</td><td>Filter Type</td><td>% Unknown</td><td>% Stack Traces</td></tr><tr><td>1,051</td><td>None</td><td>50%</td><td>50%</td></tr><tr><td>750</td><td>Package version</td><td>36%</td><td>64%</td></tr><tr><td>485</td><td>OS version</td><td>54%</td><td>46%</td></tr><tr><td>316</td><td>OS version</td><td>34%</td><td>66%</td></tr><tr><td>234</td><td>Package version</td><td>86%</td><td>14%</td></tr><tr><td>110</td><td>OS version</td><td>50%</td><td>50%</td></tr><tr><td>100</td><td>OS version</td><td>99%</td><td>1%</td></tr><tr><td>66</td><td>Package version</td><td>100%</td><td>0%</td></tr></tbody></table></figure>



<p>Even for the &#8220;higher usage&#8221; package version, however, I&#8217;m still not getting solid data on the &#8220;rare&#8221; crash types &#8211; just the frequent crashes.</p>



<p>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 <a href="https://stefanwick.com/2018/04/06/uwp-with-desktop-extension-part-1/">monitor the UWP process from Win32-land</a>, 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.</p>



<p>But… UWP apps do have access to one key API: <code>CaptureStackBackTrace</code>.</p>



<h2 class="wp-block-heading">A bare bones solution</h2>



<p> A full-fledged crash handling and stack trace solution is quite complicated: running in a <a href="https://stackoverflow.com/a/1964556">separate guard process</a> 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</p>



<ul class="wp-block-list"><li>Single thread: only show stack of active (crashing) thread</li><li>App portion of stack is critical; system/Windows portion is desirable, but less important</li><li>Severe memory corruption doesn’t need to be handled initially</li><li>Only handle C++/CX and x64. It probably works for other situations, but that’s all I’ve tested to date.</li></ul>



<p>With those criteria, I came up with a five-part solution:</p>



<ol class="wp-block-list"><li>Pre-crash: log system info to disk</li><li>Crash time: set a callback to get notified, and capture stack to disk</li><li>Next app startup: transmit log+stack trace to server</li><li>[OPTIONAL] Server: symbolify stack trace</li><li>[OPTIONAL] Server: aggregate results</li></ol>



<p>Let’s take a quick look at each piece.</p>



<h3 class="wp-block-heading">Log System Info</h3>



<p>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.</p>



<h3 class="wp-block-heading">Crash Capture: Callback on Crash</h3>



<p>There are several potential mechanisms to get notified on crashes:</p>



<ul class="wp-block-list"><li>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.</li><li>Invalid memory access: the best bet seems to be <code>__try</code> / <code>__catch</code> / <code>_set_se_translator </code>for Windows Structured Exception Handling (SEH)</li></ul>



<p>These were also options, but didn’t seem to actually get called for relevant situations like null pointer dereferences:</p>



<ul class="wp-block-list"><li>A different SEH Win32 C API: <code>SetUnhandledExceptionFilter</code></li></ul>



<ul class="wp-block-list"><li>UWP event handlers: <code>Windows::ApplicationModel::Core::CoreApplication::UnhandledErrorDetected</code> and <code>Windows::UI::Xaml::UnhandledException</code></li></ul>



<p>What is SEH? It’s a C API that handles <strong><em>hardware </em></strong>exceptions on x86/x64 chips; while it uses the <code>__try</code> and <code>__catch</code> keywords, it’s <strong><em>not </em></strong>about software exceptions (either C++ or managed code). There&#8217;s some good discussion of it in the <a href="http://crashrpt.sourceforge.net/docs/html/exception_handling.html">CrashRpt documentation</a>. 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:</p>



<ul class="wp-block-list"><li>Get called when a crash happens, on the crashing thread.</li><li>Grab a stack trace and record it (typically skipping the first 8 frames that are in VCRUNTIME and NTDLL)</li><li>Also grab and record the exception type (EXCEPTION_ACCESS_VIOLATION, EXCEPTION_STACK_OVERFLOW, etc.)</li></ul>



<p>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.)</p>



<p>The actual implementation is pretty straightforward:</p>



<pre class="wp-block-preformatted">#include &lt;eh.h&gt;

void se_trans_func( unsigned int u, _EXCEPTION_POINTERS *pExp) {
  // Skip 8 stack frames - we usually see the ones below.
  // 00:MyApp.exe+0x009a7b98		(this function)
  // 01 .. 05:VCRUNTIME140D_APP.dll
  // 06 .. 08:ntdll.dll
  Char *backTrace =GetBackTrace(8);
  Str desc;
  switch (u) {
  case EXCEPTION_ACCESS_VIOLATION:
    desc = L"ACCESS_VIOLATION";	     break;
  case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
    desc = L"ARRAY_BOUNDS_EXCEEDED"; break;
  // ... etc. ...
  case EXCEPTION_STACK_OVERFLOW:
    desc = L"STACK_OVERFLOW";        break;
  default:
    desc = StrPrintf(TEXT("%d"), u); break;
  }

  LogPrintf(L"Structured Exception Handling failure type %s. Stack trace:\n%s\n"),
    desc.Data(), backTrace);
  delete[] backTrace;

  // A bit irrelevant... we mostly just want the stack trace.
  throw L”Hardware exception”;
}

App::App() {
  // ...
  _set_se_translator(se_trans_func);
  // ...
}</pre>



<h3 class="wp-block-heading">Crash Capture: Record Stack Trace</h3>



<p>From a UWP app, we don’t have access to many Win32 APIs &#8211; but we can call <code>CaptureStackBackTrace.</code> If we combine that with calls to <code>RtlPcToFileHeader </code>and <code>GetModuleName</code>, 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:</p>



<pre class="wp-block-preformatted">myapp.exe+0x00001234</pre>



<p>Rather than the symbolified (actual function name and signature) with a function-relative offset:</p>



<pre class="wp-block-preformatted">myapp.exe crashyFunction(int, const std::string &amp;) + 0x0000001a</pre>



<p>Or even better, with source filenames and line numbers:</p>



<pre class="wp-block-preformatted">myapp.exe crashyFunction(int, const std::string &amp;) + 0x0000001a
myapp.cpp:465</pre>



<p>In theory, you <em>could</em> 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.</p>



<p>So &#8211; 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.</p>



<p>The actual capture code looks like this:</p>



<pre class="wp-block-preformatted">Str GetBackTrace(int SkipFrames)
{
  constexpr uint TRACE_MAX_STACK_FRAMES = 99;
  void *stack[TRACE_MAX_STACK_FRAMES];
  ULONG hash;
  const int numFrames = CaptureStackBackTrace(SkipFrames + 1, TRACE_MAX_STACK_FRAMES, stack, &amp;hash);
  Str result = StrPrintf(L"Stack hash: 0x%08lx\n", hash);
  for (int i = 0; i &lt; numFrames; ++i) {
    void *moduleBaseVoid = nullptr;
    RtlPcToFileHeader(stack[i], &amp;moduleBaseVoid);
    auto moduleBase = (const unsigned char *)moduleBaseVoid;
    constexpr auto MODULE_BUF_SIZE = 4096U;
    wchar_t modulePath[MODULE_BUF_SIZE];
    const wchar_t *moduleFilename = modulePath;
    if (moduleBase != nullptr) {
      GetModuleFileName((HMODULE)moduleBase, modulePath, MODULE_BUF_SIZE);
      int moduleFilenamePos = Str(modulePath).FindLastOf(L"\\");
      if (moduleFilenamePos &gt;= 0)
        moduleFilename += moduleFilenamePos + 1;
      result += StrPrintf(L"%02d:%s+0x%08lx\n"), i, moduleFilename, 
        (uint32)((unsigned char *)stack[i] - moduleBase));
     }
     else
       result += StrPrintf(L"%02d:%s+0x%016llx\n"), i, moduleFilename, 
         (uint64)stack[i]);
   }
   return result;
}</pre>



<h3 class="wp-block-heading">Next App Startup: Transmit Log</h3>



<p>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.</p>



<p>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.</p>



<h3 class="wp-block-heading">[OPTIONAL] Symbolify Stack Trace</h3>



<p>The server is &#8211; by necessity &#8211; a Windows VM listening to HTTPS requests.</p>



<p>On the server, we maintain a set of PDB files for each app version (extracted from the <code>.msixpackage</code> zip file). At present, we only handle x64 and not ARM64 crashes.</p>



<p>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.</p>



<p>When a log file comes in, we:</p>



<ul class="wp-block-list"><li>Parse the log to retrieve the app version and Windows version</li><li>Match up the relevant PDB file and directory of Windows system DLLs</li><li>For each line of the stack trace, detect whether it’s in our app or a system DLL</li><li>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 <a href="http://bytepointer.com/resources/old_new_thing/20131115_302_restoring_symbols_to_a_stack_trace_originally_generated_without_symbols.htm">Raymond Chen’s oldnewthing blog</a>.</li></ul>



<p>The heart of this is the call to CDB, which looks like this:</p>



<pre class="wp-block-preformatted">export CDB=/mnt/c/Progra\~2/Windows\ Kits/10/Debuggers/x64/cdb.exe
# Typical raw output, prior to sed expressions:
#   MyApp!MyApp::App::CrashyFunction+0x25fb
#   [C:\Users\MyUser\source\myapp\MyApp.cpp @ 757]:
# Sed expressions simplify it to:
#   App::CrashyFunction+0x25fb [MyApp.cpp @ 757]
"$CDB" -lines -z myapp.exe -c "u $entry; q" -y \
  "cache*c:\\Symbols;srv*https://msdl.microsoft.com/download/symbols"\
  | grep -m 1 -A 1 "^0:000&gt;" \
  | tail -1 \
  | sed 's/:$//;' \
  | sed 's/MyApp:://g;' \
  | sed 's/\[[^@[]*\\\([A-Za-z0-9_.]*.\(h\|cpp\) @ \)/\[\1/g'</pre>



<p>For Windows system DLL entries, we can choose a $SYS32DIR to match the client&#8217;s Windows version, and then change the -z argument to</p>



<pre class="wp-block-preformatted">-z $SYS32DIR\\$DLLFILENAME</pre>



<p>This ensures that CDB uses the <em>client’s</em> Windows version to decide which symbols to retrieve for the DLL, rather than the normal behaviour, which would use the <em>server VM</em>’s Windows version. We’ll still get a fair bit of mismatch &#8211; as we usually don’t have the DLLs that precisely match the client’s Windows version.</p>



<p>If there’s some way to specify a Windows version when telling CDB about the symbol server &#8211; let me know! That would be a huge timesaver.</p>



<h3 class="wp-block-heading">[OPTIONAL] Aggregate results</h3>



<p>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.</p>



<p>But I haven’t actually implemented this yet. For this purpose, an off-the-shelf tool will probably suffice &#8211; a service like Sentry or Visual Studio App Center would do the trick, and both accept submission of crash data via an API.</p>



<h3 class="wp-block-heading">What if we don’t symbolify?</h3>



<p>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.</p>



<h3 class="wp-block-heading">Conclusion</h3>



<p>So &#8211; 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.</p>



<p>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.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://davidpritchard.org/archives/907/feed</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>Pitfalls mixing PPL+await</title>
		<link>https://davidpritchard.org/archives/902</link>
					<comments>https://davidpritchard.org/archives/902#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Wed, 31 Mar 2021 20:08:57 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[windows]]></category>
		<guid isPermaLink="false">http://davidpritchard.org/?p=902</guid>

					<description><![CDATA[Imagine you have a C++/CX codebase with a lot of asynchronous logic written using the classic Parallel Patterns Library (PPL) tasks: It&#8217;s all running on the UI thread, so it&#8217;s in a Single Threaded Apartment, and each task in your chain of .then() continuations is guaranteed to also be on the UI thread. Now, you &#8230; <a href="https://davidpritchard.org/archives/902" class="more-link">Continue reading <span class="screen-reader-text">Pitfalls mixing PPL+await</span> <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[
<p>Imagine you have a C++/CX codebase with a lot of asynchronous logic written using the classic <a href="https://docs.microsoft.com/en-us/cpp/parallel/concrt/parallel-patterns-library-ppl?view=msvc-160">Parallel Patterns Library</a> (PPL) <a href="https://docs.microsoft.com/en-us/cpp/parallel/concrt/parallel-patterns-library-ppl?view=msvc-160">tasks</a>:</p>



<pre class="wp-block-code"><code>task&lt;void> FooAsync(StorageFolder ^folder) {
  return create_task(folder->TryGetItemAsync(L"foo.txt"))
    .then(&#91;](IStorageItem ^item) {
      // do some stuff, cast to a file, etc.
    });
}</code></pre>



<p>It&#8217;s all running on the UI thread, so it&#8217;s in a Single Threaded Apartment, and each task in your chain of <code>.then()</code> continuations is guaranteed to also be on the UI thread.</p>



<p>Now, you learn about Microsoft&#8217;s relatively new <code>co_await</code> feature, recently accepted in an adapted form in the C++20 spec. So you start using it:</p>



<pre class="wp-block-code"><code>task&lt;void> FooAsyncCo(StorageFolder ^folder) {
  IStorageItem ^item =
    co_await folder->TryGetItemAsync(L"foo.txt");
  // do some stuff, cast to a file, etc.
}</code></pre>



<p>So far so good: all code inside <code>FooAsyncCo()</code> also runs on the UI thread, nice and clean.</p>



<p>Finally, you integrate a little of this <code>co_await</code> code into your heavily PPL codebase. Helpfully, <code>co_await</code> returns a task, just like your existing async PPL method. Another developer sees that you have a bunch of methods returning task&lt;void> and decides to start building on that API using PPL:</p>



<pre class="wp-block-code badcode"><code><span class="has-inline-color has-red-color">FooAsyncCo().then(&#91;]() {
  // more stuff, after coroutine.
  // <strong>KABOOM.</strong> Running on a threadpool thread, *not* the UI thread.
  // We accidentally busted out of the STA.
});</span></code></pre>



<p>Uh-oh. When you mix the two async approaches&#8230; things break. And none of the documentation &#8211; or honestly anything I&#8217;ve read on the web to date &#8211; gives any hint of this issue.</p>



<h2 class="wp-block-heading">Not A Fix</h2>



<p>Well&#8230; let&#8217;s see. The docs do give us one tip: a task chain is only guaranteed to remain inside the Single Threaded Apartment <strong><em>if</em></strong> the chain starts with an <code>IAsyncAction</code> or <code>IAsyncOperation</code></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>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.</p><cite><a href="https://docs.microsoft.com/en-us/windows/uwp/threading-async/asynchronous-programming-in-cpp-universal-windows-platform-apps#managing-the-thread-context">Asynchronous programming in C++/CX: Managing the Thread Context</a></cite></blockquote>



<p>Well&#8230; <code>TryGetItemAsync</code> does return an <code>IAsyncOperation</code>. That would appear to be the root task in the chain&#8230; so coroutines must be treated differently, with the coroutine itself being the root.</p>



<p>Well, what if we tried making the coroutine return an <code>IAsyncAction</code>?</p>



<pre class="wp-block-preformatted"><span class="has-inline-color has-red-color">IAsyncAction ^FooAsyncCoAction(StorageFolder ^folder) {
  return create_async([folder]() -> task&lt;void> {
    // <strong>KABOOM</strong> - this is now out of the apartment.
    IStorageItem ^item =
      co_await folder->TryGetItemAsync(L"foo.txt");
    // do some stuff, cast to a file, etc.
  });
}</span>
create_task(FooAsyncCo())
.then([]() {
  // more stuff, after coroutine.
  // ok now!
});</pre>



<p>No dice. Now the coroutine runs off-thread, while the continuation runs correctly on the UI thread.</p>



<h2 class="wp-block-heading">Two Actual Fixes</h2>



<p>With a little more reading, I found Raymond Chen&#8217;s <a href="https://devblogs.microsoft.com/oldnewthing/20180801-00/?p=99385">blog post</a> about PPL and apartment-aware tasks. That led to this successful solution:</p>



<pre class="wp-block-preformatted">task&lt;void&gt; completed_apartment_aware_task() {
  return create_task(create_async([](){}));
}

<span style="color:#00a30f" class="has-inline-color">completed_apartment_aware_task()
.then(FooAsyncCo)
.then([]() {
  // <strong>Ok!</strong>
});</span></pre>



<p>This one actually works. By rooting the PPL chain in an <code>IAsyncAction</code>, the rest of the chain retains apartment awareness, and everything stays on the UI thread.</p>



<p>And I&#8217;d earlier found a different but more fragile solution:</p>



<pre class="wp-block-preformatted"><span style="color:#00a337" class="has-inline-color">create_task(FooAsyncCo, task_continuation_context::use_current())
.then([]() {
  // <strong>Ok!</strong>
});</span></pre>



<p>While this works, it&#8217;s&#8230; a bit hard to tell whether the root task, or the continuation, or what-all needs <code>use_current()</code>, and it&#8217;s always felt fragile to me. And if it gets called <strong><em>from</em></strong> a threadpool thread, it&#8217;s unclear to the caller what happens with <code>use_current().</code></p>



<h2 class="wp-block-heading">Conclusion</h2>



<p>I&#8217;ve got to say&#8230; 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&#8217;t going to happen all in one go, at least if there&#8217;s any conditional/loop/exception logic in the PPL-based code. Anyone who&#8217;s tried to migrate to <code>co_await</code> has probably run into this pitfall.</p>



<p>We haven&#8217;t adopted the <code>completed_apartment_aware_task()</code> as a root task throughout our codebase yet, but I&#8217;m hopeful that will at least offer a path forward. Just&#8230; still quite error-prone.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://davidpritchard.org/archives/902/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>A Windows to iOS Port</title>
		<link>https://davidpritchard.org/archives/871</link>
					<comments>https://davidpritchard.org/archives/871#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Tue, 16 Jun 2020 01:37:48 +0000</pubDate>
				<category><![CDATA[ios]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[windows]]></category>
		<guid isPermaLink="false">http://davidpritchard.org/?p=871</guid>

					<description><![CDATA[My main activity of 2019 was a port of the Mental Canvas 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. What&#8217;s interesting about our porting approach? Well, it&#8217;s just an unusual mix: It&#8217;s a UWP (Universal Windows Platform) &#8230; <a href="https://davidpritchard.org/archives/871" class="more-link">Continue reading <span class="screen-reader-text">A Windows to iOS Port</span> <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[
<p>My main activity of 2019 was a port of the <a href="https://www.mentalcanvas.com">Mental Canvas</a> 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.</p>



<p>What&#8217;s interesting about our porting approach? Well, it&#8217;s just an unusual mix:</p>



<ul class="wp-block-list"><li>It&#8217;s a UWP (Universal Windows Platform) app. UWP was Microsoft&#8217;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</li><li>We didn&#8217;t use React Native, Flutter, Electron, Xamarin, Qt or even Fuschia&#8217;s namesake <a href="https://en.wikipedia.org/wiki/Taligent#Pink_systemhttps://en.wikipedia.org/wiki/Taligent#Pink_system">Pink</a>. We just&#8230; wrote some native code. And a lightweight abstraction layer.</li><li>We completed the porting effort in seven months, with three developers.</li><li>We stayed with the MVVM architecture that is the norm on Windows. Many iOS developers would call that&#8230; <a href="https://ashfurrow.com/blog/mvvm-is-exceptionally-ok/">exceptionally ok</a></li><li>We didn&#8217;t use the latest and greatest Swift tools, just plain old C++11 and Objective-C for the most part. We didn&#8217;t switch to SwiftUI when it was released half way through our port.</li></ul>



<p>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&#8217;s size down.</p>



<ol class="wp-block-list"><li><a href="#StartingPoint">Starting Point</a></li><li><a href="#ui-framework-choice">UI Framework Choice</a></li><li><a href="#restructuring-ui">Restructuring the UI Thread</a></li><li><a href="#ios-bindings">Lightweight iOS Bindings</a></li><li><a href="#what-about-swiftui">What About SwiftUI?</a></li><li><a href="#incremental-porting">Incremental Porting</a></li><li><a href="#upshot-code-reuse">The Upshot: Code Reuse</a></li></ol>



<span id="more-871"></span>



<h2 class="wp-block-heading" id="starting-point">Starting Point</h2>



<p>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&#8217;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.</p>



<p>At the outset of the port, our app was only running on Windows. From a portability standpoint there were three main considerations:</p>



<ul class="wp-block-list"><li><strong>User interface</strong>: the Windows UI logic was sound, but&#8212;as it was written in C++/CX&#8212;was not immediately portable to iOS</li><li><strong>Graphics API</strong>: Apple deprecated OpenGL 18 months prior to the start of our port, although they ultimately elected to allow it to survive 2019</li><li><strong>Platform</strong>: 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</li></ul>



<p>I&#8217;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&#8217;s worth a quick glance at each portion of our user interface structure:</p>



<div class="wp-block-image"><figure class="aligncenter"><img loading="lazy" decoding="async" width="1024" height="1024" src="https://davidpritchard.org/wp-content/uploads/2020/01/MC-arch-1024x1024.png" alt="" class="wp-image-873" srcset="https://davidpritchard.org/wp-content/uploads/2020/01/MC-arch-1024x1024.png 1024w, https://davidpritchard.org/wp-content/uploads/2020/01/MC-arch-300x300.png 300w, https://davidpritchard.org/wp-content/uploads/2020/01/MC-arch-150x150.png 150w, https://davidpritchard.org/wp-content/uploads/2020/01/MC-arch-768x768.png 768w, https://davidpritchard.org/wp-content/uploads/2020/01/MC-arch-1536x1536.png 1536w, https://davidpritchard.org/wp-content/uploads/2020/01/MC-arch-2048x2048.png 2048w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>



<ul class="wp-block-list"><li><strong>Model:</strong> the render thread holds the model (i.e., the contents of a 3D scene)</li><li><strong>ViewModel:</strong> 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.</li><li><strong>View:</strong> native UI controls and their responsive layout are defined declaratively in XAML, with bindings to ViewModel properties established at compile-time</li></ul>



<p>There is almost no shared memory across threads. Communication between threads is done using a dedicated incoming message queue for each thread.</p>



<h2 class="wp-block-heading" id="ui-framework-choice">UI Framework Choice</h2>



<p>Should we use a cross-platform framework? We had a few unusual considerations</p>



<ul class="wp-block-list"><li><strong>Platforms:</strong> 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)</li><li><strong>Input devices:</strong> first-class pen support is unusual, particularly on mobile-focused frameworks</li></ul>



<p>And some more common considerations in games or other apps:</p>



<ul class="wp-block-list"><li><strong>Look and feel:</strong> a &#8220;fun&#8221; feel is key to our app, and benefits from an &#8220;easy, responsive&#8221; user interface and native feel.</li><li><strong>Graphics:</strong> UI framework must be lightweight, and co-operate and overlay cleanly over a 60 fps (or even 120 fps) graphics thread</li><li><strong>Memory:</strong> our app is quite memory intensive, especially for mobile devices with only 3GB of RAM</li></ul>



<p>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&#8217;s imperative design. We built a quick, working prototype of our app in React Native to get a feel for the technology&#8217;s strengths and weaknesses.</p>



<p>In the end, though, we chose a native port. The main downsides that we saw in React Native were:</p>



<ul class="wp-block-list"><li>no MacOS port</li><li>a less &#8220;native&#8221; feel</li><li>risk from one additional vendor dependency (Facebook) and one new technology dependency (Microsoft UWP port)</li><li>complex toolchain</li></ul>



<p>And as a more short-term issue, it was worth considering our staff&#8217;s lack of React experience. There&#8217;s always a fair bit of risk when you can&#8217;t &#8220;ease in&#8221; to a technology, and have to go straight to architecture without much concrete experience.</p>



<h2 class="wp-block-heading" id="restructuring-ui">Restructuring the UI thread</h2>



<p>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&#8230; which depends on the platform binding mechanism.</p>



<p>So&#8230; let&#8217;s think about those bindings. Each ViewModel has a set of member variables that are &#8220;bindable properties&#8221;, 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.</p>



<p>The main issues we faced in making this portable were:</p>



<ol class="wp-block-list"><li>Windows: the <code>{x:Bind}</code> mechanism requires the ViewModel to be a native object (in C++/CX, a <a href="https://docs.microsoft.com/en-us/cpp/cppcx/ref-classes-and-structs-c-cx?view=vs-2019">ref class</a>), and each bindable property has to be a special <a href="https://docs.microsoft.com/en-us/cpp/cppcx/properties-c-cx?view=vs-2019">special native type</a>.</li><li>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.</li></ol>



<p>None of this was insurmountable, and we were ultimately successful in making the ViewModel entirely portable, leaving only the View code platform-dependent.</p>



<h2 class="wp-block-heading" id="ios-bindings">Lightweight iOS Bindings</h2>



<p>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 <a href="https://mjtsai.com/blog/2014/03/16/the-siren-call-of-kvo-and-cocoa-bindings/">slow and fragile</a>, but&#8230; so are all of the awful hacks people seem to do in UIKit to keep their UI in sync.</p>



<p>At any rate, it proved fairly simple to write a few methods that simply track a list of source-target pair:</p>



<figure class="wp-block-table is-style-regular"><table><thead><tr><th>Source</th><th>TARGET</th></tr></thead><tbody><tr><td>ViewModel + Property name</td><td>UIView + Keypath (KVO)</td></tr><tr><td>e.g., Layer ViewModel + Opacity</td><td>e.g., UIStackView + @&#8221;opacitySlider.value&#8221;</td></tr></tbody></table></figure>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<p>With some kind of notification/pub-sub system, we can watch for VM property changes, and call the UIView&#8217;s <code>setValue:forKeyPath:</code> 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.</p>
</div></div>



<p>There are two little niceties that I may elaborate in a separate post:</p>



<ol class="wp-block-list"><li>Compile-time keypath validity checks: borrowed from <a href="https://web.archive.org/web/20190408182555/http://forgecode.net/2011/11/compile-time-checking-of-kvc-keys/">Nick Forge&#8217;s post</a>, to catch typos earlier and avoid a compile + run cycle.</li><li>Multi-bindings, boolean ops, and type conversions: various ways to combine/convert multiple ViewModel properties prior to binding to a UIView property.</li></ol>



<p>Suffice it to say that the final syntax is something like this:</p>



<p><code>m_bindings-&gt;add</code>(<br>    <code>BIND_CHECK2(modeSegmented, setSelectedSegmentTintColor),</code><br>    <code>VmFn::IfElse(m_vm-&gt;IsNavMode, SNavModeColor, SDrawModeColor));</code></p>



<p>which is a continuously updating version of</p>



<p><code>self.modeSegmented.selectedSegmentTintColor =<br>m_vm-&gt;IsNavMode ? SNavModeColor : SDrawModeColor;</code></p>



<h2 class="wp-block-heading" id="what-about-swiftui">What About SwiftUI?</h2>



<p>While I agree that <a href="https://ashfurrow.com/blog/mvvm-is-exceptionally-ok/">MVVM is exceptionally ok</a>, SwiftUI is clearly the &#8220;next stop&#8221; 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.</p>



<p>However, we haven&#8217;t pursued SwiftUI yet for a few reasons:</p>



<ol class="wp-block-list"><li><strong>Swift interoperability with C++ remains painful.</strong> We would need to maintain a lot of Objective C code to bridge our C++ data structures over to Swift&#8230; unless <a href="https://forums.swift.org/t/c-objective-c-interop/9989">things change some day</a>.</li><li><strong>Ship Timing:</strong> our ship date was September 2019 with a target of iOS11 and up. It&#8217;ll be a little while before we can limit the software to iOS 13 only.</li><li><strong>Development Timing:</strong> we had already ported 60% of the app by the time SwiftUI was announced. We&#8217;d heard the Catalyst rumours and <em>very faint</em> mentions of Amber ahead of WWDC, but there was nothing we could rely on.</li></ol>



<p>I&#8217;m sure that we <em>could</em> 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&#8217;ll face the following trade-off:</p>



<ul class="wp-block-list"><li><strong>Pro:</strong> Accelerated UI iteration using SwiftUI vs. UIKit. Less AutoLayout agony.</li><li><strong>Con:</strong> Pain of maintaining an Objective C++ bridge between C++11 ViewModels and Swift Views</li></ul>



<p>Once Apple inevitably starts making some UI controls Swift-only, the decision will probably tip in favour of SwiftUI.</p>



<p>At the end of the day&#8230; if you&#8217;re going for portable, you need to use portable languages. Swift deliberately ain&#8217;t that; it&#8217;s Apple&#8217;s ecosystem accelerator and lock-in mechanism.</p>



<h2 class="wp-block-heading" id="incremental-porting">Incremental Porting</h2>



<p>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.</p>



<p>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:</p>



<ul class="wp-block-list"><li>an iOS version that was demoable with a subset of UI</li><li>a Windows version that still compiled and worked 100%</li></ul>



<p>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.</p>



<h2 class="wp-block-heading" id="upshot-code-reuse">The Upshot: Code Reuse</h2>



<p>Here are the architecture diagrams after completing the port. </p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="922" src="https://davidpritchard.org/wp-content/uploads/2020/01/MC-port-both-1024x922.png" alt="" class="wp-image-874" srcset="https://davidpritchard.org/wp-content/uploads/2020/01/MC-port-both-1024x922.png 1024w, https://davidpritchard.org/wp-content/uploads/2020/01/MC-port-both-300x270.png 300w, https://davidpritchard.org/wp-content/uploads/2020/01/MC-port-both-768x692.png 768w, https://davidpritchard.org/wp-content/uploads/2020/01/MC-port-both-1536x1383.png 1536w, https://davidpritchard.org/wp-content/uploads/2020/01/MC-port-both-2048x1844.png 2048w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>And here&#8217;s an analysis of the source code size before and after the port:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="600" height="371" src="https://davidpritchard.org/wp-content/uploads/2020/01/Porting-MentalCanvas.png" alt="" class="wp-image-875" srcset="https://davidpritchard.org/wp-content/uploads/2020/01/Porting-MentalCanvas.png 600w, https://davidpritchard.org/wp-content/uploads/2020/01/Porting-MentalCanvas-300x186.png 300w" sizes="auto, (max-width: 600px) 100vw, 600px" /></figure>



<p>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.</p>



<p>A few statistics:</p>



<ul class="wp-block-list"><li>The size of the Windows build grew by 5% due to the port.</li><li>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.</li><li>If we hadn&#8217;t ported, and instead had 30,000 lines of iOS-specific code (including Metal backend), we&#8217;d probably be looking at a 110,000 line app. So we likely reduced our codebase size by about 10% using this approach.</li></ul>



<p>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&#8217;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.</p>



<p></p>



<p>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&#8230; relatively few new bugs.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://davidpritchard.org/archives/871/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Metal Memory Debugging</title>
		<link>https://davidpritchard.org/archives/838</link>
					<comments>https://davidpritchard.org/archives/838#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Wed, 27 Nov 2019 23:03:52 +0000</pubDate>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[ios]]></category>
		<guid isPermaLink="false">http://davidpritchard.org/?p=838</guid>

					<description><![CDATA[At Mental Canvas, Dave Burke 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&#8217;d share our process and findings. The issue: a transient memory &#8230; <a href="https://davidpritchard.org/archives/838" class="more-link">Continue reading <span class="screen-reader-text">Metal Memory Debugging</span> <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[
<p><img loading="lazy" decoding="async" width="485" height="612" class="wp-image-847" style="width: 150px; float: right; margin: .5em" src="https://davidpritchard.org/wp-content/uploads/2019/11/spike.png" alt="" srcset="https://davidpritchard.org/wp-content/uploads/2019/11/spike.png 485w, https://davidpritchard.org/wp-content/uploads/2019/11/spike-238x300.png 238w" sizes="auto, (max-width: 485px) 100vw, 485px" />At <a href="https://www.mentalcanvas.com">Mental Canvas</a>,  <a href="https://github.com/mealworms">Dave Burke</a> 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&#8217;d share our process and findings.</p>



<ol class="wp-block-list"><li><a href="#issue">The issue: a transient memory spike</a></li><li><a href="/archives/838#memorylimits">iOS / iPadOS memory limits</a></li><li><a href="/archives/838#hypotheses">Forming hypotheses</a></li><li><a href="/archives/838#tools">Tools at hand</a></li><li><a href="/archives/838#findings">Findings</a></li><li><a href="/archives/838#conclusion">Conclusion</a></li><li>References</li></ol>



<h3 class="wp-block-heading" id="issue">The issue: a transient memory spike</h3>



<p>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.</p>



<p>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&#8217;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&#8217;s perspective, it considers all of those temporaries to still be in use.</p>



<p>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?</p>



<span id="more-838"></span>



<h3 class="wp-block-heading" id="memorylimits">iOS / iPadOS memory limits</h3>



<p>From our empirical reviews of the various tools, we can say the following:</p>



<ul class="wp-block-list"><li>The XCode Memory Gauge precisely reflects what the operating system thinks your app&#8217;s size is (as advertised in many WWDC talks).</li><li>The great new <code><a href="https://developer.apple.com/documentation/os/3191911-os_proc_available_memory?language=objc">os_proc_available_memory()</a></code> call in iOS13 appears to precisely match the XCode Memory Gauge. A delta between <code>os_proc_available_memory()</code> at app startup and later in the app represents memory consumption in the same manner that the OS sees it.</li><li>The operating system seems to kill an app once its usage hits roughly system memory minus 1GB<ul><li>Test systems: a 3GB iPad Air (2019) and a 4GB iPad Pro (2018), running both iOS12 and iOS13</li><li>API reported RAM: 2.8GB and 3.6GB respectively</li><li>Crashes at 1.8GB and 2.7GB memory usage</li></ul></li><li>Be cautious of other memory metrics: neither XCode&#8217;s <em>GPU Frame Capture</em> memory view, the <em>Metal System Trace </em>Instrument nor the <em>Memory Graph</em> Instrument appear to give totals that line up cleanly with the XCode Memory Gauge.</li><li>Ultimately, I got<em> GPU Frame Capture </em>to give me great data — but with an awareness of <em>when</em> its snapshot is taken, which is typically at the end of rendering a visible frame, and <em>after</em> some operating system activity has happened. </li></ul>



<h3 class="wp-block-heading" id="hypotheses">Forming hypotheses</h3>



<p>One thing that caught my eye immediately: when working with a smaller batch of data (which didn&#8217;t hit the memory limit), the XCode memory graph showed an &#8220;overshoot.&#8221; 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.</p>



<div class="wp-block-image"><figure class="aligncenter is-resized"><img loading="lazy" decoding="async" src="https://davidpritchard.org/wp-content/uploads/2019/11/image.png" alt="" class="wp-image-839" width="449" height="503" srcset="https://davidpritchard.org/wp-content/uploads/2019/11/image.png 898w, https://davidpritchard.org/wp-content/uploads/2019/11/image-268x300.png 268w, https://davidpritchard.org/wp-content/uploads/2019/11/image-768x860.png 768w" sizes="auto, (max-width: 449px) 100vw, 449px" /></figure></div>



<p>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 &#8220;clean up&#8221; any unreferenced memory, 260MB was being released. If we could avoid that spike, our peak memory usage would be 25% lower.</p>



<p>So, why would that happen? Where are those large blocks of data being held?</p>



<p>The large data objects are virtually all MTLTexture objects. They&#8217;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&#8217;t completed executing yet. </p>



<p>As pseudocode on the CPU side, that looks like this:</p>



<pre class="wp-block-preformatted" style="font-size: 9pt">1   start visible frame
2   for each item in batch {
3     allocate MTLCommandBuffer
4     allocate temporary MTLTexture
5     add "Write to temporary MTLTexture" commands to MTLCommandBuffer
6     add "Read from temporary MTLTexture" command to MTLCommandBuffer
7     send list of commands to GPU ("commit")
      // Optional. This throttles throughput in favour of reducing memory
      // footprint.
8     CPU wait until GPU completes execution of MTLCommandBuffer
9   }
10  render batch results to visible frame
11  present visible frame to user</pre>



<p>Those MTLTexture objects are therefore held in several places, via reference counting. This allows us to formulate several hypotheses:</p>



<ol class="wp-block-list"><li><strong>CPU-side reference counting of MTLTexture </strong>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.</li><li><strong>Reference to MTLTexture held by MTLCommandBuffer. </strong>Even if there are no CPU-side MTLTexture references outstanding, there&#8217;s obviously still several references to the MTLTexture held by the command buffer. (i.e., &#8220;Write to here&#8230; now, read from here&#8221;.) 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&#8217;s potential for GPU/CPU contention?</li><li><strong>Requirement to use MTLPurgeable flags</strong>. There are various Metal <a href="https://developer.apple.com/documentation/metal/mtlresource/1515898-setpurgeablestate">MTLPurgeable</a> 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&#8217;s hard to tell what&#8217;s required.</li><li><strong>Incorrect GPU parallelism</strong>. In theory, the GPU will execute the &#8220;write to temporary&#8221; commands (line 4) before &#8220;read from temporary&#8221; (line 5).  But what if the parallelism there isn&#8217;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 &#8220;writing&#8221; tasks executing, and their temporary memory still in use. Multiple loop iterations might execute in parallel, with higher memory usage.</li><li><strong>Requirement for more advanced Metal data structures. </strong>Metal does offer the <a href="https://developer.apple.com/documentation/metal/mtlheap">MTLHeap</a> data structure for more manual control over memory management. This structure has an explicit setAliasable call to reuse a texture/buffer&#8217;s memory (i.e., free it). It&#8217;s possible that the behaviour we want is only possible via an MTLHeap.</li></ol>



<h3 class="wp-block-heading" id="tools">Tools at hand</h3>



<ul class="wp-block-list"><li>The <strong><em>Metal System Trace</em> instrument </strong>gives a good <em>history</em> 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 <em>doesn&#8217;t </em>tell us anything useful about memory release timing.</li><li>The <strong><em>Memory Graph</em> instrument</strong> wasn&#8217;t useful. It&#8217;s not really helpful for GPU-side information, and it didn&#8217;t give any clear way to understand autorelease pool behaviour on the CPU side.</li><li>XCode&#8217;s <strong><a href="https://developer.apple.com/documentation/metal/frame_capture_debugging_tools?language=objc"><em>GPU Frame Capture</em> </a>feature</strong> provides a great <em>snapshot</em> of memory usage, including a browseable list of all allocated textures, their sizes and their flags. It&#8217;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 &#8220;steady state&#8221; memory usage, but not the intermediate &#8220;peak&#8221; 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 <a href="https://developer.apple.com/documentation/metal/frame_capture_debugging_tools/capturing_a_frame_using_a_breakpoint?language=objc">use breakpoints </a>to control GPU frame capture.</li></ul>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="518" src="https://davidpritchard.org/wp-content/uploads/2019/11/Screen-Shot-2019-12-09-at-12.28.06-PM-1024x518.png" alt="" class="wp-image-859" srcset="https://davidpritchard.org/wp-content/uploads/2019/11/Screen-Shot-2019-12-09-at-12.28.06-PM-1024x518.png 1024w, https://davidpritchard.org/wp-content/uploads/2019/11/Screen-Shot-2019-12-09-at-12.28.06-PM-300x152.png 300w, https://davidpritchard.org/wp-content/uploads/2019/11/Screen-Shot-2019-12-09-at-12.28.06-PM-768x389.png 768w, https://davidpritchard.org/wp-content/uploads/2019/11/Screen-Shot-2019-12-09-at-12.28.06-PM.png 1316w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /><figcaption>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. </figcaption></figure>



<h3 class="wp-block-heading" id="findings">Findings</h3>



<p>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.</p>



<p>Let&#8217;s talk through each hypothesis:</p>



<ol class="wp-block-list"><li><strong>CPU-side references:</strong> 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&#8217;t happen until we returned control to the operating system, after presenting our visible frame.<br><br>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.</li><li><strong>References held by MTLCommandBuffers</strong>: it appears that MTLCommandBuffers are quite smart about tracking their data dependencies. We didn&#8217;t confirm whether textures references are released as commands execute, but clearly references are fully released when the MTLCommandBuffer completes.<ul><li>Documentation on topics such as  <a href="https://developer.apple.com/documentation/metal/mtlhazardtrackingmode">MTLHazardTrackingMode</a>  suggests that you can <em>turn off</em> the data dependency tracking, implying that intelligent dependency tracking is the default.</li></ul></li><li><strong>GPU flags like MTLPurgeableState: </strong>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&#8217;t need to be set.</li><li><strong>Incorrect GPU parallelism: </strong>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.</li></ol>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="410" src="https://davidpritchard.org/wp-content/uploads/2019/12/Screen-Shot-2019-12-09-at-1.02.28-PM-1024x410.png" alt="" class="wp-image-860" srcset="https://davidpritchard.org/wp-content/uploads/2019/12/Screen-Shot-2019-12-09-at-1.02.28-PM-1024x410.png 1024w, https://davidpritchard.org/wp-content/uploads/2019/12/Screen-Shot-2019-12-09-at-1.02.28-PM-300x120.png 300w, https://davidpritchard.org/wp-content/uploads/2019/12/Screen-Shot-2019-12-09-at-1.02.28-PM-768x308.png 768w, https://davidpritchard.org/wp-content/uploads/2019/12/Screen-Shot-2019-12-09-at-1.02.28-PM-1536x615.png 1536w, https://davidpritchard.org/wp-content/uploads/2019/12/Screen-Shot-2019-12-09-at-1.02.28-PM-2048x820.png 2048w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /><figcaption>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 &#8220;Metal Resource Allocations&#8221; 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.</figcaption></figure>



<ol class="wp-block-list" start="5"><li><strong>Advanced data structures (MTLHeap):</strong> we explored this more manual memory management system extensively, but it didn&#8217;t prove necessary for our situation, and didn&#8217;t address the underlying CPU-side autorelease pool issue.</li></ol>



<h3 class="wp-block-heading" id="conclusion">Conclusion</h3>



<p>With a newfound respect for autorelease pools, we&#8217;d solved the issue. The revised pseudocode is shown below, including debugging capture after processing item #10. New lines are in bold.</p>



<pre class="wp-block-preformatted" style="font-size: 9pt">1   start visible frame
<strong>2   start GPU Frame Capture
</strong>3   for each item in batch {
<strong>4     @autoreleasepool {
</strong>5       allocate MTLCommandBuffer
6       allocate temporary MTLTexture
7       add "Write to temporary MTLTexture" commands to MTLCommandBuffer
8       add "Read from temporary MTLTexture" command to MTLCommandBuffer
9       send list of commands to GPU ("commit")
10      CPU wait until GPU completes execution of MTLCommandBuffer 
<strong>11      print "Before drain: " + os_proc_memory_available()
</strong>12    }
<strong>13    print "After drain: " + os_proc_memory_available()
14    if (index == 10)
15      end GPU Frame Capture
</strong>16  }
10  render batch results to visible frame
11  present visible frame to user </pre>



<p>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.</p>



<div class="wp-block-image"><figure class="aligncenter is-resized"><img loading="lazy" decoding="async" src="https://davidpritchard.org/wp-content/uploads/2019/11/image-1.png" alt="" class="wp-image-841" width="327" height="313" srcset="https://davidpritchard.org/wp-content/uploads/2019/11/image-1.png 654w, https://davidpritchard.org/wp-content/uploads/2019/11/image-1-300x287.png 300w" sizes="auto, (max-width: 327px) 100vw, 327px" /></figure></div>



<h2 class="wp-block-heading">References</h2>



<ul class="wp-block-list"><li><a href="https://developer.apple.com/documentation/metal/reducing_the_memory_footprint_of_metal_apps?language=objc">Reducing the Memory Footprint of Metal Apps</a></li><li>Developing Optimized Metal Apps and Games video <a href="https://developer.apple.com/videos/play/wwdc2019/606/?time=1589">Part 3: Memory Footprint</a>. (Do use the searchable, hyperlinked transcript!)</li><li><a href="https://developer.apple.com/documentation/metal/heaps/image_filter_graph_with_heaps_and_fences?language=objc">Image Filter Graph with heaps and fences</a> sample code</li><li>Archived documentation on <a href="https://developer.apple.com/library/archive/documentation/Miscellaneous/Conceptual/MetalProgrammingGuide/ResourceHeaps/ResourceHeaps.html">MTLHeap and fences</a> adds useful supplemental detail beyond the current documentation.</li></ul>
]]></content:encoded>
					
					<wfw:commentRss>https://davidpritchard.org/archives/838/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Adventures in Vintage Emme</title>
		<link>https://davidpritchard.org/archives/786</link>
					<comments>https://davidpritchard.org/archives/786#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Wed, 20 Feb 2019 02:32:26 +0000</pubDate>
				<category><![CDATA[modelling & sci/tech]]></category>
		<guid isPermaLink="false">http://davidpritchard.org/?p=786</guid>

					<description><![CDATA[Imagine there are no variable names. Imagine working &#8211; in 2016 &#8211; with registers. Imagine one minute file load times. Imagine that all commands are just numbers. Imagine there&#8217;s no usable string processing. Welcome to Emme 3. During the years that I worked in travel demand forecasting, this was the main tool available to me. &#8230; <a href="https://davidpritchard.org/archives/786" class="more-link">Continue reading <span class="screen-reader-text">Adventures in Vintage Emme</span> <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[
<p><img loading="lazy" decoding="async" width="541" height="235" class="wp-image-822" style="width: 150px; float: right" src="https://davidpritchard.org/wp-content/uploads/2019/02/emmelogo.png" alt="" srcset="https://davidpritchard.org/wp-content/uploads/2019/02/emmelogo.png 541w, https://davidpritchard.org/wp-content/uploads/2019/02/emmelogo-300x130.png 300w" sizes="auto, (max-width: 541px) 100vw, 541px" />Imagine there are no variable names. Imagine working &#8211; in 2016 &#8211; with registers. Imagine one minute file load times. Imagine that all commands are just numbers. Imagine there&#8217;s no usable string processing.</p>



<p>Welcome to Emme 3. During the years that I worked in travel demand forecasting, this was the main tool available to me.</p>



<p>Emme was undoubtedly a trailblazing innovator when it first came out in 1982 and remained a power user&#8217;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.</p>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="511" height="388" src="https://davidpritchard.org/wp-content/uploads/2019/02/emme2.gif" alt="" class="wp-image-817"/><figcaption>Emme 2&#8217;s graphical capabilities</figcaption></figure>



<span id="more-786"></span>



<p>Like any early 80s software, the <a href="https://pdxscholar.library.pdx.edu/cgi/viewcontent.cgi?referer=https://www.google.com/&amp;httpsredir=1&amp;article=1077&amp;context=cus_pubs">original version</a> is old enough that I&#8217;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&#8217;t have floating-point capabilities and required a separate co-processor chip or board, as this <a href="http://www.spiess.ch/emme2/e2news/news02/node2.html">1986 press release</a> hypes:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>&#8220;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.&#8221;</p></blockquote>



<p>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&#8217;s Emme 3, offering a Windows interface on top of an unchanged command-line core. The revival gained steam with 2012&#8217;s Emme 4 &#8211; which began to repackage the core algorithms with both a Python API and GUI, including IPython Notebook. It&#8217;s now a pleasure to use &#8211; a shiny, modern and easy-to-use front-end with a very powerful core under the hood.</p>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="1024" height="588" src="https://davidpritchard.org/wp-content/uploads/2019/02/emme_notebook-1024x588.png" alt="" class="wp-image-816" srcset="https://davidpritchard.org/wp-content/uploads/2019/02/emme_notebook-1024x588.png 1024w, https://davidpritchard.org/wp-content/uploads/2019/02/emme_notebook-300x172.png 300w, https://davidpritchard.org/wp-content/uploads/2019/02/emme_notebook-768x441.png 768w, https://davidpritchard.org/wp-content/uploads/2019/02/emme_notebook.png 1335w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /><figcaption>Emme fully modernized: an Emme 4 session with the IPython Notebook</figcaption></figure>



<p>But I&#8217;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&#8217;t quite complete yet.</p>



<p>Next sections:</p>



<ul class="wp-block-list"><li><a href="#ThePain">The Pain</a></li><li><a href="#ThePromise">The Promise</a></li><li><a href="#MyOwnPath">My Own Path</a></li><li><a href="#Conclusion">Conclusion</a></li></ul>



<a name="ThePain"></a><h2>The Pain</h2>



<h2 class="wp-block-heading">Menus &amp; Macros</h2>



<p>Analysis and database management in Emme 3 was still entirely a 1980s experience, driven by a console menu system. And &#8211; the kicker &#8211; 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 <font color="red">red</font>:</p>



<p><pre>3. M A T R I X   E D I T O R<br>3.01 Input / modify / output zone groups<br>3.11 Input matrices using batch entry<br>3.12 Input / modify matrices interactively<br>3.13 Plot matrices<br>3.14 Output matrices<br>3.16 Plot histogram of matrices<br>3.21 Matrix calculations<br>3.22 Matrix balancing<br>3.23 Triple-index Operations<br>Enter: Next module=<font color="red"><strong>3.12</strong></font><br><br>3.12 Input / modify matrices interactively<br>1= initialize a matrix<br>2= delete a matrix<br>3= modify a matrix<br>4= copy a matrix<br>5= list a matrix<br>6= list matrix directory<br>7= initialize matrix directory<br>8= change matrix protection flags<br>Enter: next=<font color="red">2</font><br>Enter: Matrix=<font color="red">mf13</font><br>mf13 diftrt transit time difference     (12-09-13 10:31:21)<br>Delete this matrix?<font color="red">y</font><br>Deleted<br><br>3.12 Input / modify matrices interactively<br>1= initialize a matrix<br>2= delete a matrix<br>3= modify a matrix<br>4= copy a matrix<br>5= list a matrix<br>6= list matrix directory<br>7= initialize matrix directory<br>8= change matrix protection flags<br>Enter: next= <font color="red">q</font></pre></p>



<p>Then your &#8220;source code&#8221; would look like this:</p>



<pre class="wp-block-preformatted">3.12<br>2<br>mf13<br>y<br>q</pre>



<p>Or, if you took advantage of the shorthand notation, you could instead say</p>



<pre class="wp-block-preformatted">~+|3.12|2|mf13|y|q</pre>



<p>Perfectly legible &#8211; a nice, succinct way to say &#8220;I deleted a matrix!&#8221;, right? The menus themselves are <em>mostly </em>in the manual, and they&#8217;re <em>mostly</em> 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&#8217;t exist, you&#8217;ll get a different series of questions.</p>



<h3 class="wp-block-heading">Variably Painful</h3>



<p>The data model is pretty straightforward, mostly:</p>



<ul class="wp-block-list"><li>a map represented as a graph of nodes and directed edges, with per-node and per-edge custom data</li><li>transit routes</li><li>origin-destination data such as matrices</li><li>global variables</li></ul>



<p>The pain is in working with this data. Most of these were accessed by numerical IDs, not by name &#8211; 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 &#8211; you can refer to them using a six-character name. Unfortunately, mf&#8221;ttawq6&#8243; wasn&#8217;t really enough to clearly define &#8220;am weighted travel time for the home-station leg of commuter rail trips&#8221;.</p>



<p>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.</p>



<a name="ThePromise"></a><h2>The Promise</h2>



<p>So why did people put up with this, instead of just using a normal GIS system &#8211; 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&#8217;d describe four main reasons:</p>



<ol class="wp-block-list"><li>A data model that includes transit lines.</li><li>Ready-made implementations of most of the standard operations you&#8217;ll need, like traffic and transit assignment, management of park &amp; ride demand, etc.</li><li>Base of existing trained users</li><li>A domain-specific GUI and graphing system.  (While I&#8217;ve complained here about the <em>unmodernized</em> parts of Emme 3, the GUI front-end was modernized and was on an equal footing with peers of its era like ArcGIS.)</li></ol>



<p>I have little experience with peer software like TransCAD or CUBE, but I&#8217;ve heard that Emme&#8217;s advantages over those include:</p>



<ol class="wp-block-list"><li>High-quality, battle-tested algorithms</li><li>Customizability </li></ol>



<p>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&#8217;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 <a href="http://www.spiess.ch/emme2/e2news/news01/node4.html">network calculator</a> or&#8230; uh&#8230; ArcINFO and its network tools?)</p>



<p>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.</p>



<a name="MyOwnPath"></a><h2>My Own Path</h2>



<p>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&#8217;t have to just repeat the earlier 3.12 incantation three times; instead, I had a nice file called <a href="/wp-content/uploads/2019/02/matrix-delete.mac">matrix-delete.mac</a> that allowed me to just write: </p>



<p>~&lt;matrix-delete mf3 mf4 mf15</p>



<p>The windows filenames aren&#8217;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&#8217;d praise the virtues of Microsoft&#8217;s 6~n.3 file naming.)</p>



<p>I&#8217;m sure that if I&#8217;d chosen some standard auxiliary tool as my second home &#8211; like shelling out to Python to generate custom macros on demand &#8211; I could have gone a lot further. But&#8230; taking on external dependencies is also a challenge, particularly when working in a team environment with a lot of non-software staff who can&#8217;t readily install/configure Python themselves. Not to mention having to fight the government IT staff every step of the way.</p>



<p>Good software practices can still emerge in such an environment &#8211; some day, ask me about how we implemented version control on our network data.</p>



<a name="Conclusion"></a><h2>Conclusion</h2>



<p>I&#8217;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&#8217;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&#8217;s vendor took the painful steps necessary to modernize 1980s-era software. I honestly agree with the vendor&#8217;s decisions &#8211; I&#8217;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 &#8220;clean slate rewrite.&#8221;</p>



<h2 class="wp-block-heading">References</h2>



<p>There&#8217;s a great article on the <a href="https://www.cirrelt.ca/DocumentsTravail/CIRRELT-2008-11.pdf">history of Emme</a> from Michael Florian. And there&#8217;s clearly some drama in the backstory &#8211; it&#8217;s clear that long-time contributor Heinz Spiess had a major falling out with the company in 2005, when the <a href="http://www.spiess.ch/emme2/archive/postscript/enifpaper.pdf">Unix GUI &#8220;Enif&#8221;</a> was abandoned in favour of the Emme 3&#8217;s Windows GUI. Speiss also keeps a <a href="http://emme2.enif.ch/e2news/e2news.html">great archive</a> of the 80s and 90s Emme 2 newsletters.</p>



<div class="wp-block-image"><figure class="aligncenter"><img loading="lazy" decoding="async" width="150" height="97" src="https://davidpritchard.org/wp-content/uploads/2019/02/emme_email.png" alt="" class="wp-image-826"/></figure></div>
]]></content:encoded>
					
					<wfw:commentRss>https://davidpritchard.org/archives/786/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Working with XAML Designer 2 (UwpSurface)</title>
		<link>https://davidpritchard.org/archives/736</link>
					<comments>https://davidpritchard.org/archives/736#comments</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 02 Nov 2018 19:19:57 +0000</pubDate>
				<category><![CDATA[User Interface]]></category>
		<category><![CDATA[windows]]></category>
		<guid isPermaLink="false">http://davidpritchard.org/?p=736</guid>

					<description><![CDATA[In September 2017, Microsoft released a rewritten XAML Designer program within Visual Studio. It&#8217;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 &#8230; <a href="https://davidpritchard.org/archives/736" class="more-link">Continue reading <span class="screen-reader-text">Working with XAML Designer 2 (UwpSurface)</span> <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<div class="wp-block-image">
<figure class="alignright"><a href="https://davidpritchard.org/wp-content/uploads/2018/11/designercrash.png"><img decoding="async" src="https://davidpritchard.org/wp-content/uploads/2018/11/designercrash-300x161.png" alt="" class="wp-image-740"/></a></figure>
</div>


<p>In September 2017, Microsoft released a <a href="https://blogs.msdn.microsoft.com/visualstudio/2017/09/11/a-significant-update-to-the-xaml-designer/">rewritten XAML Designer program</a> within Visual Studio. It&#8217;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.<br>For my app, the new Designer simply broke everything initially. Why? Presumably for technical reasons, it only works with <tt>{Binding}</tt> and not <tt>{x:Bind}&nbsp;</tt>&#8211; but this was not made at all clear in the launch announcements. UWP developers have been encouraged to use <tt>{x:Bind}</tt>: it&#8217;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 <tt>{x:Bind}</tt>&#8216;s <tt>FallbackValue</tt> parameter to preview different modes of my UI &#8211; but <tt>{Binding}</tt> has no equivalent mechanism.<br>After a <em><strong>lot</strong></em> of tinkering, I&#8217;ve finally learned a few important things about the new XAML Designer, and got a usable workflow.</p>



<h2 class="wp-block-heading">Top Takeaways</h2>



<ul class="wp-block-list">
<li>UwpSurface is much faster and stabler than XDesProc. The dialog above (&#8220;Click here to reload the designer&#8221;) is largely history.</li>



<li>It works for both 32-bit and 64-bit apps (x86 or x64), which is a big step forward</li>



<li>Only <tt>{Binding}</tt> is evaluated; <tt>{x:Bind}</tt> is completely ignored.</li>



<li>DesignInstances (a live ViewModel) can be attached via <tt>DataContext</tt> or <tt>d:DataContext</tt>, although the <tt>DesignInstance</tt> parameter doesn&#8217;t seem to work</li>



<li>ViewModel code executes, but I don&#8217;t see any indication that View code executes, despite discussion to the contrary in Microsoft&#8217;s launch blog post</li>



<li>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.<em>x</em>. They&#8217;re fine in C#.<br>(<em><strong>Update Jan. 2019:</strong> </em>Visual Studio 15.9 <a href="https://developercommunity.visualstudio.com/content/problem/374489/uwpsurface-and-ccx-design-instance.html">fixed this bug</a>, but ViewModels that implement property change notifications <a href="https://developercommunity.visualstudio.com/content/problem/396264/uwpsurface-and-ccx-design-instance-implementing-ic.html">still do not work</a>.)</li>



<li>You can <a href="https://github.com/MicrosoftDocs/visualstudio-docs/blob/master/docs/debugger/walkthrough-debugging-at-design-time.md">attach a debugger</a> and see debug output from your ViewModel, or any exceptions. I haven&#8217;t been able to set breakpoints.</li>



<li><em><strong>Update Jan. 2019:</strong></em> Visual Studio 15.9 added support for something I requested in mid-2018: the <tt>FallbackValue</tt> parameter was added to <tt>{Binding}</tt>, which makes that an equally viable way to work with XAML Designer. <tt>FallbackValue</tt> doesn&#8217;t work for <tt>{x:Bind}</tt> in the new Designer.</li>
</ul>



<h2 class="wp-block-heading">My strategy for a C++/CX app</h2>



<ul class="wp-block-list">
<li>Stick with <tt>{x:Bind}</tt> and its functions in most of my code</li>



<li>For the few properties that are central to a clean layout (usually about 2-5), use <tt>{Binding}</tt> and type converters</li>



<li>For those properties, build a duplicate &#8220;DesignTimeMock&#8221; ViewModel class &#8211; with no C++/CX base classes &#8211; and return the design-time property values there. Most properties can be safely omitted.</li>



<li>In the XAML code, define two different DataContexts like this:<br><pre>&lt;UserControl.DataContext><br>    &lt;local:MyViewModel /><br>&lt;/UserControl.DataContext><br>&lt;d:UserControl.DataContext><br>    &lt;local:MyViewModel_DesignTimeMock/><br>&lt;/d:UserControl.DataContext></pre><br><p>This attaches one ViewModel for runtime, and the mock for design time.</p><br></li>



<li><em><strong>Update Jan. 2019: </strong></em>Visual Studio 15.9 now allows a different approach: instead of a DesignTimeMock, you can just have a single DataContext using the &#8220;real&#8221; view model, but instead change <tt>{x:Bind}</tt> to <tt>{Binding}</tt> 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.</li>
</ul>



<p>It&#8217;s not ideal. But&#8230; it&#8217;s better than nothing.</p>



<h2 class="wp-block-heading">Closing Thoughts</h2>



<p>Despite the difficulties, I&nbsp;<em>do</em> 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&#8217;s just going to take some time to reach feature parity with the old version, which will mean some teething pain for &#8220;guinea pig&#8221; developers like myself.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://davidpritchard.org/archives/736/feed</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>Asynchronous Best Practices in C++/CX (Part 2)</title>
		<link>https://davidpritchard.org/archives/722</link>
					<comments>https://davidpritchard.org/archives/722#comments</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Tue, 12 Jun 2018 17:50:02 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[windows]]></category>
		<guid isPermaLink="false">http://davidpritchard.org/?p=722</guid>

					<description><![CDATA[This is part two in a series of articles on asynchronous coding in C++/CX. See the introduction here. 2. Be Aware of Thread Scheduling Rules 3. Be Aware of Object and Parameter Lifetimes]]></description>
										<content:encoded><![CDATA[<div class="wp-block-image">
<figure class="alignright"><a href="https://davidpritchard.org/wp-content/uploads/2018/11/title-1.png"><img decoding="async" src="https://davidpritchard.org/wp-content/uploads/2018/11/title-1.png" alt="" class="wp-image-715"/></a></figure>
</div>


<p>This is part two in a series of articles on asynchronous coding in C++/CX. See the <a href="https://davidpritchard.org/archives/712">introduction here</a>.</p>



<ol class="wp-block-list">
<li><a href="https://davidpritchard.org/archives/712#tip1">Prefer a task chain to nested tasks</a></li>



<li><a href="#tip2">Be aware of thread scheduling rules</a></li>



<li><a href="#tip3">Be aware of object and parameter lifetimes</a></li>



<li>Consider the effect of OS suspend/resume</li>



<li>Style: never put a try/catch block around a task chain.</li>



<li>References</li>
</ol>



<p><a name="tip2"></a></p>



<h2 class="wp-block-heading">2. Be Aware of Thread Scheduling Rules</h2>



<ul class="wp-block-list">
<li>When working with non-threadsafe code, you will usually prefer to continue <em>on the same thread</em> and not have to deal with multiple threads contending for the same data.</li>



<li></li>



<li>Helpfully, Universal Windows Platform (UWP) apps have a user interface thread inside a Single Threaded Apartment. On that thread, continuations (either a task <tt>.then()</tt> statement or code after a <tt>co_await</tt> statement) will <em><strong>almost always</strong></em> be scheduled to continue on the same thread.
<ul class="wp-block-list">
<li><strong>Exception #1:</strong> if the initial task in a chain is <em>not</em> an IAsyncAction or IAsyncOperation (or their WithProgress variants), then the continuation may &#8220;break out&#8221; of the apartment to a different thread.</li>



<li><strong>Exception #2:</strong> 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 <tt>.then()</tt> task-based continuation seemed to cause it, although I haven&#8217;t dug very deep into the exact conditions.</li>
</ul>
</li>



<li>If a continuation is <em>not</em> on the UI thread, then the guarantees go out the window. For threadpool threads, a continuation can be scheduled on any thread.
<ul class="wp-block-list">
<li>So, partway through a coroutine, you may find yourself on an entirely different thread.</li>



<li>I imagine this is to prevent &#8220;starvation&#8221; of asynchronous chains when running on a compute-intensive thread, for example. But it can be quite a gotcha.</li>



<li>The <tt>concurrency::create_task()</tt> method can take a <tt>task_continuation_context::use_current()</tt> parameter to (try) to force execution to stay on the same thread, but there&#8217;s no equivalent for coroutines. It&#8217;s not clear to me that this always works, however; I need to test further and explore.</li>
</ul>
</li>



<li><span style="font-weight: 400;">When in doubt: use <tt>std::this_thread::get_id()</tt> to check the thread id over the course of an asynchronous chain of operation.</span></li>



<li>When on a threadpool thread, my preference is to write a <em>standalone coroutine </em>in a <em>functional style </em>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&#8217;ll write an accompanying method that calls the coroutine, then schedules a final task <em>on the original thread</em> to mutate the object. Here&#8217;s an example:<br><pre style="font-size: 9pt;">class MyClass {<br>private:<br>  static concurrency::task&lt;std::wstring> Coroutine(wstring filename) {<br>    // ... do some stuff, open file with co_await, read data, etc...<br>    co_return result;<br>  }<br>public:<br>  void MemberFnAsync() {<br>    concurrency::create_task(Coroutine(m_filename),<br>      task_continuation_context::use_current())<br>    .then([this](wstring result) {<br>      this->SetTextFromFile(result);<br>    });<br>  }<br>};</pre><br></li>
</ul>



<p><a name="tip3"></a></p>



<h2 class="wp-block-heading">3. Be Aware of Object and Parameter Lifetimes</h2>



<ul class="wp-block-list">
<li>When passing data into asynchronous code, you want to <em>pass by value.</em> That applies to both lambda captures (for continuation tasks) and to coroutine parameters.</li>



<li>Why? If you pass do a traditional C++ calling convention like this:<br><pre style="font-size: 9pt;">concurrency::task&lt;void> MyCoroutineAsync (const wstring &amp;str) {<br>// do some stuff.<br>co_await OtherFn();<br>AnotherFn(str);<br>}</pre><br><p>&#8230; the <tt>str</tt> parameter will typically be valid until the first <tt>co_await</tt> statement, but at that point execution will return to the caller until <tt>OtherFn()</tt> completes. The <tt>str</tt> parameter may well then be destroyed—especially if it was an rvalue or a stack variable. By the time the coroutine resumes and <tt>AnotherFn()</tt> is called, <tt>str</tt> will be a dangling pointer.</p></li>



<li>The underlying issue is that the coroutine task chain <em>outlives its caller</em> and therefore can&#8217;t count on references to data from its caller.</li>



<li>This applies to reference and pointer data. Pass-by-value is usually preferred, but of course smart pointer types are also valid.
<ul class="wp-block-list">
<li>Just be sure to pass smart pointers in a manner that creates a separate reference count: <tt>std::shared_ptr&lt;T></tt>, not <tt>const std::shared_ptr&lt;T>&amp;</tt></li>
</ul>
</li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://davidpritchard.org/archives/722/feed</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>Asynchronous Best Practices in C++/CX (Part 1)</title>
		<link>https://davidpritchard.org/archives/712</link>
					<comments>https://davidpritchard.org/archives/712#comments</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Thu, 07 Jun 2018 15:18:29 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[windows]]></category>
		<guid isPermaLink="false">http://davidpritchard.org/?p=712</guid>

					<description><![CDATA[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&#160;in many cases you can&#8217;t even call the synchronous equivalent from Win32 or C. This includes networking operations, &#8230; <a href="https://davidpritchard.org/archives/712" class="more-link">Continue reading <span class="screen-reader-text">Asynchronous Best Practices in C++/CX (Part 1)</span> <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<div class="wp-block-image">
<figure class="alignright"><a href="https://davidpritchard.org/wp-content/uploads/2018/11/title-1.png"><img decoding="async" src="https://davidpritchard.org/wp-content/uploads/2018/11/title-1-300x107.png" alt="" class="wp-image-715"/></a></figure>
</div>


<p>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&nbsp;in many cases you can&#8217;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 &#8220;best practices&#8221; to share.<br>
C++/CX offers two different approaches for dealing with asynchronous operations:</p>



<ol class="wp-block-list">
<li>task continuations using the Parallel Patterns Library (PPL)</li>



<li>coroutines (as of early 2016)</li>
</ol>



<p>Personally, I vastly prefer coroutines; having <code>co_await</code> gives C++/CX a distinctly C# flavour, and the entire API starts to feel &#8220;natural.&#8221; However, at my current job we have not yet standardized on coroutines, and have a mix of both approaches instead. And to be fair &#8211; despite Microsoft&#8217;s <a href="https://blogs.msdn.microsoft.com/vcblog/2016/04/04/using-c-coroutines-to-simplify-async-uwp-code/">assurances</a> that they are &#8220;production ready&#8221;, I&#8217;ve personally hit a few coroutine <a href="https://developercommunity.visualstudio.com/content/problem/5849/co-await-trashes-lambda-captures.html">bugs</a> and they do occasionally <a href="https://developercommunity.visualstudio.com/content/problem/263131/co-await-optimization-bugs-in-visual-studio-157-x6.html">completely break</a> with compiler updates.<br>
I&#8217;m going to write up my advice in a series of posts, as the examples can be pretty lengthy.</p>



<ol class="wp-block-list">
<li><a href="#tip1">Prefer a task chain to nested tasks</a></li>



<li><a href="https://davidpritchard.org/archives/722#tip2">Be aware of thread scheduling rules</a></li>



<li><a href="https://davidpritchard.org/archives/722#tip3">Stay aware of object and parameter lifetimes</a></li>



<li>Consider the effect of OS suspend/resume</li>



<li>Style: never put a try/catch block around a task chain.</li>



<li>References</li>
</ol>



<h2 class="wp-block-heading">1. Prefer a Task Chain to Nested Tasks</h2>



<p>When writing a series of API calls that need local variables, conditional logic, or loops, it&#8217;s tempting to write it as a nest of tasks. But a nest will:</p>



<ul class="wp-block-list">
<li><strong>hurts legibility:</strong> an extra indent level for every sequential API call, and very wordy</li>



<li></li>



<li><strong>painful/missing exception handling: </strong>every level of the nest needs its own exception handler. Exceptions will<strong> </strong><em>not</em> propagate automatically from an inner task to the outer exception handler, but will instead raise an unobserved exception error.</li>



<li><strong>makes it hard to return a waitable/gettable task</strong> that can track when the entire chain completes.</li>
</ul>



<p>Consider this example nested code:</p>



<pre class="wp-block-preformatted">concurrency::create_task(folder-&gt;GetFileAsync())
.then([](StorageFile ^file) {
  if(file != nullptr) {
    concurrency::create_task(file-&gt;ReadAsync())
    .then([](IRandomAccessStream ^stream) {
      /* do something with stream */
    })
    .then([](concurrency::task&lt;void&gt; t) {
      try { t.get(); }
      catch (COMException ^) { /* do something */}
      catch (...) { /* do something */ }
    });
  }
})
.then([](concurrency::task&lt;void&gt; t) {
  try { t.get(); }
  catch (COMException ^) { /* do something */}
  catch (...) { /* do something */ }
});</pre>



<p>Here&#8217;s the equivalent implemented as a task chain:</p>



<pre class="wp-block-preformatted">concurrency::create_task(folder-&gt;GetFileAsync())
.then([](StorageFile ^file) {
   if(file == nullptr)
     concurrency::cancel_current_task();
   return file-&gt;ReadAsync();
}).then([](IRandomAccessStream ^stream) {
   /* do something with stream */
}).then([](concurrency::task&lt;void&gt; t) {
  &nbsp;try { t.get(); }
   catch (std::task_cancelled ) { }
   catch (COMException ^) { /* do something */}
   catch (...) { /* do something */ }
});</pre>



<p>Let&#8217;s look at a few other situations and the best way to make a chain:</p>



<ul class="wp-block-list">
<li><strong>Need access to local variable in earlier stage</strong> 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:<br><pre style="font-size: 9pt;">struct StackFrame { String ^filename; }<br>auto stack = std::make_shared&lt;StackFrame>();<br>concurrency::create_task(folder->GetFileAsync())<br>.then([stack](StorageFile ^file) {<br>   stack->filename = file->Name;<br>   return file->ReadAsync();<br>}).then([stack](IRandomAccessStream ^stream) {<br>   /* do something with stream and stack->filename */<br>})</pre><br></li>



<li><strong>Need a loop with asynchronous calls on each item</strong>: collect the tasks for each item in the loop into a std::vector and return when_all on that vector.<br><pre style="font-size: 9pt;">concurrency::create_task(folder->GetFilesAsync())<br>.then([stack](IVector&lt;StorageFile> ^files) {<br>   std::vector&lt;concurrency::task&lt;void> > tasks;<br>   for (auto file : files) {<br>     tasks.push_back(concurrency::create_task(file->DeleteAsync()));<br>   return concurrency::when_all(tasks.begin(), tasks.end());<br>});</pre><br></li>
</ul>



<p>Of course—just in case you&#8217;re curious—as a coroutine this is all trivial and highly readable.</p>



<pre class="wp-block-preformatted">try {
  StorageFile ^file = co_await folder-&gt;GetFileAsync();
  if (file != nullptr) {
    IRandomAccessStream ^stream = co_await file-&gt;ReadAsync();
    /* do something with stream and file-&gt;Name */
  }
}
catch (COMException ^) { /* do something */}
catch (...) { /* do something */ }</pre>



<p>Continues in <a href="https://davidpritchard.org/archives/722">Part 2</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://davidpritchard.org/archives/712/feed</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>This One Weird Grid Trick in XAML</title>
		<link>https://davidpritchard.org/archives/706</link>
					<comments>https://davidpritchard.org/archives/706#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 01 Jun 2018 16:32:29 +0000</pubDate>
				<category><![CDATA[User Interface]]></category>
		<category><![CDATA[windows]]></category>
		<guid isPermaLink="false">http://davidpritchard.org/?p=706</guid>

					<description><![CDATA[I recently found a neat XAML user interface trick that I hadn&#8217;t seen in my usual resources. Suppose you have: XAML makes it easy to do&#160;proportional&#160;space allocation &#8211; e.g., give row #1 two-thirds and row #2 one-third by giving them &#8220;2*&#8221; and &#8220;*&#8221; height respectively. But it doesn&#8217;t do priorities. The trick: combine a massive &#8230; <a href="https://davidpritchard.org/archives/706" class="more-link">Continue reading <span class="screen-reader-text">This One Weird Grid Trick in XAML</span> <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<div class="wp-block-image">
<figure class="alignright"><img loading="lazy" decoding="async" width="300" height="237" src="https://davidpritchard.org/wp-content/uploads/2018/11/rows-300x237.png" alt="" class="wp-image-708"/></figure>
</div>


<p>I recently found a neat XAML user interface trick that I hadn&#8217;t seen in my <a href="https://wpf.2000things.com/tag/grid/">usual resources</a>. Suppose you have:</p>



<ul class="wp-block-list">
<li>a grid-based responsive user interface that you want to grow/shrink to fit the window</li>



<li>suppose it has a fixed width, and each row has a &#8220;core&#8221; minimum height it needs.</li>



<li>then there&#8217;s some extra vertical space that you want to sprinkle around</li>



<li>you have some <em>priorities</em> &#8211; first give row #1 extra space, then row #2 any extra.</li>
</ul>



<p>XAML makes it easy to do&nbsp;<em>proportional</em>&nbsp;space allocation &#8211; e.g., give row #1 two-thirds and row #2 one-third by giving them &#8220;2*&#8221; and &#8220;*&#8221; height respectively. But it doesn&#8217;t do priorities.<br>
The trick: combine a massive star size with a MaxHeight. That looks like this:</p>



<pre class="wp-block-preformatted">&lt;Grid&gt;
  &lt;Grid.RowDefinitions&gt;
    &lt;RowDefinition Height="1000*" MaxHeight="200" /&gt;
    &lt;RowDefinition Height="*" /&gt;
  &lt;/Grid.RowDefinitions&gt;
&lt;/Grid&gt;</pre>



<p>Essentially, row #1 gets &#8220;first claim&#8221; on any extra space, up to a limit of 200 pixels. Any extra space beyond 200 pixels falls over to row #2.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://davidpritchard.org/archives/706/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>My First XAML Tips for the Universal Windows Platform</title>
		<link>https://davidpritchard.org/archives/678</link>
					<comments>https://davidpritchard.org/archives/678#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Thu, 02 Nov 2017 01:56:48 +0000</pubDate>
				<category><![CDATA[User Interface]]></category>
		<category><![CDATA[windows]]></category>
		<guid isPermaLink="false">http://davidpritchard.org/?p=678</guid>

					<description><![CDATA[I&#8217;m a latecomer to Microsoft&#8217;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 &#8220;Universal Windows Platform&#8221; (UWP) &#8211; that is, the Windows 10 &#8230; <a href="https://davidpritchard.org/archives/678" class="more-link">Continue reading <span class="screen-reader-text">My First XAML Tips for the Universal Windows Platform</span> <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<div class="wp-block-image">
<figure class="alignright"><a href="https://davidpritchard.org/wp-content/uploads/2018/11/xaml-e1509587351270.png"><img decoding="async" src="https://davidpritchard.org/wp-content/uploads/2018/11/xaml-e1509587351270.png" alt="" class="wp-image-684"/></a></figure>
</div>


<p>I&#8217;m a latecomer to Microsoft&#8217;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 &#8220;Universal Windows Platform&#8221; (UWP) &#8211; that is, the Windows 10 user interface layer (and Win8).<br>
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&#8217;s old enough that much of the Google search hits and StackOverflow results are not applicable on the modern UWP platform.<br>
<a href="https://davidpritchard.org/wp-content/uploads/2018/11/xaml-1.png"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-681" src="https://davidpritchard.org/wp-content/uploads/2018/11/xaml-1.png" alt="" width="813" height="358"></a></p>



<h2 class="wp-block-heading">My Tips</h2>



<p>So what were a few of my first lessons when using XAML on UWP?<br>
</p>



<span id="more-678"></span>



<ul class="wp-block-list">
<li><a href="#prefer">Prefer x:Bind to Binding</a></li>



<li><a href="#responsive">Responsive Design</a></li>



<li><a href="#expressions">Expressions in x:Bind calls</a></li>



<li><a href="#nocodebehind">Prefer XAML or ViewModels over code-behind</a></li>



<li><a href="#designer">Use XAML Designer</a></li>



<li><a href="#commands">Use Commands, get disabling for free</a></li>



<li><a href="#more">More to read</a></li>
</ul>



<p><a name="prefer"></a></p>



<h2 class="wp-block-heading">Prefer x:Bind to Binding</h2>



<p>The main reason: x:Bind gives you compile-time checks and better performance. Be aware of the small differences:</p>



<ul class="wp-block-list">
<li>OneTime binding by default instead of OneWay binding</li>



<li>Binding path is the framework element (control) not the DataContext (view model)</li>
</ul>



<p><a name="responsive"></a></p>



<h2 class="wp-block-heading">Responsive Design</h2>



<p>Combining VisualState and AdaptiveTriggers is quite a powerful way to hide/show, relocate<br>
or resize different parts of a user interface. The only pain point with this is that you can&#8217;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&#8217;t be previewed in XAML Designer, which I found useful. Instead, I created a dummy invisible control, set the triggers to modify that control,<br>
and then bound the style to the invisible control&#8217;s properties. This works both &#8220;live&#8221; and in XAML Designer, even updating live as the XAML Designer target device resolution is changed.<br>
I found <a href="https://www.codeproject.com/Articles/896974/Advanced-View-States-for-Windows-apps">this </a>reference useful on this subject.<br>
<a name="expressions"></a></p>



<h2 class="wp-block-heading">Expressions in x:Bind calls</h2>



<p>In the Windows Anniversary Edition (Fall 2016), Microsoft added some <em>great</em> new<br>
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:</p>



<pre class="wp-block-preformatted">&lt;Button Visibility="{x:Bind Vm.xAnd(Vm.IsVisible, Vm.IsStateOn)}"
 Width={x:Bind Vm.xMult(Vm.Width, 0.5)}"&gt;</pre>



<p>Notice a few things here:</p>



<ul class="wp-block-list">
<li>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. <strong>Always prefer x:Bind expressions to convertors.</strong></li>



<li>you can write single or multi-operand functions. (In Visual Studio 2015, XAML Designer didn&#8217;t support multi-operand, but Visual Studio 2017 added full support.)</li>



<li>you can embed constants and work with integer or double types</li>



<li>you cannot nest functions or overload function names</li>
</ul>



<p>This opens up a huge number of ways of writing cleaner and more readable XAML code.<br>
<a name="nocodebehind"></a></p>



<h2 class="wp-block-heading">Prefer XAML or ViewModels over code-behind</h2>



<p>Code-behind (the C++ or C# source for the XAML View) should be a last resort. It&#8217;s already challenging enough to keep track of a XAML file and a ViewModel; burying logic in the View divides the reader&#8217;s attention even further.<br>
There are situations where something is part of the &#8220;View&#8221; rather than the ViewModel: for example, a property that has nothing to do with the Model and only exists to enable the XAML implementation &#8211; 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 &#8211; if you can do it in XAML, that&#8217;s usually better.<br>
<a name="designer"></a></p>



<h2 class="wp-block-heading">Use XAML Designer</h2>



<p>XAML Designer &#8211; the Visual Studio built-in visual editor for XAML &#8211; hasn&#8217;t had much love in a long time. Many things are broken, and it can&#8217;t handle some useful things. And if you don&#8217;t put effort into setting up the XAML correctly, a page/control can be totally illegible.<br>
Why prefer XAML Designer?</p>



<ul class="wp-block-list">
<li>Compile times for XAML &#8211; especially include C++ code generations &#8211; are brutal. You can iterate much faster if you avoid the compile.</li>



<li>XAML Designer can do a lot of responsive design without even doing a build</li>



<li>It&#8217;s a much quicker way to learn XAML, and even explore events/properties that you weren&#8217;t aware of.</li>
</ul>



<p>How to make XAML Designer more useable:</p>



<ul class="wp-block-list">
<li>Ensure your UserControl has d:DesignWidth and d:DesignHeight settings</li>



<li>Ensure ViewModel has a default zero-parameter constructor</li>



<li>Bind the ViewModel to the DataContext in XAML, not in code-behind. e.g.,<br><pre>&lt;Page.DataContext&gt;&lt;local:MyViewModel/&gt;&lt;/Page.DataContext&gt;</pre><br></li>



<li>Use AdaptiveTriggers for responsive design, rather than ViewModel or View logic</li>



<li>Add FallbackValues for x:Binds that XAML Designer can&#8217;t understand. They&#8217;ll only be used at design-time.</li>



<li>Don&#8217;t bother trying to get &#8220;real&#8221; design-time data working via d:DesignInstance; it doesn&#8217;t work with a 64-bit application or with x:Bind.<br><br>(In theory, you&#8217;re supposed to be able to write<br><pre>d:DataContext="{d:DesignInstance local:MyViewModel, IsDesignTimeCreatable=True}"</pre><br><p>and get XAML Designer to actually construct an instance of your real ViewModel, and query its values to get the &#8220;right&#8221; default values when you&#8217;re designing. But&#8230; it clearly hasn&#8217;t been maintained in a long time, it only works in 32-bit and with Binding.)</p></li>
</ul>



<p><a name="commands"></a></p>



<h2 class="wp-block-heading">Use Commands, get disabling for free</h2>



<p>Why use Commands as the mechanism for the XAML/View to modify the view model&#8217;s state?<br>
For one, it&#8217;s often less code: XAML can directly bind to the viewmodel&#8217;s command property, instead of XAML having a click event, and the View&#8217;s click event codebehind then modifies the viewmodel.<br>
But also: you can set it up so that when the command &#8220;cannot execute&#8221;, then the user interface will automatically grey out and prevent clicking. The details:</p>



<ul class="wp-block-list">
<li>the command should be a ViewModel property, it should implement a CanExecute method, and that CanExecute method should issue a PropertyChanged notification</li>



<li>the XAML control should be bound to the command</li>



<li>when the command cannot be executed, the XAML control will then enter the &#8220;Disabled&#8221; visual state.<br><br><a name="more"></a></li>
</ul>



<h2 class="wp-block-heading">More to Read</h2>



<ul class="wp-block-list">
<li><a href="https://github.com/futurice/windows-app-development-best-practices">Best Practices for Developing UWP Applications</a>: a bit C#-centric, but some good tips
<ul class="wp-block-list">
<li>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.</li>



<li>Use the Live Visual Tree and Live Property Explorer</li>



<li>Use templated controls over user controls</li>



<li>If you have troubles with Visual Studio stability (or speed), try disabling XAML Designer</li>
</ul>
</li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://davidpritchard.org/archives/678/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
