<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>NSBlog</title><link>http://www.mikeash.com/pyblog/</link><description>Mac OS X and Cocoa programming</description><lastBuildDate>Sun, 12 Apr 2026 02:24:10 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>objc_msgSend's New Prototype
</title><link>http://www.mikeash.com/pyblog/objc_msgsends-new-prototype.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;objc_msgSend's New Prototype
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2019 10 11  12 09"
                  tags="objc"
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;objc_msgSend's New Prototype
&lt;/div&gt;
              &lt;p&gt;Apple's new OSes are out. If you've looked through the documentation, you may have noticed that &lt;a href="https://developer.apple.com/documentation/objectivec/1456712-objc_msgsend"&gt;the prototype for &lt;code&gt;objc_msgSend&lt;/code&gt; has changed&lt;/a&gt;. Previously, it was declared as a function that took &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;SEL&lt;/code&gt;, and variadic arguments after that, and returned &lt;code&gt;id&lt;/code&gt;. Now it's declared as a function that takes and returns &lt;code&gt;void&lt;/code&gt;. Similar functions like &lt;code&gt;objc_msgSendSuper&lt;/code&gt; also became &lt;code&gt;void&lt;/code&gt;/&lt;code&gt;void&lt;/code&gt;. Why the change?&lt;/p&gt;

&lt;p&gt;&lt;b&gt;The True Prototype&lt;/b&gt;&lt;br&gt;There's a big and surprisingly difficult question behind this: what is the &lt;em&gt;true&lt;/em&gt; prototype of &lt;code&gt;objc_msgSend&lt;/code&gt;? That is to say, what parameters does it actually take, and what does it actually return? This question doesn't have a straightforward answer.&lt;/p&gt;

&lt;p&gt;You may have heard that &lt;code&gt;objc_msgSend&lt;/code&gt; is implemented in assembly because it's so commonly called that it needs every bit of performance it can get. This is true, but not entirely complete. It's not possible to implement it in C at &lt;em&gt;any&lt;/em&gt; speed.&lt;/p&gt;

&lt;p&gt;The fast path of &lt;code&gt;objc_msgSend&lt;/code&gt; does a few critical things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Load the class of the object.&lt;/li&gt;
&lt;li&gt;Look up the selector in that class's method cache.&lt;/li&gt;
&lt;li&gt;Jump to the method implementation found in the cache.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;From the perspective of the method implementation, it looks like the caller invoked it directly. Because &lt;code&gt;objc_msgSend&lt;/code&gt; jumps straight to the method implementation without making a function call, it effectively disappears once its job is done. The implementation is careful not to disturb any of the registers that can be used to pass arguments to a function. The caller calls &lt;code&gt;objc_msgSend&lt;/code&gt; &lt;em&gt;as if&lt;/em&gt; it was going to directly call the method implementation, passing all of the parameters in the same way it would for a direct function call. Once &lt;code&gt;objc_msgSend&lt;/code&gt; looks up the implementation and jumps to it, those parameters are still exactly where the implementation expects them to be. When the implementation returns, it returns directly to the caller, and the return value is provided by the standard mechanism.&lt;/p&gt;

&lt;p&gt;This answers the above question: the prototype of &lt;code&gt;objc_msgSend&lt;/code&gt; is that of the method implementation it ends up calling.&lt;/p&gt;

&lt;p&gt;But wait, isn't the whole point of dynamic method lookup and message sending that you don't know what method implementation you'll be calling? This is true! However, you do know what &lt;em&gt;type signature&lt;/em&gt; the implementation will have. The compiler can get this information from the declaration of the method in an &lt;code&gt;@interface&lt;/code&gt; or &lt;code&gt;@protocol&lt;/code&gt; block, and uses that to generate the appropriate parameter passing and return value fetching code. If you override a method, the compiler complains if you don't match the type signature. It's possible to work around this by hiding declarations or adding methods at runtime, and in that case you can end up with a type signature for a method implementation that doesn't match the call site. The behavior of such a call then depends on how those two type signatures match up at the ABI level, with anything from perfectly reasonable and correct behavior (if the ABIs match so all the parameters happen to line up) to complete nonsense (if they don't).&lt;/p&gt;

&lt;p&gt;This hints at an answer to this article's question: the old prototype worked in some circumstances (when the ABIs matched) and failed strangely in others (when the ABIs didn't match). The new prototype never works unless you cast it to the appropriate type first. As long as you cast it to the correct type, it always works. The new way of doing things thus encourages doing things correctly and makes it harder to do things wrong.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;The Minimal Prototype&lt;/b&gt;&lt;br&gt;Although the prototype of &lt;code&gt;objc_msgSend&lt;/code&gt; depends on the method implementation that will be called, there are two things that are common across all method implementations: the first parameter is always &lt;code&gt;id self&lt;/code&gt;, and the second parameter is always &lt;code&gt;SEL _cmd&lt;/code&gt;. The number and type of any additional parameters is unknown, as is the return type, but those two parameters are known. &lt;code&gt;objc_msgSend&lt;/code&gt; needs these two pieces of information to perform its method dispatch work, so they always have to be in the same place for it to be able to find them.&lt;/p&gt;

&lt;p&gt;We could write an approximate generalized prototype for &lt;code&gt;objc_msgSend&lt;/code&gt; to represent this:&lt;/p&gt;

&lt;pre&gt;    ??? objc_msgSend(id self, SEL _cmd, ???)
&lt;/pre&gt;

&lt;p&gt;Where &lt;code&gt;???&lt;/code&gt; means that we don't know, and it depends on the particular method implementation that will be called. Of course, C has no way to represent a wildcard like this.&lt;/p&gt;

&lt;p&gt;For the return value, we can try to pick something common. Since Objective-C is all about objects, it would make sense to assume the return value is &lt;code&gt;id&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;    id objc_msgSend(id self, SEL _cmd, ???)
&lt;/pre&gt;

&lt;p&gt;This not only covers cases where the return value is an object, but also cases where it's &lt;code&gt;void&lt;/code&gt; and some other cases where it's a different type but the value isn't used.&lt;/p&gt;

&lt;p&gt;How about the parameters? C actually does have a way to indicate an arbitrary number of parameters of arbitrary types, in the form of variadic function prototypes. An ellipsis at the end of the parameter list means that a variable number of arbitrarily typed values follows:&lt;/p&gt;

&lt;pre&gt;    id objc_msgSend(id self, SEL _cmd, ...)
&lt;/pre&gt;

&lt;p&gt;This is exactly what the prototype used to be before the recent change.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;ABI Mismatches&lt;/b&gt;&lt;br&gt;The pertinent question at runtime is whether the ABI at the call site matches the ABI of the method implementation. Which is to say, will the receiver retrieve the parameters from the same location and in the same format that the caller passes them? If the caller puts a parameter into &lt;code&gt;$rdx&lt;/code&gt; then the implementation needs to retrieve that parameter from &lt;code&gt;$rdx&lt;/code&gt;, otherwise havoc will ensue.&lt;/p&gt;

&lt;p&gt;The minimal prototype may be able to express the concept of passing an arbitrary number of arbitrary types, but for it to actually work at runtime, it needs to use the same ABI as the method implementation. That implementation is almost certainly using a different prototype, and usually has a fixed number of arguments.&lt;/p&gt;

&lt;p&gt;There is no guarantee that the ABI for a variadic function matches the ABI for a function with a fixed number of arguments. On some platforms, they match almost perfectly. On others, they don't match at all.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Intel ABI&lt;/b&gt;&lt;br&gt;Let's look at a concrete example. macOS uses the standard &lt;a href="https://www.uclibc.org/docs/psABI-x86_64.pdf"&gt;System V ABI for x86-64&lt;/a&gt;. There is a ton of detail in the ABI, but we'll focus on the basics.&lt;/p&gt;

&lt;p&gt;Parameters are passed in registers. Integer parameters are passed in registers &lt;code&gt;rdi&lt;/code&gt;, &lt;code&gt;rsi&lt;/code&gt;, &lt;code&gt;rdx&lt;/code&gt;, &lt;code&gt;rcx&lt;/code&gt;, &lt;code&gt;r8&lt;/code&gt;, and &lt;code&gt;r9&lt;/code&gt;, in that order. Floating point parameters are passed in the SSE registers &lt;code&gt;xmm0&lt;/code&gt; through &lt;code&gt;xmm7&lt;/code&gt;. When calling a variadic function, the register &lt;code&gt;al&lt;/code&gt; is set to the number of SSE registers that were used to pass parameters. Integer return values are placed in &lt;code&gt;rax&lt;/code&gt; and &lt;code&gt;rdx&lt;/code&gt;, and floating-point return values are placed in &lt;code&gt;xmm0&lt;/code&gt; and &lt;code&gt;xmm1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The ABI for variadic functions is almost identical to the ABI for normal functions. The one exception is passing the number of SSE registers used in &lt;code&gt;al&lt;/code&gt;. However, this is harmless when using the variadic ABI to call a normal function, as the normal function will ignore the contents of &lt;code&gt;al&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The C language messes things up a bit. C specifies that certain types get promoted to wider types when passed as a variadic argument. Integers smaller than &lt;code&gt;int&lt;/code&gt; (such as &lt;code&gt;char&lt;/code&gt; and &lt;code&gt;short&lt;/code&gt;) get promoted to &lt;code&gt;int&lt;/code&gt;, and &lt;code&gt;float&lt;/code&gt; gets promoted to &lt;code&gt;double&lt;/code&gt;. If your method signature includes one of these types, it's not possible for a caller to pass a parameter as that exact type if it's using a variadic prototype.&lt;/p&gt;

&lt;p&gt;For integers, this doesn't actually matter. The integer gets stored in the bottom bits of the appropriate register, and the bits end up in the same place either way. However, it's catastrophic for &lt;code&gt;float&lt;/code&gt;. Converting a smaller integer to an &lt;code&gt;int&lt;/code&gt; just requires padding it out with extra bits. Converting &lt;code&gt;float&lt;/code&gt; to &lt;code&gt;double&lt;/code&gt; involves converting the value to a different structure altogether. The bits in a &lt;code&gt;float&lt;/code&gt; don't line up with the corresponding bits in a &lt;code&gt;double&lt;/code&gt;. If you try to use a variadic prototype to call a non-variadic function that takes a &lt;code&gt;float&lt;/code&gt; parameter, that function will receive garbage.&lt;/p&gt;

&lt;p&gt;To illustrate this problem, here's a quick example:&lt;/p&gt;

&lt;pre&gt;    // Use the old variadic prototype for objc_msgSend.
    #define OBJC_OLD_DISPATCH_PROTOTYPES 1

    #import &amp;lt;Foundation/Foundation.h&amp;gt;
    #import &amp;lt;objc/message.h&amp;gt;

    @interface Foo : NSObject @end
    @implementation Foo
    - (void)log: (float)x {
        printf("%f\n", x);
    }
    @end

    int main(int argc, char **argv) {
        id obj = [Foo new];
        [obj log: (float)M_PI];
        objc_msgSend(obj, @selector(log:), (float)M_PI);
    }
&lt;/pre&gt;

&lt;p&gt;It produces this output:&lt;/p&gt;

&lt;pre&gt;    3.141593
    3370280550400.000000
&lt;/pre&gt;

&lt;p&gt;As you can see, the value came through correctly when written as a message send, but got completely mangled when passed through an explicit call to &lt;code&gt;objc_msgSend&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This can be remedied by casting &lt;code&gt;objc_msgSend&lt;/code&gt; to have the right signature. Recall that &lt;code&gt;objc_msgSend&lt;/code&gt;'s actual prototype is that of whatever method will end up being invoked, so the correct way to use it is to cast it to the corresponding function pointer type. This call works correctly:&lt;/p&gt;

&lt;pre&gt;    ((void (*)(id, SEL, float))objc_msgSend)(obj, @selector(log:), M_PI);
&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;ARM64 ABI&lt;/b&gt;&lt;br&gt;Let's look at another relevant example. iOS uses &lt;a href="https://developer.apple.com/library/archive/documentation/Xcode/Conceptual/iPhoneOSABIReference/Articles/ARM64FunctionCallingConventions.html"&gt;a variation on the standard ABI for ARM64&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Integer parameters are passed in registers &lt;code&gt;x0&lt;/code&gt; through &lt;code&gt;x7&lt;/code&gt;. Floating point parameters are passed in &lt;code&gt;v0&lt;/code&gt; through &lt;code&gt;v7&lt;/code&gt;. Additional parameters are passed on the stack. Return values are placed in the same register or registers where they would be passed as parameters.&lt;/p&gt;

&lt;p&gt;This is only true for normal parameters. Variadic parameters are never passed in registers. They are always passed on the stack, even when parameter registers are available.&lt;/p&gt;

&lt;p&gt;There's no need for a careful analysis of how this will work out in practice. The ABIs are completely mismatched and a method called with an uncast &lt;code&gt;objc_msgSend&lt;/code&gt; will receive garbage in its parameters.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;The New Prototype&lt;/b&gt;&lt;br&gt;The new prototype is short and sweet:&lt;/p&gt;

&lt;pre&gt;    void objc_msgSend(void);
&lt;/pre&gt;

&lt;p&gt;This isn't correct at all. However, neither was the old prototype. This one is much more &lt;em&gt;obviously&lt;/em&gt; incorrect, and that's a good thing. The old prototype made it easy to to use it without casting it, and worked often enough that you could easily end up thinking everything was OK. When you hit the problematic cases, the bugs were very unclear.&lt;/p&gt;

&lt;p&gt;This prototype doesn't even allow you to pass the two required parameters of &lt;code&gt;self&lt;/code&gt; and &lt;code&gt;_cmd&lt;/code&gt;. You can call it with no parameters at all, but it'll immediately crash and it should be pretty obvious about what went wrong. If you try to use it without casting, the compiler will complain, which is much better than weird broken parameter values.&lt;/p&gt;

&lt;p&gt;Because it still has a function type, you can still cast it to a function pointer of the appropriate type and invoke it that way. This will work correctly as long as you get the types right.&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;
</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/objc_msgsends-new-prototype.html</guid><pubDate>Fri, 11 Oct 2019 12:09:00 GMT</pubDate></item><item><title>Friday Q&amp;A 2018-06-29: Debugging with C-Reduce
</title><link>http://www.mikeash.com/pyblog/friday-qa-2018-06-29-debugging-with-c-reduce.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;Friday Q&amp;amp;A 2018-06-29: Debugging with C-Reduce
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2018 06 29  13 35"
                  tags="fridayqna debugging"
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;Friday Q&amp;amp;A 2018-06-29: Debugging with C-Reduce
&lt;/div&gt;
              &lt;p&gt;Debugging a complex problem is tough, and it can be especially difficult when it's not obvious which chunk of code is responsible. It's common to attempt to produce a reduced test case in order to narrow it down. It's tedious to do this manually, but it's also the sort of thing computers are really good at. C-Reduce is a program which automatically takes programs and pares them down to produce a reduced test case. Let's take a look at how to use it.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Overview&lt;/b&gt;&lt;br&gt;C-Reduce is based on two main ideas.&lt;/p&gt;

&lt;p&gt;First, there's the idea of a reduction pass. This is a transformation performed on some source code which produces a reduced version of that code. C-Reduce has a bunch of different passes, including things like deleting lines or renaming tokens to shorter versions.&lt;/p&gt;

&lt;p&gt;Second, there's the idea of an interestingness test. The reduction passes are blind, and often produce programs which no longer contain the bug, or which don't compile at all. When you use C-Reduce, you provide not only a program to reduce but also a small script which tests whether a reduced program is "interesting." Exactly what "interesting" means is up to you. If you're trying to isolate a bug, then "interesting" would mean that the bug still occurs in the program. You can define it to mean whatever you want, as long as you can script it. Whatever test you provide, C-Reduce will try to provide a reduced version of the program that still passes the test.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Installation&lt;/b&gt;&lt;br&gt;C-Reduce has a lot of dependencies and can be difficult to install. Thankfully, Homebrow has it, so you can let it take care of things:&lt;/p&gt;

&lt;pre&gt;    brew install creduce
&lt;/pre&gt;

&lt;p&gt;If you'd rather do it yourself, &lt;a href="https://github.com/csmith-project/creduce/blob/master/INSTALL"&gt;take a look at C-Reduce's INSTALL file&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Simple Example&lt;/b&gt;&lt;br&gt;It's difficult to come up with small examples for C-Reduce, since its whole purpose is to start from something large and &lt;em&gt;produce&lt;/em&gt; a small example, but we'll give it our best try. Here's a simple C program that produces a somewhat cryptic warning:&lt;/p&gt;

&lt;pre&gt;    $ cat test.c
    #include &amp;lt;stdio.h&amp;gt;

    struct Stuff {
        char *name;
        int age;
    }

    main(int argc, char **argv) {
        printf("Hello, world!\n");
    }
    $ clang test.c
    test.c:3:1: warning: return type of 'main' is not 'int' [-Wmain-return-type]
    struct Stuff {
    ^
    test.c:3:1: note: change return type to 'int'
    struct Stuff {
    ^~~~~~~~~~~~
    int
    test.c:10:1: warning: control reaches end of non-void function [-Wreturn-type]
    }
    ^
    2 warnings generated.
&lt;/pre&gt;

&lt;p&gt;Somehow our &lt;code&gt;struct&lt;/code&gt; is messing with &lt;code&gt;main&lt;/code&gt;! How could that be? Maybe reducing it would help us figure it out.&lt;/p&gt;

&lt;p&gt;We need an interestingness test. We'll write a small shell script to compile this program and check for the warning in the output. C-Reduce is eager to please and can easily reduce a program far beyond what we really want. To keep it under control, we'll write a script that not only checks for the warning, but also rejects any program that produces an error, and requires &lt;code&gt;struct Stuff&lt;/code&gt; to be somewhere in the compiler output. Here's the script:&lt;/p&gt;

&lt;pre&gt;    #!/bin/bash

    clang test.c &amp;amp;&amp;gt; output.txt
    grep error output.txt &amp;amp;&amp;amp; exit 1
    grep "warning: return type of 'main' is not 'int'" output.txt &amp;amp;&amp;amp;
    grep "struct Stuff" output.txt
&lt;/pre&gt;

&lt;p&gt;First, it compiles the program and saves the compiler output into &lt;code&gt;output.txt&lt;/code&gt;. If the output contains the text "error" then it immediately signals that this program is not interesting by exiting with error code 1. Otherwise it checks for both the warning and for &lt;code&gt;struct Stuff&lt;/code&gt; in the output. &lt;code&gt;grep&lt;/code&gt; exits with code &lt;code&gt;0&lt;/code&gt; if it finds a match, so the result is that this script exits with code &lt;code&gt;0&lt;/code&gt; if both of those match, and code &lt;code&gt;1&lt;/code&gt; if either one fails. Exit code &lt;code&gt;0&lt;/code&gt; signals to C-Reduce that the reduced program is interesting, while code &lt;code&gt;1&lt;/code&gt; signals that it's not interesting and should be discarded.&lt;/p&gt;

&lt;p&gt;Now we have enough to run C-Reduce:&lt;/p&gt;

&lt;pre&gt;    $ creduce interestingness.sh test.c 
    ===&amp;lt; 4907 &amp;gt;===
    running 3 interestingness tests in parallel
    ===&amp;lt; pass_includes :: 0 &amp;gt;===
    (14.6 %, 111 bytes)

    ...lots of output...

    ===&amp;lt; pass_clex :: rename-toks &amp;gt;===
    ===&amp;lt; pass_clex :: delete-string &amp;gt;===
    ===&amp;lt; pass_indent :: final &amp;gt;===
    (78.5 %, 28 bytes)
    ===================== done ====================

    pass statistics:
      method pass_balanced :: parens-inside worked 1 times and failed 0 times
      method pass_includes :: 0 worked 1 times and failed 0 times
      method pass_blank :: 0 worked 1 times and failed 0 times
      method pass_indent :: final worked 1 times and failed 0 times
      method pass_indent :: regular worked 2 times and failed 0 times
      method pass_lines :: 3 worked 3 times and failed 30 times
      method pass_lines :: 8 worked 3 times and failed 30 times
      method pass_lines :: 10 worked 3 times and failed 30 times
      method pass_lines :: 6 worked 3 times and failed 30 times
      method pass_lines :: 2 worked 3 times and failed 30 times
      method pass_lines :: 4 worked 3 times and failed 30 times
      method pass_lines :: 0 worked 4 times and failed 20 times
      method pass_balanced :: curly-inside worked 4 times and failed 0 times
      method pass_lines :: 1 worked 6 times and failed 33 times

              ******** .../test.c ********

    struct Stuff {
    } main() {
    }
&lt;/pre&gt;

&lt;p&gt;At the end, it outputs the reduced version of the program that it came up with. It also saves the reduced version into the original file. Beware of this when working on real code! Be sure to run C-Reduce on a copy of the code (or on a file that's already checked into version control), not on an irreplaceable original.&lt;/p&gt;

&lt;p&gt;This reduced version makes the problem more apparent: we forgot the semicolon at the end of the declaration of &lt;code&gt;struct Stuff&lt;/code&gt;, and we forgot the return type on &lt;code&gt;main&lt;/code&gt;, which causes the compiler to interpret &lt;code&gt;struct Stuff&lt;/code&gt; as the return type to &lt;code&gt;main&lt;/code&gt;. This is bad, because &lt;code&gt;main&lt;/code&gt; has to return &lt;code&gt;int&lt;/code&gt;, thus the warning.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Xcode Projects&lt;/b&gt;&lt;br&gt;That's fine for something we've already reduced to a single file, but what about something more complex? Most of us have Xcode projects, so what if we want to reduce one of those?&lt;/p&gt;

&lt;p&gt;This gets awkward because of the way C-Reduce works. It copies the file to reduce into a new directory, then runs your interestingness script there. This allows it to run a lot of tests in parallel, but this breaks if you need other stuff for it to work. Since your interestingness script can run arbitrary commands, you can work around this by copying the rest of the project into the temporary directory.&lt;/p&gt;

&lt;p&gt;I created a standard Cocoa Objective-C app project in Xcode and then modified the &lt;code&gt;AppDelegate.m&lt;/code&gt; file like so:&lt;/p&gt;

&lt;pre&gt;    #import "AppDelegate.h"

    @interface AppDelegate () {
        NSWindow *win;
    }

    @property (weak) IBOutlet NSWindow *window;
    @end

    @implementation AppDelegate

    - (void)applicationDidFinishLaunching: (NSRect)visibleRect {
        NSLog(@"Starting up");
        visibleRect = NSInsetRect(visibleRect, 10, 10);
        visibleRect.size.height *= 2.0/3.0;
        win = [[NSWindow alloc] initWithContentRect: NSMakeRect(0, 0, 100, 100) styleMask:NSWindowStyleMaskTitled backing:NSBackingStoreBuffered defer:NO];
        [win makeKeyAndOrderFront: nil];
        NSLog(@"Off we go");
    }


    @end
&lt;/pre&gt;

&lt;p&gt;This strange code crashes the app on startup:&lt;/p&gt;

&lt;pre&gt;    * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
      * frame #0: 0x00007fff3ab3bf2d CoreFoundation`__CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 13
&lt;/pre&gt;

&lt;p&gt;This is not a very informative backtrace. We could try to debug (or just notice the problem), but instead let's reduce!&lt;/p&gt;

&lt;p&gt;The interestingness test needs to do some more work here. Let's start with a helper to run the app with a timeout. We're looking for a crash, and if the app &lt;em&gt;doesn't&lt;/em&gt; crash it'll just stay open, so we need to kill it after a few seconds. I found this handy perl snippet repeated all over the internet:&lt;/p&gt;

&lt;pre&gt;    function timeout() { perl -e 'alarm shift; exec @ARGV' "$@"; }
&lt;/pre&gt;

&lt;p&gt;Next, we need to copy the Xcode project over:&lt;/p&gt;

&lt;pre&gt;    cp -a ~/Development/creduce-examples/Crasher .
&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;AppDelegate.m&lt;/code&gt; file isn't automatically placed in the appropriate location, so copy that across. (Note: C-Reduce will copy the file back if it finds a reduction, so be sure to use &lt;code&gt;cp&lt;/code&gt; here rather than &lt;code&gt;mv&lt;/code&gt;. Using &lt;code&gt;mv&lt;/code&gt; will result in a cryptic fatal error.)&lt;/p&gt;

&lt;pre&gt;    cp AppDelegate.m Crasher/Crasher
&lt;/pre&gt;

&lt;p&gt;Then switch into the &lt;code&gt;Crasher&lt;/code&gt; directory and build the project, exiting on failure:&lt;/p&gt;

&lt;pre&gt;    cd Crasher
    xcodebuild || exit 1
&lt;/pre&gt;

&lt;p&gt;If it worked, run the app with a timeout. My system is configured so that &lt;code&gt;xcodebuild&lt;/code&gt; places the build result in a local &lt;code&gt;build&lt;/code&gt; directory. Yours may be configured differently, so check first. Note that if your configuration builds to a shared build directory, you'll want to disable C-Reduce's parallel builds by adding &lt;code&gt;--n 1&lt;/code&gt; to the command line when invoking it.&lt;/p&gt;

&lt;pre&gt;    timeout 5 ./build/Release/Crasher.app/Contents/MacOS/Crasher
&lt;/pre&gt;

&lt;p&gt;If it crashes, it'll exit with the special code &lt;code&gt;139&lt;/code&gt;. Translate that into an exit code of &lt;code&gt;0&lt;/code&gt;, and in all other cases exit with code &lt;code&gt;1&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;    if [ $? -eq 139 ]; then
        exit 0
    else
        exit 1
    fi
&lt;/pre&gt;

&lt;p&gt;Now we're ready to run C-Reduce:&lt;/p&gt;

&lt;pre&gt;    $ creduce interestingness.sh Crasher/AppDelegate.m
    ...
    (78.1 %, 151 bytes)
    ===================== done ====================

    pass statistics:
      method pass_ints :: a worked 1 times and failed 2 times
      method pass_balanced :: curly worked 1 times and failed 3 times
      method pass_clex :: rm-toks-7 worked 1 times and failed 74 times
      method pass_clex :: rename-toks worked 1 times and failed 24 times
      method pass_clex :: delete-string worked 1 times and failed 3 times
      method pass_blank :: 0 worked 1 times and failed 1 times
      method pass_comments :: 0 worked 1 times and failed 0 times
      method pass_indent :: final worked 1 times and failed 0 times
      method pass_indent :: regular worked 2 times and failed 0 times
      method pass_lines :: 8 worked 3 times and failed 43 times
      method pass_lines :: 2 worked 3 times and failed 43 times
      method pass_lines :: 6 worked 3 times and failed 43 times
      method pass_lines :: 10 worked 3 times and failed 43 times
      method pass_lines :: 4 worked 3 times and failed 43 times
      method pass_lines :: 3 worked 3 times and failed 43 times
      method pass_lines :: 0 worked 4 times and failed 23 times
      method pass_lines :: 1 worked 6 times and failed 45 times

              ******** /Users/mikeash/Development/creduce-examples/Crasher/Crasher/AppDelegate.m ********

    #import "AppDelegate.h"
    @implementation AppDelegate
    - (void)applicationDidFinishLaunching:(NSRect)a {
      a = NSInsetRect(a, 0, 10);
      NSLog(@"");
    }
    @end
&lt;/pre&gt;

&lt;p&gt;That's a lot shorter! The &lt;code&gt;NSLog&lt;/code&gt; line looks harmless, although it must be part of the crash if C-Reduce didn't remove it. The &lt;code&gt;a = NSInsetRect(a, 0, 10);&lt;/code&gt; line is the only other thing that actually does something. Where does &lt;code&gt;a&lt;/code&gt; come from and why would writing to it do something bad? It's just the parameter to &lt;code&gt;applicationDidFinishLaunching:&lt;/code&gt; which... is not an &lt;code&gt;NSRect&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;    - (void)applicationDidFinishLaunching:(NSNotification *)notification;
&lt;/pre&gt;

&lt;p&gt;Oops! The parameter type mismatch resulted in stack corruption that caused the uninformative crash.&lt;/p&gt;

&lt;p&gt;C-Reduce took a long time to run on this example, because building an Xcode project takes longer than compiling a single file, and because a lot of the test cases hit the five-second timeout when running. C-Reduce copies the reduced file back to the original directory on every success, so you can leave it open in a text editor to watch it at work. If you think it's gone far enough, you can ^C it and you'll be left with the partially-reduced file. If you decide you want to run it some more, re-run it and it will continue from there.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Swift&lt;/b&gt;&lt;br&gt;What if you're using Swift and want to reduce a problem? Given the name, I originally thought that C-Reduce only worked on C (and maybe C++, since so many tools do both).&lt;/p&gt;

&lt;p&gt;Thankfully, I was wrong. C-Reduce does have some C-specific reduction passes, but it has a lot of others that are relatively language agnostic. It may be less effective, but as long as you can write an interestingness test for your problem, C-Reduce can probably work on it no matter what language you're using.&lt;/p&gt;

&lt;p&gt;Let's try it. I found a &lt;a href="https://bugs.swift.org/browse/SR-7354"&gt;nice compiler bug on bugs.swift.org&lt;/a&gt;. It's already been fixed, but Xcode 9.3's Swift crashes on it and I happen to have that version handy. Here's a slightly modified version of the example from that bug:&lt;/p&gt;

&lt;pre&gt;    import Foundation

    func crash() {
        let blah = ProblematicEnum.problematicCase.problematicMethod()
        NSLog("\(blah)")
    }

    enum ProblematicEnum {
        case first, second, problematicCase

        func problematicMethod() -&amp;gt; SomeClass {
            let someVariable: SomeClass

            switch self {
            case .first:
                someVariable = SomeClass()
            case .second:
                someVariable = SomeClass()
            case .problematicCase:
                someVariable = SomeClass(someParameter: NSObject())
                _ = NSObject().description
                return someVariable // EXC_BAD_ACCESS (simulator: EXC_I386_GPFLT, device: code=1)
            }

            let _ = [someVariable]
            return SomeClass(someParameter: NSObject())
        }

    }

    class SomeClass: NSObject {
        override init() {}
        init(someParameter: NSObject) {}
    }

    crash()
&lt;/pre&gt;

&lt;p&gt;Let's try running it with optimizations enabled:&lt;/p&gt;

&lt;pre&gt;    $ swift -O test.swift 
    &amp;lt;unknown&amp;gt;:0: error: fatal error encountered during compilation; please file a bug report with your project and the crash log
    &amp;lt;unknown&amp;gt;:0: note: Program used external function '__T04test15ProblematicEnumON' which could not be resolved!
    ...
&lt;/pre&gt;

&lt;p&gt;The interestingness test is fairly simple for this one. Run that command and check the exit code:&lt;/p&gt;

&lt;pre&gt;    swift -O test.swift
    if [ $? -eq 134 ]; then
        exit 0
    else
        exit 1
    fi
&lt;/pre&gt;

&lt;p&gt;Running C-Reduce on this, it produces the following example:&lt;/p&gt;

&lt;pre&gt;    enum a {
        case b, c, d
        func e() -&amp;gt; f {
            switch self {
            case .b:
                0
            case .c:
                0
            case .d:
                0
            }
            return f()
        }
    }
    class f{}
&lt;/pre&gt;

&lt;p&gt;Diving into the actual compiler bug is beyond the scope of this article, but this reduction would be really handy if we actually set out to fix it. We have a considerably simpler test case to work with. We can also infer that there's some interaction between the swift statement and the instantiation of the class, since C-Reduce probably would have removed one of them if it were unnecessary. This would give us some good hints about what might be happening in the compiler to cause this crash.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;br&gt;Blind reduction of a test case is not a very sophisticated debugging technique, but the ability to automate it can make it extremely useful. C-Reduce can be a fantastic addition to your debugging toolbox. It's not suitable for everything, but what is? For problems where it's useful, it can help enormously. It can be a bit tricky to get it to work with multi-file test cases, but some cleverness with the interestingness script solves the problem. Despite the name, it works out of the box on Swift and many other languages, so don't give up on it just because you're not working in C.&lt;/p&gt;

&lt;p&gt;That's it for today. Check back next time for more fun, games, and code. Friday Q&amp;amp;A is driven by reader ideas, so if you have something you'd like to see covered here next time or some other time, please &lt;a href="mailto:mike@mikeash.com"&gt;send it in&lt;/a&gt;!&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;
</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/friday-qa-2018-06-29-debugging-with-c-reduce.html</guid><pubDate>Fri, 29 Jun 2018 13:35:00 GMT</pubDate></item><item><title>Friday Q&amp;A 2018-04-27: Generating Text With Markov Chains in Swift
</title><link>http://www.mikeash.com/pyblog/friday-qa-2018-04-27-generating-text-with-markov-chains-in-swift.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;Friday Q&amp;amp;A 2018-04-27: Generating Text With Markov Chains in Swift
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2018 04 28  01 27"
                  tags="fridayqna"
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;Friday Q&amp;amp;A 2018-04-27: Generating Text With Markov Chains in Swift
&lt;/div&gt;
              &lt;p&gt;Markov chains make for a simple way to generate realistic looking but nonsensical text. Today, I'm going to use that technique to build a text generator based on this blog's contents, an idea suggested/inspired by reader Jordan Pittman.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Markov Chains&lt;/b&gt;&lt;br&gt;At a theoretical level, a Markov chain is a state machine where each transition has a probability associated with it. You can walk through the chain by choosing a start state and then transitioning to subsequent states randomly, weighted by the transition probabilities, until you reach a terminal state.&lt;/p&gt;

&lt;p&gt;Markov chains have &lt;a href="https://en.wikipedia.org/wiki/Markov_chain#Steady-state_analysis_and_limiting_distributions"&gt;numerious applications&lt;/a&gt;, but the most amusing is for text generation. There, each state is some unit of text, typically a word. The states and transitions are generated from some input corpus, and then text is generated by walking through the chain and outputting the word for each state. The result rarely makes sense, as the chain doesn't contain enough information to retain any of the underlying meaning of the input corpus, or even much of the grammatical structure, but that lack of sense can be hilarious.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Representation&lt;/b&gt;&lt;br&gt;The nodes in the chain will be represented as instances of a &lt;code&gt;Word&lt;/code&gt; class. This class will store a &lt;code&gt;String&lt;/code&gt; for the word it represents, and a set of links to other words.&lt;/p&gt;

&lt;p&gt;How do we represent that set of links? The most obvious approach would be some sort of counted set, which would store other &lt;code&gt;Word&lt;/code&gt; instances along with a count of the number of times that transition was seen in the input corpus. Randomly choosing a link from such a set can be tricky, though. A simple way is to generate a random number betewen 0 and the total count of the entire set, then iterate through the set until you encounter that many links, and choose the link you landed on. This is easy but slow. Another approach would be to precompute an array that stores the cumulative total for each link in the array, then do a binary search on a random number between 0 and the total. This is harder but faster. If you want to get really fancy, you can do even more preprocessing and end up with &lt;a href="http://www.keithschwarz.com/darts-dice-coins/"&gt;a compact structure you can query in constant time&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Ultimately, I decided to be lazy and use a structure that's extremely wasteful in space, but efficient in time and easy to implement. Each &lt;code&gt;Word&lt;/code&gt; contains an array of subsequent &lt;code&gt;Word&lt;/code&gt;s. If a link occurs multiple times, the duplicates remain in the array. Choosing a random element with the appropriate weight consists of choosing a random index in the array.&lt;/p&gt;

&lt;p&gt;Here's what the &lt;code&gt;Word&lt;/code&gt; class looks like:&lt;/p&gt;

&lt;pre&gt;    class Word {
        let str: String?
        var links: [Word] = []

        init(str: String?) {
            self.str = str
        }

        func randomNext() -&amp;gt; Word {
            let index = arc4random_uniform(UInt32(links.count))
            return links[Int(index)]
        }
    }
&lt;/pre&gt;

&lt;p&gt;Note that the &lt;code&gt;links&lt;/code&gt; array will likely result in lots of circular references. To avoid leaking memory, we'll need to have something manually clean those up.&lt;/p&gt;

&lt;p&gt;That something will be the &lt;code&gt;Chain&lt;/code&gt; class, which will manage all of the &lt;code&gt;Word&lt;/code&gt;s in a chain:&lt;/p&gt;

&lt;pre&gt;    class Chain {
        var words: [String?: Word] = [:]
&lt;/pre&gt;

&lt;p&gt;In &lt;code&gt;deinit&lt;/code&gt;, it clears all of the &lt;code&gt;links&lt;/code&gt; arrays to eliminate any cycles:&lt;/p&gt;

&lt;pre&gt;        deinit {
            for word in words.values {
                word.links = []
            }
        }
&lt;/pre&gt;

&lt;p&gt;Without this step, a lot of the &lt;code&gt;Word&lt;/code&gt; instances would leak.&lt;/p&gt;

&lt;p&gt;Let's look at how words are added to the chain. The &lt;code&gt;add&lt;/code&gt; method will take an array of &lt;code&gt;String&lt;/code&gt;s, each one of which holds a word (or any other unit that the caller wants to work with):&lt;/p&gt;

&lt;pre&gt;        func add(_ words: [String]) {
&lt;/pre&gt;

&lt;p&gt;If there aren't actually any words, bail out early:&lt;/p&gt;

&lt;pre&gt;            if words.isEmpty { return }
&lt;/pre&gt;

&lt;p&gt;We want to iterate over pairs of words, where the second element in the pair is the word that follows the first element. For example, in the sentence "Help, I'm being oppressed," we want to iterate over &lt;code&gt;("Help", "I'm")&lt;/code&gt;, &lt;code&gt;("I'm", "being")&lt;/code&gt;, &lt;code&gt;("being", "oppressed")&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Actually, we want a bit more as well, since we want to encode the beginning and end of the sentence. We represent those as &lt;code&gt;nil&lt;/code&gt;, so the actual sequence we want to iterate over is &lt;code&gt;(nil, "Help")&lt;/code&gt;, &lt;code&gt;("Help", "I'm")&lt;/code&gt;, &lt;code&gt;("I'm", "being")&lt;/code&gt;, &lt;code&gt;("being", "oppressed")&lt;/code&gt;, &lt;code&gt;("oppressed", nil)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To allow for &lt;code&gt;nil&lt;/code&gt;, we need an array whose contents are &lt;code&gt;String?&lt;/code&gt; rather than plain &lt;code&gt;String&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;            let words = words as [String?]
&lt;/pre&gt;

&lt;p&gt;Next, we'll construct two arrays, one by prepending &lt;code&gt;nil&lt;/code&gt; and one by appending &lt;code&gt;nil&lt;/code&gt;. Zipping them together produces the sequence we want:&lt;/p&gt;

&lt;pre&gt;            let wordPairs = zip([nil] + words, words + [nil])
            for (first, second) in wordPairs {
&lt;/pre&gt;

&lt;p&gt;For each word in the pair, we'll fetch the corresponding &lt;code&gt;Word&lt;/code&gt; object using a handy helper function:&lt;/p&gt;

&lt;pre&gt;                let firstWord = word(first)
                let secondWord = word(second)
&lt;/pre&gt;

&lt;p&gt;Then all we have to do is add &lt;code&gt;secondWord&lt;/code&gt; into the links of &lt;code&gt;firstWord&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;                firstWord.links.append(secondWord)
            }
        }
&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;word&lt;/code&gt; helper fetches the instance from the &lt;code&gt;words&lt;/code&gt; dictionary if it exists, otherwise it creates a new instance and puts it into the dictionary. This frees other code from worrying about whether there's already a &lt;code&gt;Word&lt;/code&gt; for any given string:&lt;/p&gt;

&lt;pre&gt;        func word(_ str: String?) -&amp;gt; Word {
            if let word = words[str] {
                return word
            } else {
                let word = Word(str: str)
                words[str] = word
                return word
            }
        }
&lt;/pre&gt;

&lt;p&gt;Finally, we want to generate new sequences of words:&lt;/p&gt;

&lt;pre&gt;        func generate() -&amp;gt; [String] {
&lt;/pre&gt;

&lt;p&gt;We'll generate the words one by one, accumulating them here:&lt;/p&gt;

&lt;pre&gt;            var result: [String] = []
&lt;/pre&gt;

&lt;p&gt;Loop "forever." The exit condition doesn't map cleanly to a loop condition, so we'll handle that inside the loop:&lt;/p&gt;

&lt;pre&gt;            while true {
&lt;/pre&gt;

&lt;p&gt;Fetch the &lt;code&gt;Word&lt;/code&gt; instance for the last string in &lt;code&gt;result&lt;/code&gt;. This neatly handles the initial case where &lt;code&gt;result&lt;/code&gt; is empty, since &lt;code&gt;last&lt;/code&gt; produces &lt;code&gt;nil&lt;/code&gt; which indicates the first word:&lt;/p&gt;

&lt;pre&gt;                let currentWord = word(result.last)
&lt;/pre&gt;

&lt;p&gt;Randomly get a linked word:&lt;/p&gt;

&lt;pre&gt;                let nextWord = currentWord.randomNext()
&lt;/pre&gt;

&lt;p&gt;If the linked word isn't the end, append it to &lt;code&gt;result&lt;/code&gt;. If it is the end, terminate the loop:&lt;/p&gt;

&lt;pre&gt;                if let str = nextWord.str {
                    result.append(str)
                } else {
                    break
                }
            }
&lt;/pre&gt;

&lt;p&gt;Return the accumulated result:&lt;/p&gt;

&lt;pre&gt;            return result
        }
    }
&lt;/pre&gt;

&lt;p&gt;One last thing: we're using &lt;code&gt;String?&lt;/code&gt; as the key type for &lt;code&gt;words&lt;/code&gt;, but &lt;code&gt;Optional&lt;/code&gt; doesn't conform to &lt;code&gt;Hashable&lt;/code&gt;. Here's a quick extension that adds it when its wrapped type conforms:&lt;/p&gt;

&lt;pre&gt;    extension Optional: Hashable where Wrapped: Hashable {
        public var hashValue: Int {
            switch self {
            case let wrapped?: return wrapped.hashValue
            case .none: return 42
            }
        }
    }
&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;Generating Input&lt;/b&gt;&lt;br&gt;That's the Markov chain itself, but it's pretty boring without some real text to put into it.&lt;/p&gt;

&lt;p&gt;I decided to pull text from an RSS feed. What better feed to choose than my own blog's full text feed?&lt;/p&gt;

&lt;pre&gt;    let feedURL = URL(string: "https://www.mikeash.com/pyblog/rss.py?mode=fulltext")!
&lt;/pre&gt;

&lt;p&gt;RSS is an XML format, so use &lt;code&gt;XMLDocument&lt;/code&gt; to parse it:&lt;/p&gt;

&lt;pre&gt;    let xmlDocument = try! XMLDocument(contentsOf: feedURL, options: [])
&lt;/pre&gt;

&lt;p&gt;The article bodies are in XML &lt;code&gt;description&lt;/code&gt; nodes which are nested inside &lt;code&gt;item&lt;/code&gt; nodes. An XPath query retrieves those:&lt;/p&gt;

&lt;pre&gt;    let descriptionNodes = try! xmlDocument.nodes(forXPath: "//item/description")
&lt;/pre&gt;

&lt;p&gt;We want the strings in the XML nodes, so extract those and throw away any that are &lt;code&gt;nil&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;    let descriptionHTMLs = descriptionNodes.compactMap({ $0.stringValue })
&lt;/pre&gt;

&lt;p&gt;We don't care about the markup at all. &lt;code&gt;NSAttributedString&lt;/code&gt; can parse &lt;code&gt;HTML&lt;/code&gt; and produce a string with attributes, which we can then throw away:&lt;/p&gt;

&lt;pre&gt;    let descriptionStrings = descriptionHTMLs.map({
        NSAttributedString(html: $0.data(using: .utf8)!, options: [:], documentAttributes: nil)!.string
    })
&lt;/pre&gt;

&lt;p&gt;Let's take a quick detour to a function that breaks up a string into parts. We ultimately want to consume arrays of &lt;code&gt;String&lt;/code&gt;, where each array represents a sentence. A string will contain zero or more sentences, so this &lt;code&gt;wordSequences&lt;/code&gt; function returns an array of arrays of &lt;code&gt;String&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;    func wordSequences(in str: String) -&amp;gt; [[String]] {
&lt;/pre&gt;

&lt;p&gt;Results get accumulated into a local variable:&lt;/p&gt;

&lt;pre&gt;        var result: [[String]] = []
&lt;/pre&gt;

&lt;p&gt;Breaking a &lt;code&gt;String&lt;/code&gt; into sentences isn't always easy. You could search for the appropriate punctuation, but consider a sentence like "Mr. Jock, TV quiz Ph.D., bags few lynx." That's one sentence, despite having four periods.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;NSString&lt;/code&gt; provides some methods for intelligently examining parts of a string, and &lt;code&gt;String&lt;/code&gt; gets those when you &lt;code&gt;import Foundation&lt;/code&gt;. We'll ask &lt;code&gt;str&lt;/code&gt; to enumerate its sentences, and let &lt;code&gt;Foundation&lt;/code&gt; figure out how:&lt;/p&gt;

&lt;pre&gt;        str.enumerateSubstrings(in: str.startIndex..., options: .bySentences, { substring, substringRange, enclosingRange, stop in
&lt;/pre&gt;

&lt;p&gt;We face a similar problem splitting each sentence into words. &lt;code&gt;NSString&lt;/code&gt; does provide a method for enumerating over words, but this presents some problems, like losing punctuation. I ultimately decided to take a dumb approach for word splitting and just split on spaces. This means that you end up with words that contain punctuation as part of their string. This constrains the Markov chain more than if the punctuation was removed, but on the other hand it means that the output naturally contains something like reasonable punctuation. It seemed like a good tradeoff.&lt;/p&gt;

&lt;p&gt;Some newlines make their way into the data set, so we'll cut those off at this point:&lt;/p&gt;

&lt;pre&gt;            let words = substring!.split(separator: " ").map({
                $0.trimmingCharacters(in: CharacterSet.newlines)
            })
&lt;/pre&gt;

&lt;p&gt;The sliced-up sentence gets added to &lt;code&gt;result&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;            result.append(words)
        })
&lt;/pre&gt;

&lt;p&gt;After enumeration is complete, &lt;code&gt;result&lt;/code&gt; is filled out with the sentences from the input, and we return it to the caller:&lt;/p&gt;

&lt;pre&gt;        return result
    }
&lt;/pre&gt;

&lt;p&gt;Back to the main code. Now that we have a way to convert a string into a list of sentences, we can build our Markov chain. We'll start with an empty &lt;code&gt;Chain&lt;/code&gt; object:&lt;/p&gt;

&lt;pre&gt;    let chain = Chain()
&lt;/pre&gt;

&lt;p&gt;Then we go through all the strings, extract the sentences, and add them to the chain:&lt;/p&gt;

&lt;pre&gt;    for str in descriptionStrings {
        for sentence in wordSequences(in: str) {
            chain.add(sentence)
        }
    }
&lt;/pre&gt;

&lt;p&gt;All that's left is to generate some new sentences! We'll call &lt;code&gt;generate()&lt;/code&gt; and then join the result with spaces. The output is hit-or-miss (which is no surprise given the random nature of the technique) so we'll generate a lot:&lt;/p&gt;

&lt;pre&gt;    for _ in 0 ..&amp;lt; 200 {
        print("\"" + chain.generate().joined(separator: " ") + "\"")
    }
&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;Example Output&lt;/b&gt;&lt;br&gt;For your entertainment, here are some examples of the output of this program:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"We're ready to be small, weak references in New York City."&lt;/li&gt;
&lt;li&gt;"It thus makes no values?"&lt;/li&gt;
&lt;li&gt;"Simple JSON tasks, it's wasteful if you can be."&lt;/li&gt;
&lt;li&gt;"Another problem, but it would make things more programming-related mystery goo."&lt;/li&gt;
&lt;li&gt;"The escalating delays after excessive focus on Friday, September 29th."&lt;/li&gt;
&lt;li&gt;"You may not set."&lt;/li&gt;
&lt;li&gt;"Declare conformance to use = Self.init() to detect the requested values."&lt;/li&gt;
&lt;li&gt;"The tagged pointer is inlined at this nature; even hundreds of software and writing out at 64 bits wide."&lt;/li&gt;
&lt;li&gt;"We're ready to express that it works by reader ideas, so the decoding methods for great while, it's inaccessible to 0xa4, which takes care of increasing addresses as the timing."&lt;/li&gt;
&lt;li&gt;"APIs which is mostly a one-sided use it yourself?"&lt;/li&gt;
&lt;li&gt;"There's no surprise."&lt;/li&gt;
&lt;li&gt;"I wasn't sure why I've been called 'zero-cost' in control both take serious effort to miss instead of ARC and games."&lt;/li&gt;
&lt;li&gt;"For now, we can look at the filesystem."&lt;/li&gt;
&lt;li&gt;"The intent is intended as reader-writer locks."&lt;/li&gt;
&lt;li&gt;"For example, we can use of the code?"&lt;/li&gt;
&lt;li&gt;"Swift's generics can all fields of Swift programming, with them is no parameters are static subscript, these instantiate self = cluster.reduce(0, +) / Double(cluster.count)"&lt;/li&gt;
&lt;li&gt;"However, the common case, you to the left-hand side tables."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There's a lot of nonsense as well, so you have to dig through to find good ones, but Markov chains can produce some pretty funny output.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;br&gt;Markov chains have many practical uses, but they can also be hilariously useless when used to generate text. Aside from being entertaining, this code also demonstrates how to deal with circular references in a situation where there's no clear directionality, how to use &lt;code&gt;NSString&lt;/code&gt;'s intelligent enumeration methods to extract features from text, and a brief demonstration of the power of conditional conformances.&lt;/p&gt;

&lt;p&gt;That wraps it up for today. Stop by next time for more fun, games, and maybe even a little education. Until then, Friday Q&amp;amp;A is driven by reader ideas, so if you have a topic you'd like to see covered here, please &lt;a href="mailto:mike@mikeash.com"&gt;send it in&lt;/a&gt;!&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/friday-qa-2018-04-27-generating-text-with-markov-chains-in-swift.html</guid><pubDate>Sat, 28 Apr 2018 01:27:00 GMT</pubDate></item><item><title>Friday Q&amp;A 2017-12-08: Type Erasure in Swift
</title><link>http://www.mikeash.com/pyblog/friday-qa-2017-12-08-type-erasure-in-swift.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;Friday Q&amp;amp;A 2017-12-08: Type Erasure in Swift
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2017 12 15  14 09"
                  tags="fridayqna swift"
                  translations="http://fertlond.com/tipus-torles-a-swift/ Hungarian Szabolcs Csintalan"
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;Friday Q&amp;amp;A 2017-12-08: Type Erasure in Swift
&lt;/div&gt;
              &lt;p&gt;You might have heard the term type erasure. You might have even used type-erased types in the standard library, such as &lt;code&gt;AnySequence&lt;/code&gt;. But what exactly &lt;em&gt;is&lt;/em&gt; type erasure and how do you do it yourself? In this article, I'll explore type erasure, why you'd want it, and how to make it happen, a topic suggested by Lorenzo Boaro.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Motivation&lt;/b&gt;&lt;br&gt;There are times when you want to hide an underlying type from outside users. Sometimes it's just a matter of hiding implementation details. In other cases, it can prevent a static type from spreading through the codebase, or allow distinct types to interoperate. Type erasure is the process of removing a specific type annotation in favor of a more general one.&lt;/p&gt;

&lt;p&gt;Protocols or abstract superclasses could be considered a really simple form of type erasure. Take &lt;code&gt;NSString&lt;/code&gt; as an example. You never get a plain &lt;code&gt;NSString&lt;/code&gt; instance; it's always an instance of some concrete subclass, usually private. That is mostly hidden from view, though, and the APIs all work with &lt;code&gt;NSString&lt;/code&gt;. All of the various subclasses can be used without having to know what they are, and without having to sprinkle your code with their types.&lt;/p&gt;

&lt;p&gt;More advanced techniques become useful when dealing with Swift's generics and protocols with associated types. Swift doesn't allow using such protocols as concrete types. For example, if you want to write some code that accepts any &lt;code&gt;Sequence&lt;/code&gt; of &lt;code&gt;Int&lt;/code&gt; values, you can't write this:&lt;/p&gt;

&lt;pre&gt;    func f(seq: Sequence&amp;lt;Int&amp;gt;) { ...
&lt;/pre&gt;

&lt;p&gt;That's not legal Swift. You can specialize generic types that way, but not protocols. You can work around this using generics:&lt;/p&gt;

&lt;pre&gt;    func f&amp;lt;S: Sequence&amp;gt;(seq: S) where S.Element == Int { ...
&lt;/pre&gt;

&lt;p&gt;Sometimes this works great, but there are cases where it can be troublesome. Often you can't just add generics in one spot: one generic function requires others to be generic which require yet more.... Even worse, you can't use this for return values or properties at all. This won't work the way you want it to at all:&lt;/p&gt;

&lt;pre&gt;    func g&amp;lt;S: Sequence&amp;gt;() -&amp;gt; S where S.Element == Int { ...
&lt;/pre&gt;

&lt;p&gt;We're looking for something where &lt;code&gt;g&lt;/code&gt; can return any conforming type, but instead this allows the &lt;em&gt;caller&lt;/em&gt; to choose which type it wants, and &lt;code&gt;g&lt;/code&gt; is then required to provide an appropriate value.&lt;/p&gt;

&lt;p&gt;Swift provides the &lt;code&gt;AnySequence&lt;/code&gt; type to solve this problem. &lt;code&gt;AnySequence&lt;/code&gt; wraps an arbitrary &lt;code&gt;Sequence&lt;/code&gt; and erases its type, providing access to it through the &lt;code&gt;AnySequence&lt;/code&gt; type instead. Using this, we can rewrite &lt;code&gt;f&lt;/code&gt; and &lt;code&gt;g&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;    func f(seq: AnySequence&amp;lt;Int&amp;gt;) { ...

    func g() -&amp;gt; AnySequence&amp;lt;Int&amp;gt; { ...
&lt;/pre&gt;

&lt;p&gt;The generics disappear and all the specific types are still hidden. There's a small code complexity and runtime cost from having to wrap the values in &lt;code&gt;AnySequence&lt;/code&gt;, but the code is nice and clean.&lt;/p&gt;

&lt;p&gt;The Swift standard library has a bunch of these &lt;code&gt;Any&lt;/code&gt; types, such as &lt;code&gt;AnyCollection&lt;/code&gt;, &lt;code&gt;AnyHashable&lt;/code&gt;, and &lt;code&gt;AnyIndex&lt;/code&gt;. It can be useful to create your own to go along with your own generics and protocols, or just use the techniques to simplify your code when dealing with them. Let's explore the various ways to accomplish type erasure.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Type Erasure With Classes&lt;/b&gt;&lt;br&gt;We need to wrap up some common functionality from multiple types without exposing those types. This sounds a lot like a superclass-subclass relationship, and in fact we can use subclasses to implement type erasure. The superclass can expose an API that's blind to the underlying implementation type, and a subclass can implement that API with knowledge of the underlying type.&lt;/p&gt;

&lt;p&gt;Let's see how our own version of &lt;code&gt;AnySequence&lt;/code&gt; would look using this technique. I'll call it &lt;code&gt;MAnySequence&lt;/code&gt; to incorporate my name:&lt;/p&gt;

&lt;pre&gt;    class MAnySequence&amp;lt;Element&amp;gt;: Sequence {
&lt;/pre&gt;

&lt;p&gt;This class is also going to need an iterator type that it can return from the &lt;code&gt;makeIterator&lt;/code&gt; method. We have to perform type erasure twice so that we can hide the underlying &lt;code&gt;Sequence&lt;/code&gt; type as well as its &lt;code&gt;Iterator&lt;/code&gt; type. This inner &lt;code&gt;Iterator&lt;/code&gt; class conforms to &lt;code&gt;IteratorProtocol&lt;/code&gt; and implements its &lt;code&gt;next&lt;/code&gt; method to call &lt;code&gt;fatalError&lt;/code&gt;. Swift doesn't have built-in support for abstract classes, so this will have to suffice:&lt;/p&gt;

&lt;pre&gt;        class Iterator: IteratorProtocol {
            func next() -&amp;gt; Element? {
                fatalError("Must override next()")
            }
        }
&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;MAnySequence&lt;/code&gt; gets a similar implementation of &lt;code&gt;makeIterator&lt;/code&gt;. It calls &lt;code&gt;fatalError&lt;/code&gt; to encourage its subclass to override it:&lt;/p&gt;

&lt;pre&gt;        func makeIterator() -&amp;gt; Iterator {
            fatalError("Must override makeIterator()")
        }
    }
&lt;/pre&gt;

&lt;p&gt;That is the type-erased public API. The private implementation subclasses it. The public class is parameterized by the element type, but the private implementation class is parameterized by the sequence type it wraps:&lt;/p&gt;

&lt;pre&gt;    private class MAnySequenceImpl&amp;lt;Seq: Sequence&amp;gt;: MAnySequence&amp;lt;Seq.Element&amp;gt; {
&lt;/pre&gt;

&lt;p&gt;This class needs an internal subclass of the internal &lt;code&gt;Iterator&lt;/code&gt; class from above:&lt;/p&gt;

&lt;pre&gt;        class IteratorImpl: Iterator {
&lt;/pre&gt;

&lt;p&gt;It wraps an instance of the sequence's &lt;code&gt;Iterator&lt;/code&gt; type:&lt;/p&gt;

&lt;pre&gt;            var wrapped: Seq.Iterator

            init(_ wrapped: Seq.Iterator) {
                self.wrapped = wrapped
            }
&lt;/pre&gt;

&lt;p&gt;It implements &lt;code&gt;next&lt;/code&gt; to call through to that wrapped iterator:&lt;/p&gt;

&lt;pre&gt;            override func next() -&amp;gt; Seq.Element? {
                return wrapped.next()
            }
        }
&lt;/pre&gt;

&lt;p&gt;Similarly, &lt;code&gt;MAnySequenceImpl&lt;/code&gt; wraps an instance of the sequence:&lt;/p&gt;

&lt;pre&gt;        var seq: Seq

        init(_ seq: Seq) {
            self.seq = seq
        }
&lt;/pre&gt;

&lt;p&gt;It implements &lt;code&gt;makeIterator&lt;/code&gt; to get an iterator from wrapped sequence, and then wrap that iterator in &lt;code&gt;IteratorImpl&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;        override func makeIterator() -&amp;gt; IteratorImpl {
            return IteratorImpl(seq.makeIterator())
        }

    }
&lt;/pre&gt;

&lt;p&gt;We need a way to actually create these things. A &lt;code&gt;static&lt;/code&gt; method on &lt;code&gt;MAnySequence&lt;/code&gt; creates an instance of &lt;code&gt;MAnySequenceImpl&lt;/code&gt; and returns it to the caller as an &lt;code&gt;MAnySequence&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;    extension MAnySequence {
        static func make&amp;lt;Seq: Sequence&amp;gt;(_ seq: Seq) -&amp;gt; MAnySequence&amp;lt;Element&amp;gt; where Seq.Element == Element {
            return MAnySequenceImpl&amp;lt;Seq&amp;gt;(seq)
        }
    }
&lt;/pre&gt;

&lt;p&gt;In production code, we would probably want to clean this up a bit by using an extra level of indirection so that &lt;code&gt;MAnySequence&lt;/code&gt; could provide an initializer instead.&lt;/p&gt;

&lt;p&gt;Let's try it out:&lt;/p&gt;

&lt;pre&gt;    func printInts(_ seq: MAnySequence&amp;lt;Int&amp;gt;) {
        for elt in seq {
            print(elt)
        }
    }

    let array = [1, 2, 3, 4, 5]
    printInts(MAnySequence.make(array))
    printInts(MAnySequence.make(array[1 ..&amp;lt; 4]))
&lt;/pre&gt;

&lt;p&gt;It works!&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Type Erasure With Functions&lt;/b&gt;&lt;br&gt;We want to expose functionality from multiple types without exposing those types. A natural approach for this is to store functions whose signatures only involve the types we want to expose. The function bodies can be created in a context where the underlying implementation types are known.&lt;/p&gt;

&lt;p&gt;Let's look at how &lt;code&gt;MAnySequence&lt;/code&gt; would look with this approach. It starts off similar to the previous implementation, although this one can be a &lt;code&gt;struct&lt;/code&gt; rather than a &lt;code&gt;class&lt;/code&gt; because it's just a dumb container and there's no inheritance:&lt;/p&gt;

&lt;pre&gt;    struct MAnySequence&amp;lt;Element&amp;gt;: Sequence {
&lt;/pre&gt;

&lt;p&gt;Like before, it needs an &lt;code&gt;Iterator&lt;/code&gt; that it can return. This one is also a &lt;code&gt;struct&lt;/code&gt; and it contains a stored property which is a function that takes no parameters and returns an &lt;code&gt;Element?&lt;/code&gt;, which is the signature used for the &lt;code&gt;next&lt;/code&gt; method in &lt;code&gt;IteratorProtocol&lt;/code&gt;. It then implement &lt;code&gt;IteratorProtocol&lt;/code&gt; to call that function:&lt;/p&gt;

&lt;pre&gt;        struct Iterator: IteratorProtocol {
            let _next: () -&amp;gt; Element?

            func next() -&amp;gt; Element? {
                return _next()
            }
        }
&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;MAnySequence&lt;/code&gt; itself is similar: it contains a stored property which is a function that takes no arguments and returns an &lt;code&gt;Iterator&lt;/code&gt;. &lt;code&gt;Sequence&lt;/code&gt; is then implemented by calling through to that function:&lt;/p&gt;

&lt;pre&gt;        let _makeIterator: () -&amp;gt; Iterator

        func makeIterator() -&amp;gt; Iterator {
            return _makeIterator()
        }
&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;MAnySequence&lt;/code&gt;'s &lt;code&gt;init&lt;/code&gt; is where the magic happens. It takes an arbitrary &lt;code&gt;Sequence&lt;/code&gt; as its parameter:&lt;/p&gt;

&lt;pre&gt;        init&amp;lt;Seq: Sequence&amp;gt;(_ seq: Seq) where Seq.Element == Element {
&lt;/pre&gt;

&lt;p&gt;It then needs to wrap the functionality of this sequence in a function:&lt;/p&gt;

&lt;pre&gt;            _makeIterator = {
&lt;/pre&gt;

&lt;p&gt;How do we make an iterator here? We'll start by asking &lt;code&gt;seq&lt;/code&gt; to make one:&lt;/p&gt;

&lt;pre&gt;                var iterator = seq.makeIterator()
&lt;/pre&gt;

&lt;p&gt;Then we'll wrap that iterator in &lt;code&gt;Iterator&lt;/code&gt;. Its &lt;code&gt;_next&lt;/code&gt; function can just call &lt;code&gt;iterator&lt;/code&gt;'s &lt;code&gt;next&lt;/code&gt; method:&lt;/p&gt;

&lt;pre&gt;                return Iterator(_next: { iterator.next() })
            }
        }

    }
&lt;/pre&gt;

&lt;p&gt;Here's some code that uses it:&lt;/p&gt;

&lt;pre&gt;    func printInts(_ seq: MAnySequence&amp;lt;Int&amp;gt;) {
        for elt in seq {
            print(elt)
        }
    }

    let array = [1, 2, 3, 4, 5]
    printInts(MAnySequence(array))
    printInts(MAnySequence(array[1 ..&amp;lt; 4]))
&lt;/pre&gt;

&lt;p&gt;This one works too!&lt;/p&gt;

&lt;p&gt;This function-based approach to type erasure can be particularly nice when you need to wrap a small amount of functionality as part of a larger type, and don't need separate classes implementing the entire functionality of whatever types you're erasing.&lt;/p&gt;

&lt;p&gt;For example, let's say you want to write some code that works with various collection types, but all it really needs to be able to do with those collections is get a count and do a zero-based integer subscript. For example, this might be a table view data source. It might then look like this:&lt;/p&gt;

&lt;pre&gt;    class GenericDataSource&amp;lt;Element&amp;gt; {
        let count: () -&amp;gt; Int
        let getElement: (Int) -&amp;gt; Element

        init&amp;lt;C: Collection&amp;gt;(_ c: C) where C.Element == Element, C.Index == Int {
            count = { c.count }
            getElement = { c[$0 - c.startIndex] }
        }
    }
&lt;/pre&gt;

&lt;p&gt;Then the rest of the code in &lt;code&gt;GenericDataSource&lt;/code&gt; can easily call &lt;code&gt;count()&lt;/code&gt; and &lt;code&gt;getElement()&lt;/code&gt; to perform operations on that passed-in collection, without that collection type contaminating &lt;code&gt;GenericDataSource&lt;/code&gt;'s generic parameters.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;br&gt;Type erasure is a useful technique for stopping the viral spread of generics in your code, or just keeping interfaces simple. It's accomplished by wrapping the underlying type in a way which separates the API from the functionality. This can be done with an abstract public superclass and a private subclass, or it can be done by wrapping the API in functions. Type erasure with functions is particularly useful for simple cases where you only need a few pieces of functionality.&lt;/p&gt;

&lt;p&gt;The Swift standard library provides several type erased types that you can take advantage of. For example, &lt;code&gt;AnySequence&lt;/code&gt; wraps a &lt;code&gt;Sequence&lt;/code&gt;, as the name indicates, and lets you iterate over a sequence without needing to know its type. &lt;code&gt;AnyIterator&lt;/code&gt; is the companion to this type, providing a type-erased iterator. &lt;code&gt;AnyHashable&lt;/code&gt; provides type-erased access to &lt;code&gt;Hashable&lt;/code&gt; types. There are a few more for the various collection protocols. Search the documentation for &lt;code&gt;Any&lt;/code&gt; to see those. The standard library also uses type erasure as part of the &lt;code&gt;Codable&lt;/code&gt; API: &lt;code&gt;KeyedEncodingContainer&lt;/code&gt; and &lt;code&gt;KeyedDecodingContainer&lt;/code&gt; are type-erased wrappers around the corresponding container protocols, and are used to allow &lt;code&gt;Encoder&lt;/code&gt; and &lt;code&gt;Decoder&lt;/code&gt; implementations to provide containers without having to incorporate the container types into the API.&lt;/p&gt;

&lt;p&gt;That's it for today! Come back next time for more programming fun and games. Friday Q&amp;amp;A is driven by reader suggestions, so if you have a topic you'd like to see me cover here, please &lt;a href="mailto:mike@mikeash.com"&gt;send it in&lt;/a&gt;!&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;
</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/friday-qa-2017-12-08-type-erasure-in-swift.html</guid><pubDate>Fri, 15 Dec 2017 14:09:00 GMT</pubDate></item><item><title>Friday Q&amp;A 2017-11-10: Observing the A11's Heterogenous Cores
</title><link>http://www.mikeash.com/pyblog/friday-qa-2017-11-10-observing-the-a11s-heterogenous-cores.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;Friday Q&amp;amp;A 2017-11-10: Observing the A11's Heterogenous Cores
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2017 11 10  12 41"
                  tags="fridayqna hardware performance"
                  translations="http://fertlond.com/megfigyeljuk-az-a11-heterogen-mag/ Hungarian Szabolcs Csintalan"
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;Friday Q&amp;amp;A 2017-11-10: Observing the A11's Heterogenous Cores
&lt;/div&gt;
              &lt;p&gt;Apple's newest mobile CPU, the A11, brings a new level of heterogeneous computing to iOS, with both high and low performance cores that are always on. With the release of the iPhone X, I set out to see if I could observe these heterogeneous cores in action.&lt;/p&gt;

&lt;p&gt;(Yes, I'm aware that A11 devices could be obtained weeks ago when the iPhone 8 came out, but I didn't know anybody who got one, and it was hard to work up much excitement for it with the iPhone X coming not long after.)&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Brief Review&lt;/b&gt;&lt;br&gt;Multicore CPUs have been around in the Apple world since at least the Power Mac G5, which was available with up to two cores per CPU, and up to two CPUs in one machine.&lt;/p&gt;

&lt;p&gt;They've become the norm in many parts of the computing world. They're a natural response to increasing transistor counts as silicon chip fabrication technology continues its &lt;a href="https://en.wikipedia.org/wiki/Moore%27s_law"&gt;asymptotic march toward infinity&lt;/a&gt;. CPU designers always want to use more transistors to make their hardware faster, but there are diminishing returns. Rather than put more transistors into speeding up single-threaded performance, those transistors can be used to effectively put multiple CPUs onto a single chip. Those became known as CPU cores.&lt;/p&gt;

&lt;p&gt;These days you can buy CPUs with dozens or even hundreds of cores. That's often not the best tradeoff, since a lot of software won't take advantage of that many. It can be better to have fewer, faster cores instead. These days, typical user-facing computers have somewhere in the neighborhood of between two and 16 cores.&lt;/p&gt;

&lt;p&gt;Usually, all of the cores in a system are identical. Software can run on any or all of them and it doesn't make a bit of difference. If you dig deeply enough, some CPUs have sets of cores which can transfer data within the group more quickly than outside the group. It thus makes sense to put multiple threads working on the same data together within such a group. This is one of the reasons for the &lt;a href="https://developer.apple.com/library/content/releasenotes/Performance/RN-AffinityAPI/index.html"&gt;thread affinity API&lt;/a&gt;. Even so, the individual cores are still the same, they just aren't connected 100% symmetrically.&lt;/p&gt;

&lt;p&gt;Last year, Apple introduced their A10 CPU with heterogeneous cores. It's a four-core CPU, but those cores are not identical. Instead, it has two high-performance cores and two high-efficiency cores. The high-efficiency cores are slower, but consume much less power. For tasks that don't need to be completed as quickly as possible, running on the high-efficiency cores makes them use much less power. The system would switch between running software on the high-performance cores or the high-efficiency cores depending on the workload at any given time.&lt;/p&gt;

&lt;p&gt;This is a great idea, since iPhones are battery-powered and really want to use as little power as possible, and a lot of the work that iPhones do is relatively mundane tasks that don't need to be super fast, like downloading the latest tweets from your stream or loading the next chunk of audio data from the flash storage. It's a bit wasteful, though, since you have two cores just sitting there doing nothing at any given time. That's what you want in high-efficiency mode, since the whole idea is to run less hardware in order to consume less power, but in high-performance mode it's unfortunate that it can't take advantage of the two idle high-efficiency cores.&lt;/p&gt;

&lt;p&gt;This year, Apple introduced the A11 which takes the concept a step further. It has six cores: two high-performance and four high-efficiency. And unlike the A10, the A11 is able to use all six cores simultaneously. If the workload requires it, the A11 can run two threads on the high-performance cores while at the same time running four more threads on the high-efficiency cores.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Planning&lt;/b&gt;&lt;br&gt;I started thinking about how we could catch it in the act. The system probably moves threads around regularly, so timing a long-running computation probably wouldn't reveal much. Short-running computations would be hard to manage, since we'd want to ensure there were exactly six going simultaneously.&lt;/p&gt;

&lt;p&gt;I decided to write a test that would do a lot of small computations on each thread. It would then sample the timing of a few of those small computations during the process. Hopefully they would happen quickly enough that they would be unlikely to migrate between cores during the sample.&lt;/p&gt;

&lt;p&gt;The next question was what sort of computation to perform. I started out doing a SHA-256 of the current iteration count, but I was afraid that special cryptographic instructions might interfere with the results. I then tried a simple square root algorithm on the current iteration count. I thought this might be placing too much emphasis on floating-point performance, so I finally redid it to do an integer square root instead. Ultimately, all three gave the same basic results. I stuck with the integer square root since integer computations seem like the predominant workload in most software.&lt;/p&gt;

&lt;p&gt;My theory was that this should show a strongly bimodal distribution of running times on the A11. Did it work? Read below to find out!&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Code&lt;/b&gt;&lt;br&gt;Each thread runs a function which takes a number of iterations to perform, and a sampling interval. It returns the runtimes for each sample in an array, expressed in terms of the units provided by the &lt;code&gt;mach_absolute_time&lt;/code&gt; call:&lt;/p&gt;

&lt;pre&gt;    func ThreadFunc(iterations: UInt64, interval: UInt64) -&amp;gt; [UInt64] {
&lt;/pre&gt;

&lt;p&gt;It creates an array that will eventually hold all of the sampled running times:&lt;/p&gt;

&lt;pre&gt;        var times: [UInt64] = []
&lt;/pre&gt;

&lt;p&gt;Then it enters a loop for the given number of iterations:&lt;/p&gt;

&lt;pre&gt;        for i in 1 ... iterations {
&lt;/pre&gt;

&lt;p&gt;Before it does any work, it grabs the current time. It does this regardless of whether or not this is a run to sample, in an attempt to make the non-sampled iterations as similar as possible to the sampled iterations:&lt;/p&gt;

&lt;pre&gt;            let start = mach_absolute_time()
&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;iterations&lt;/code&gt; is a &lt;code&gt;UInt64&lt;/code&gt; but we want to work on &lt;code&gt;Int64&lt;/code&gt; numbers, so convert it and stash it in a variable:&lt;/p&gt;

&lt;pre&gt;            let x = Int64(i)
&lt;/pre&gt;

&lt;p&gt;I implemented the &lt;a href="https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method"&gt;Babylonian method&lt;/a&gt; for computing a square root. This consists of making a guess at the square root, then iteratively refining that guess by computing the average of &lt;code&gt;guess&lt;/code&gt; and &lt;code&gt;x / guess&lt;/code&gt;. Iterate until the desired precision is reached. It's not a very fast method, but we don't care about speed here, other than for consistency. I implemented this algorithm to run &lt;code&gt;1024&lt;/code&gt; iterations, which is way too many for any sort of reasonable result, but it provides a nice amount of work for our benchmarking purposes:&lt;/p&gt;

&lt;pre&gt;            var guess = x
            for _ in 0 ... 1024 {
                guess = (guess + x / guess) / 2
            }
&lt;/pre&gt;

&lt;p&gt;I had to make sure that the compiler would actually perform this computation and not throw away the whole thing as unnecessary. That meant I had to use the result somehow. I added a dummy check to see if the computed square root was way off from the actual one, with a print (which can't be optimized away) in that case:&lt;/p&gt;

&lt;pre&gt;            if abs(guess * guess - x) &amp;gt; 1000000000 {
                print("Found a really inexact square root! \(guess * guess) \(x)")
            }
&lt;/pre&gt;

&lt;p&gt;None of my actual runs ever hit the print, so there was no IO to skew the timing.&lt;/p&gt;

&lt;p&gt;With the work completed, it gets the current time again:&lt;/p&gt;

&lt;pre&gt;            let end = mach_absolute_time()
&lt;/pre&gt;

&lt;p&gt;If this is a sampling iteration, add the total runtime for this iteration to the &lt;code&gt;times&lt;/code&gt; array:&lt;/p&gt;

&lt;pre&gt;            if i % interval == 0 {
                times.append(end - start)
            }
        }
&lt;/pre&gt;

&lt;p&gt;Once all of the iterations are complete, return the times:&lt;/p&gt;

&lt;pre&gt;        return times
    }
&lt;/pre&gt;

&lt;p&gt;That's the code for a single thread. We also need code to spawn these threads and analyze the results. That code starts with some constants for the number of threads to spawn, the number of iterations to run, and the sampling interval:&lt;/p&gt;

&lt;pre&gt;    let threadCount = 6
    let iterations: UInt64 = 1000000
    let interval = iterations / 20
&lt;/pre&gt;

&lt;p&gt;It makes an array in which to gather all of the sampled times:&lt;/p&gt;

&lt;pre&gt;    var times: [UInt64] = []
&lt;/pre&gt;

&lt;p&gt;It will use an &lt;code&gt;NSCondition&lt;/code&gt; object to synchronize access to &lt;code&gt;times&lt;/code&gt; and wait for results to come in:&lt;/p&gt;

&lt;pre&gt;    let cond = NSCondition()
&lt;/pre&gt;

&lt;p&gt;We'll track the number of active threads so we can know when they've all completed:&lt;/p&gt;

&lt;pre&gt;    var runningThreads = threadCount
&lt;/pre&gt;

&lt;p&gt;With the initial setup complete, it starts spawning threads:&lt;/p&gt;

&lt;pre&gt;    for _ in 0 ..&amp;lt; threadCount {
        Thread.detachNewThread({
&lt;/pre&gt;

&lt;p&gt;The first thing each thread does is call &lt;code&gt;ThreadFunc&lt;/code&gt; to do the work and gather results:&lt;/p&gt;

&lt;pre&gt;            let oneTimes = ThreadFunc(iterations: iterations, interval: interval)
&lt;/pre&gt;

&lt;p&gt;Once the results come back, it appends them to &lt;code&gt;times&lt;/code&gt; and signals that this thread has completed:&lt;/p&gt;

&lt;pre&gt;            cond.lock()
            times.append(contentsOf: oneTimes)
            runningThreads -= 1
            cond.signal()
            cond.unlock()
        })
    }
&lt;/pre&gt;

&lt;p&gt;Back in the controlling code, it waits for all of the running threads to complete:&lt;/p&gt;

&lt;pre&gt;    cond.lock()
    while runningThreads &amp;gt; 0 {
        cond.wait()
    }
    cond.unlock()
&lt;/pre&gt;

&lt;p&gt;At this point, it has all samples in the &lt;code&gt;times&lt;/code&gt; array. Those samples are in terms of the units returned by &lt;code&gt;mach_absolute_time&lt;/code&gt;, which aren't all that useful on their own, although their relative values are still instructive. We'll convert them to nanoseconds:&lt;/p&gt;

&lt;pre&gt;    let nanoseconds = times.map({ machToNanoseconds($0) })
&lt;/pre&gt;

&lt;p&gt;Next, it runs a really simple clustering algorithm, which just steps through the samples and looks for gaps where the the relative difference between two samples is greater than some threshold. I wasn't sure which threshold value would be appropriate, so I had it try a bunch:&lt;/p&gt;

&lt;pre&gt;    for threshold in [0.01, 0.02, 0.03, 0.04, 0.05, 0.1] {
        print("Threshold: \(threshold)")
        let clusters = cluster(nanoseconds, threshold: threshold)
&lt;/pre&gt;

&lt;p&gt;This returns each cluster as an array of values within the cluster. The code then computes the mean, median, and standard deviation for each cluster ard prints them out:&lt;/p&gt;

&lt;pre&gt;        for cluster in clusters {
            let mean = cluster.reduce(0, +) / Double(cluster.count)
            let median = cluster[cluster.count / 2]
            let stddev = sqrt(cluster.map({ ($0 - mean) * ($0 - mean) }).reduce(0, +) / Double(cluster.count))

            print("count: \(cluster.count) - mean: \(mean) - median: \(median) - stddev: \(stddev)")
        }
        print("----------")
    }
&lt;/pre&gt;

&lt;p&gt;That's it! We're ready to see the results.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Results&lt;/b&gt;&lt;br&gt;I first ran it in on my iPhone 6+ to generate something of a baseline. The threshold of 0.05 seemed to provide the best clustering. Here are those results:&lt;/p&gt;

&lt;pre&gt;    Threshold: 0.05
    count: 120 - mean: 10993.4027777778 - median: 10958.3333333333 - stddev: 75.1148490502343
&lt;/pre&gt;

&lt;p&gt;Each sample takes almost the same amount of time. They're around 11 microseconds, with a standard deviation of only 75 nanoseconds.&lt;/p&gt;

&lt;p&gt;Here are the results from the iPhone X:&lt;/p&gt;

&lt;pre&gt;    Threshold: 0.05
    count: 54 - mean: 6969.90740740741 - median: 6958.33333333333 - stddev: 24.6068190109599
    count: 65 - mean: 9082.69230769231 - median: 9250.0 - stddev: 278.358695652034
    count: 1 - mean: 14125.0 - median: 14125.0 - stddev: 0.0
&lt;/pre&gt;

&lt;p&gt;There's one outlier, which which shows up pretty consistently across multiple runs. I'm not entirely sure why it would be so consistent. Maybe it takes a moment to ramp the CPU up to full speed? Ignoring the outlier, we see the heterogeneous cores clearly. There's one narrow cluster centered around ~7 microseconds, and another narrow cluster centered around ~9 microseconds, and nothing in between.&lt;/p&gt;

&lt;p&gt;The speed difference is smaller than I expected, but in my experiments it varied quite a bit depending on the type of work being done. This particular microbenchmark is probably bottlenecked on integer division, which is not the most representative task.&lt;/p&gt;

&lt;p&gt;Regardless, the signal is clear, with one chunk of samples running significantly faster than the other chunk, illustrating the high-performance and high-efficiency cores working simultaneously.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;br&gt;It's been interesting to follow the development of Apple's CPUs, and the heterogeneous cores in their latest iteration are really nifty. I expected it to take some work to observe them, but it ended up being straightforward. By running a long sequence of quick computations on multiple threads and sampling a few of them, the disparate cores become obvious.&lt;/p&gt;

&lt;p&gt;That's it for today! Friday Q&amp;amp;A will be back next time with more fun and games. In the meantime, if you have a topic you'd like to see covered here, please &lt;a href="mailto:mike@mikeash.com"&gt;send it in&lt;/a&gt;!&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;
</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/friday-qa-2017-11-10-observing-the-a11s-heterogenous-cores.html</guid><pubDate>Fri, 10 Nov 2017 12:41:00 GMT</pubDate></item><item><title>Friday Q&amp;A 2017-10-27: Locks, Thread Safety, and Swift: 2017 Edition
</title><link>http://www.mikeash.com/pyblog/friday-qa-2017-10-27-locks-thread-safety-and-swift-2017-edition.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;Friday Q&amp;amp;A 2017-10-27: Locks, Thread Safety, and Swift: 2017 Edition
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2017 10 27  11 28"
                  tags="fridayqna swift threading"
                  translations="http://kylinroc.github.io/friday-qa-2017-10-27-locks-thread-safety-and-swift-2017-edition.html Chinese 李孛"
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;Friday Q&amp;amp;A 2017-10-27: Locks, Thread Safety, and Swift: 2017 Edition
&lt;/div&gt;
              &lt;p&gt;Back in the dark ages of Swift 1, I wrote &lt;a href="friday-qa-2015-02-06-locks-thread-safety-and-swift.html"&gt;an article about locks and thread safety in Swift&lt;/a&gt;. The march of time has made it fairly obsolete, and reader &lt;a href="https://twitter.com/sethwillits"&gt;Seth Willits&lt;/a&gt; suggested I update it for the modern age, so here it is!&lt;/p&gt;

&lt;p&gt;This article will repeat some material from the old one, with changes to bring it up to date, and some discussion of how things have changed. Reading the previous article is not necessary before you read this one.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;A Quick Recap on Locks&lt;/b&gt;&lt;br&gt;A lock, or mutex, is a construct that ensures only one thread is active in a given region of code at any time. They're typically used to ensure that multiple threads accessing a mutable data structure all see a consistent view of it. There are several kinds of locks:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Blocking locks sleep a thread while it waits for another thread to release the lock. This is the usual behavior.&lt;/li&gt;
&lt;li&gt;Spinlocks use a busy loop to constantly check to see if a lock has been released. This is more efficient if waiting is rare, but wastes CPU time if waiting is common.&lt;/li&gt;
&lt;li&gt;Reader/writer locks allow multiple "reader" threads to enter a region simultaneously, but exclude all other threads (including readers) when a "writer" thread acquires the lock. This can be useful as many data structures are safe to read from multiple threads simultaneously, but unsafe to write while other threads are either reading or writing.&lt;/li&gt;
&lt;li&gt;Recursive locks allow a single thread to acquire the same lock multiple times. Non-recursive locks can deadlock, crash, or otherwise misbehave when re-entered from the same thread.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;b&gt;APIs&lt;/b&gt;&lt;br&gt;Apple's APIs have a bunch of different mutex facilities. This is a long but not exhaustive list:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;pthread_mutex_t&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pthread_rwlock_t&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;DispatchQueue&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;OperationQueue&lt;/code&gt; when configured to be serial.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;NSLock&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;os_unfair_lock&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In addition to this, Objective-C provides the &lt;code&gt;@synchronized&lt;/code&gt; language construct, which at the moment is implemented on top of &lt;code&gt;pthread_mutex_t&lt;/code&gt;. Unlike the others, &lt;code&gt;@synchronized&lt;/code&gt; doesn't use an explicit lock object, but rather treats an arbitrary Objective-C object as if it were a lock. A &lt;code&gt;@synchronized(someObject)&lt;/code&gt; section will block access to any other &lt;code&gt;@synchronized&lt;/code&gt; sections that use the same object pointer. These different facilities all have different behaviors and capabilities:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;pthread_mutex_t&lt;/code&gt; is a blocking lock that can optionally be configured as a recursive lock.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pthread_rwlock_t&lt;/code&gt; is a blocking reader/writer lock.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;DispatchQueue&lt;/code&gt; can be used as a blocking lock. It can be used as a reader/writer lock by configuring it as a concurrent queue and using barrier blocks. It also supports asynchronous execution of the locked region.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;OperationQueue&lt;/code&gt; can be used as a blocking lock. Like &lt;code&gt;dispatch_queue_t&lt;/code&gt; it supports asynchronous execution of the locked region.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;NSLock&lt;/code&gt; is blocking lock as an Objective-C class. Its companion class &lt;code&gt;NSRecursiveLock&lt;/code&gt; is a recursive lock, as the name indicates.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;os_unfair_lock&lt;/code&gt; is a less sophisticated, lower-level blocking lock.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Finally, &lt;code&gt;@synchronized&lt;/code&gt; is a blocking recursive lock.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Spinlocks, Lack of&lt;/b&gt;&lt;br&gt;I mentioned spinlocks as one type of lock, but none of the APIs listed here are spinlocks. This is a big change from the previous article, and is the main reason I'm writing this update.&lt;/p&gt;

&lt;p&gt;Spinlocks are really simple, and are efficient in the right circumstances. Unfortunately, they're a little too simple for the complexities of the modern world.&lt;/p&gt;

&lt;p&gt;The problem is thread priorities. When there are more runnable threads than CPU cores, higher priority threads get preference. This is a useful notion, because CPU cores are always a limited resource, and you don't want some time-insensitive background network operation stealing time from your UI while the user is trying to use it.&lt;/p&gt;

&lt;p&gt;When a high-priority thread gets stuck and has to wait for a low-priority thread to finish some work, but the high-priority thread prevents the low-priority thread from actually performing that work, it can result in long hangs or even a permanent deadlock.&lt;/p&gt;

&lt;p&gt;The deadlock scenario looks like this, where H is a high-priority thread and L is a low-priority thread:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;L acquires the spinlock.&lt;/li&gt;
&lt;li&gt;L starts doing some work.&lt;/li&gt;
&lt;li&gt;H becomes ready to run, and preempts L.&lt;/li&gt;
&lt;li&gt;H attempts to acquire the spinlock, but fails, because L still holds it.&lt;/li&gt;
&lt;li&gt;H begins angrily spinning on the spinlock, repeatedly trying to acquire it, and monopolizing the CPU.&lt;/li&gt;
&lt;li&gt;H can't proceed until L finishes its work. L can't finish its work unless H stops angrily spinning on the spinlock.&lt;/li&gt;
&lt;li&gt;Sadness.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There are ways to solve this problem. For example, H might donate its priority to L in step 4, allowing L to complete its work in a timely fashion. It's possible to make a spinlock that solves this problem, but Apple's old spinlock API, &lt;code&gt;OSSpinLock&lt;/code&gt;, doesn't.&lt;/p&gt;

&lt;p&gt;This was fine for a long time, because thread priorities didn't get much use on Apple's platforms, and the priority system used dynamic priorities that kept the deadlock scenario from persisting too long. More recently, &lt;a href="https://developer.apple.com/library/content/documentation/Performance/Conceptual/EnergyGuide-iOS/PrioritizeWorkWithQoS.html"&gt;quality of service classes&lt;/a&gt; made different priorities more common, and made the deadlock scenario more likely to persist.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;OSSpinLock&lt;/code&gt;, which did a fine job for so long, stopped being a good idea with the release of iOS 8 and macOS 10.10. It's now been formally deprecated. The replacement is &lt;code&gt;os_unfair_lock&lt;/code&gt;, which fills the same overall purpose as a low-level, unsophisticated, cheap lock, but is sufficiently sophisticated to avoid problems with priorities.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Value Types&lt;/b&gt;&lt;br&gt;Note that &lt;code&gt;pthread_mutex_t&lt;/code&gt;, &lt;code&gt;pthread_rwlock_t&lt;/code&gt;, and &lt;code&gt;os_unfair_lock&lt;/code&gt; are value types, not reference types. That means that if you use &lt;code&gt;=&lt;/code&gt; on them, you make a copy. This is important, because these types can't be copied! If you copy one of the &lt;code&gt;pthread&lt;/code&gt; types, the copy will be unusable and may crash when you try to use it. The &lt;code&gt;pthread&lt;/code&gt; functions that work with these types assume that the values are at the same memory addresses as where they were initialized, and putting them somewhere else afterwards is a bad idea. &lt;code&gt;os_unfair_lock&lt;/code&gt; won't crash, but you get a completely separate lock out of it which is never what you want.&lt;/p&gt;

&lt;p&gt;If you use these types, you must be careful never to copy them, whether explicitly with a &lt;code&gt;=&lt;/code&gt; operator, or implicitly by, for example, embedding them in a &lt;code&gt;struct&lt;/code&gt; or capturing them in a closure.&lt;/p&gt;

&lt;p&gt;Additionally, since locks are inherently mutable objects, this means you need to declare them with &lt;code&gt;var&lt;/code&gt; instead of &lt;code&gt;let&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The others are reference types, meaning they can be passed around at will, and can be declared with &lt;code&gt;let&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Initialization&lt;/b&gt;&lt;br&gt;You must be careful with the &lt;code&gt;pthread&lt;/code&gt; locks, because you can create a value using the empty &lt;code&gt;()&lt;/code&gt; initializer, but that value won't be a valid lock. These locks must be separately initialized using &lt;code&gt;pthread_mutex_init&lt;/code&gt; or &lt;code&gt;pthread_rwlock_init&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;    var mutex = pthread_mutex_t()
    pthread_mutex_init(&amp;amp;mutex, nil)
&lt;/pre&gt;

&lt;p&gt;It's tempting to write an extension on these types which wraps up the initialization. However, there's no guarantee that initializers work on the variable directly, rather than on a copy. Since these types can't be safely copied, such an extension can't be safely written unless you have it return a pointer or a wrapper class.&lt;/p&gt;

&lt;p&gt;If you use these APIs, don't forget to call the corresponding &lt;code&gt;destroy&lt;/code&gt; function when it's time to dispose of the lock.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Use&lt;/b&gt;&lt;br&gt;&lt;code&gt;DispatchQueue&lt;/code&gt; has a callback-based API which makes it natural to use it safely. Depending on whether you need the protected code to run synchronously or asynchronously, call &lt;code&gt;sync&lt;/code&gt; or &lt;code&gt;async&lt;/code&gt; and pass it the code to run:&lt;/p&gt;

&lt;pre&gt;    queue.sync(execute: { ... })
    queue.async(execute: { ... })
&lt;/pre&gt;

&lt;p&gt;For the &lt;code&gt;sync&lt;/code&gt; case, the API is nice enough to capture the return value from the protected code and provide it as the return value of the &lt;code&gt;sync&lt;/code&gt; method:&lt;/p&gt;

&lt;pre&gt;    let value = queue.sync(execute: { return self.protectedProperty })
&lt;/pre&gt;

&lt;p&gt;You can even &lt;code&gt;throw&lt;/code&gt; errors inside the protected block and they'll propagate out.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;OperationQueue&lt;/code&gt; is similar, although it doesn't have a built-in way to propogate return values or errors. You'll have to build that yourself, or use &lt;code&gt;DispatchQueue&lt;/code&gt; instead.&lt;/p&gt;

&lt;p&gt;The other APIs require separate locking and unlocking calls, which can be exciting when you forget one of them. The calls look like this:&lt;/p&gt;

&lt;pre&gt;    pthread_mutex_lock(&amp;amp;mutex)
    ...
    pthread_mutex_unlock(&amp;amp;mutex)

    nslock.lock()
    ...
    nslock.unlock()

    os_unfair_lock_lock(&amp;amp;lock)
    ...
    os_unfair_lock_unlock(&amp;amp;lock)
&lt;/pre&gt;

&lt;p&gt;Since the APIs are virtually identical, I'll use &lt;code&gt;nslock&lt;/code&gt; for further examples. The others are the same, but with different names.&lt;/p&gt;

&lt;p&gt;When the protected code is simple, this works well. But what if it's more complicated? For example:&lt;/p&gt;

&lt;pre&gt;    nslock.lock()
    if earlyExitCondition {
        return nil
    }
    let value = compute()
    nslock.unlock()
    return value
&lt;/pre&gt;

&lt;p&gt;Oops, sometimes you don't unlock the lock! This is a good way to make hard-to-find bugs. Maybe you're always disciplined with your &lt;code&gt;return&lt;/code&gt; statements and never do this. What if you throw an error?&lt;/p&gt;

&lt;pre&gt;    nslock.lock()
    guard something else { throw error }
    let value = compute()
    nslock.unlock()
    return value
&lt;/pre&gt;

&lt;p&gt;Same problem! Maybe you're really disciplined and would never do this either. Then you're safe, but even then the code is a bit ugly:&lt;/p&gt;

&lt;pre&gt;    nslock.lock()
    let value = compute()
    nslock.unlock()
    return value
&lt;/pre&gt;

&lt;p&gt;The obvious fix for this is to use Swift's &lt;code&gt;defer&lt;/code&gt; mechanism. The moment you lock, defer the unlock. Then no matter how you exit the code, the lock will be released:&lt;/p&gt;

&lt;pre&gt;    nslock.lock()
    defer { nslock.unlock() }
    return compute()
&lt;/pre&gt;

&lt;p&gt;This works for early returns, throwing errors, or just normal code.&lt;/p&gt;

&lt;p&gt;It's still annoying to have to write two lines, so we can wrap everything up in a callback-based function like &lt;code&gt;DispatchQueue&lt;/code&gt; has:&lt;/p&gt;

&lt;pre&gt;    func withLocked&amp;lt;T&amp;gt;(_ lock: NSLock, _ f: () throws -&amp;gt; T) rethrows -&amp;gt; T {
        lock.lock()
        defer { lock.unlock() }
        return try f()
    }

    let value = withLocked(lock, { return self.protectedProperty })
&lt;/pre&gt;

&lt;p&gt;When implementing this for value types, you'll need to be sure to take a &lt;em&gt;pointer&lt;/em&gt; to the lock rather than the lock itself. Remember, you don't want to copy these things! The &lt;code&gt;pthread&lt;/code&gt; version would look like this:&lt;/p&gt;

&lt;pre&gt;    func withLocked&amp;lt;T&amp;gt;(_ mutexPtr: UnsafeMutablePointer&amp;lt;pthread_mutex_t&amp;gt;, _ f: () throws -&amp;gt; T) rethrows -&amp;gt; T {
        pthread_mutex_lock(mutexPtr)
        defer { pthread_mutex_unlock(mutexPtr) }
        return try f()
    }

    let value = withLocked(&amp;amp;mutex, { return self.protectedProperty })
&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;Choosing Your Lock API&lt;/b&gt;&lt;br&gt;&lt;code&gt;DispatchQueue&lt;/code&gt; is an obvious favorite. It has a nice Swifty API and is pleasant to use. The Dispatch library gets a huge amount of attention from Apple, and that means that it can be counted on to perform well, work reliably, and get lots of cool new features.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;DispatchQueue&lt;/code&gt; allows for a lot of nifty advanced uses, such as scheduling timers or event sources to fire directly on the queue you're using as a lock, ensuring that the handlers are synchronized with other things using the queue. The ability to set target queues allows expressing complex lock hierarchies. Custom concurrent queues can be easily used as reader-writer locks. You only have to change a single letter to execute protected code asynchronously on a background thread rather than synchronously. And the API is easy to use and hard to misuse. It's a win all around. There's a reason GCD quickly became one of my favorite APIs, and remains one to this day.&lt;/p&gt;

&lt;p&gt;Like most things, it's not perfect. A dispatch queue is represented by an object in memory, so there's a bit of overhead. They're missing some niche features, like condition variables or recursiveness. Every once in a great while, it's useful to be able to make individual lock and unlock calls rather than be forced to use a callback-based API. &lt;code&gt;DispatchQueue&lt;/code&gt; is usually the right choice, and is a great default if you don't know what to pick, but there are occasionally reasons to use others.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;os_unfair_lock&lt;/code&gt; can be a good choice when per-lock overhead is important (because for some reason you have a huge number of them) and you don't need fancy features. It's implemented as a single 32-bit integer which you can place wherever you need it, so overhead is small. &lt;/p&gt;

&lt;p&gt;As the name hints, one of the features that &lt;code&gt;os_unfair_lock&lt;/code&gt; is missing is fairness. Lock fairness means that there's at least some attempt to ensure that different threads waiting on a lock all get a chance to acquire it. Without fairness, it's possible for a thread that rapidly releases and re-acquires the lock to monopolize it while other threads are waiting.&lt;/p&gt;

&lt;p&gt;Whether or not this is a problem depends on what you're doing. There are some use cases where fairness is necessary, and some where it doesn't matter at all. The lack of fairness allows &lt;code&gt;os_unfair_lock&lt;/code&gt; to have better performance, so it can provide an edge in cases where fairness isn't needed.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pthread_mutex&lt;/code&gt; is somewhere in the middle. It's considerably larger than &lt;code&gt;os_unfair_lock&lt;/code&gt;, at 64 bytes, but you can still control where it's stored. It implements fairness, although this is a detail of Apple's implementation, not part of the API spec. It also provides various other advanced features, such as the ability to make the mutex recursive, and fancy thread priority stuff.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pthread_rwlock&lt;/code&gt; provides a reader/writer lock. It takes up a whopping 200 bytes and doesn't provide much in the way of interesting features, so there doesn't seem to be much reason to use it over a concurrent &lt;code&gt;DispatchQueue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;NSLock&lt;/code&gt; is a wrapper around &lt;code&gt;pthread_mutex&lt;/code&gt;. It's hard to come up with a use case for this, but it could be useful if you need explicit lock/unlock calls but don't want the hassle of manually initializing and destroying a &lt;code&gt;pthread_mutex&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;OperationQueue&lt;/code&gt; offers callback-based API like &lt;code&gt;DispatchQueue&lt;/code&gt;, with some advanced features for things like dependency management between operations, but without many of the other features offered by &lt;code&gt;DispatchQueue&lt;/code&gt;. There is little reason to use &lt;code&gt;OperationQueue&lt;/code&gt; as a locking API, although it can be useful for other things.&lt;/p&gt;

&lt;p&gt;In short: &lt;code&gt;DispatchQueue&lt;/code&gt; is probably the right choice. In certain circumstances, &lt;code&gt;os_unfair_lock&lt;/code&gt; may be better. The others are usually not the ones to use.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;br&gt;Swift has no language facilities for thread synchronization, but the APIs make up for it. GCD remains one of Apple's crown jewels, and the Swift API for it is great. For the rare occasions where it's not suitable, there are many other options to choose from. We don't have &lt;code&gt;@synchronized&lt;/code&gt; or atomic properties, but we have things that are better.&lt;/p&gt;

&lt;p&gt;That wraps it up for this time. Check back again for more fun stuff. If you get bored in the meantime, &lt;a href="/book.html"&gt;buy one of my books!&lt;/a&gt; Friday Q&amp;amp;A is driven by reader ideas, so if you have a topic you'd like to see covered here, please &lt;a href="mailto:mike@mikeash.com"&gt;send it in&lt;/a&gt;!&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;
</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/friday-qa-2017-10-27-locks-thread-safety-and-swift-2017-edition.html</guid><pubDate>Fri, 27 Oct 2017 11:28:00 GMT</pubDate></item><item><title>The Complete Friday Q&amp;A Volumes II and III Are Out!
</title><link>http://www.mikeash.com/pyblog/the-complete-friday-qa-volumes-ii-and-iii-are-out.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;The Complete Friday Q&amp;amp;A Volumes II and III Are Out!
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2017 10 10  16 19"
                  tags="advertisement"
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;The Complete Friday Q&amp;amp;A Volumes II and III Are Out!
&lt;/div&gt;
              &lt;p&gt;It's finally here! I'm pleased to present &lt;em&gt;The Complete Friday Q&amp;amp;A&lt;/em&gt; Volumes II and III.&lt;/p&gt;

&lt;p&gt;These collect my blog posts from November 2010 through 2016. As with Volume I, they are available in both digital and print versions.&lt;/p&gt;

&lt;p&gt;&lt;a href="/book-store.html"&gt;Click here to see my store for the digital versions.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the print versions, or for links to the digital versions on iBooks (and eventually Kindle, but that will take some time), &lt;a href="/book.html"&gt;visit my book page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When purchasing directly from me, you'll receive $10 off each additional volume in a multi-volume purchase. If you already own Volume I and would like to complete the set, &lt;a href="mailto:mike@mikeash.com"&gt;e-mail me&lt;/a&gt; with your receipt to get a coupon for $10 off.&lt;/p&gt;

&lt;p&gt;If you would like both the print and digital versions, place an order for the print versions, then &lt;a href="mailto:mike@mikeash.com"&gt;e-mail me&lt;/a&gt; with your receipt to get a coupon for $20 off the digital copies.&lt;/p&gt;

&lt;p&gt;It has taken me far too long, but I'm so glad to finally have these out. Many thanks to my guest authors, reviewers, and readers.&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;
</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/the-complete-friday-qa-volumes-ii-and-iii-are-out.html</guid><pubDate>Tue, 10 Oct 2017 16:19:00 GMT</pubDate></item><item><title>Friday Q&amp;A 2017-10-06: Type-Safe User Defaults
</title><link>http://www.mikeash.com/pyblog/friday-qa-2017-10-06-type-safe-user-defaults.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;Friday Q&amp;amp;A 2017-10-06: Type-Safe User Defaults
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2017 10 06  12 55"
                  tags="fridayqna swift"
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;Friday Q&amp;amp;A 2017-10-06: Type-Safe User Defaults
&lt;/div&gt;
              &lt;p&gt;It's fun to re-imagine traditional techniques with a Swift twist. I've implemented a type-safe layer on top of the venerable &lt;code&gt;NSUserDefaults&lt;/code&gt;, and I'm going to discuss my little library today. Credit/blame for this idea goes to local reader José Vazquez, although he inspired it by accident while talking about something else.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;User Defaults&lt;/b&gt;&lt;br&gt;&lt;code&gt;NSUserDefaults&lt;/code&gt;, or just &lt;code&gt;UserDefaults&lt;/code&gt; in Swift, is a typical dynamically-typed string-oriented Objective-C API. It stores string keys and property list values. This is perfectly fine, but I wanted to do better. I came up with this wishlist:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Keys should be declared, not written ad hoc at the point of use. You can do this with &lt;code&gt;UserDefaults&lt;/code&gt; by declaring string constants, but it's easy to get lazy and not do it.&lt;/li&gt;
&lt;li&gt;There should be no repetition in the common case. A key's string should automatically be made to match its identifier in the code.&lt;/li&gt;
&lt;li&gt;No casting should be required. Keys should have a value type associated with them and the conversion handled internally.&lt;/li&gt;
&lt;li&gt;It should interoperate smoothly with values read and written directly through &lt;code&gt;UserDefaults&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Non-plist value types should be supported through &lt;code&gt;Codable&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Default values should be specified as part of the key rather than registered separately.&lt;/li&gt;
&lt;li&gt;The value should be made available as a property so that it can be the target of mutating methods and operators.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I managed to hit all of these points, although the implementation is somewhat gnarly.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Code&lt;/b&gt;&lt;br&gt;As usual, the code is available on GitHub if you want to play with it or see it all in one place:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mikeash/TSUD"&gt;https://github.com/mikeash/TSUD&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Example Use&lt;/b&gt;&lt;br&gt;Before we get into the implementation, let's take at what it looks like to use this API. That will inform the implementation choices and make it clearer why things work the way they do.&lt;/p&gt;

&lt;p&gt;To declare a key, write a &lt;code&gt;struct&lt;/code&gt; conforming to the &lt;code&gt;TSUD&lt;/code&gt; protocol. Inside, implement a single &lt;code&gt;static&lt;/code&gt; property called &lt;code&gt;defaultValue&lt;/code&gt; which contains the value to be returned if &lt;code&gt;UserDefaults&lt;/code&gt; doesn't contain a value:&lt;/p&gt;

&lt;pre&gt;    struct fontSize: TSUD {
        static let defaultValue = 12.0
    }
&lt;/pre&gt;

&lt;p&gt;To read or write the value, use the &lt;code&gt;value&lt;/code&gt; property on the &lt;code&gt;struct&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;    let font = NSFont.systemFont(ofSize: fontSize.value)
    fontSize.value = 14.0
&lt;/pre&gt;

&lt;p&gt;Since &lt;code&gt;value&lt;/code&gt; is just a property, you can do disturbing and unnatural things like &lt;code&gt;+=&lt;/code&gt; to it.&lt;/p&gt;

&lt;pre&gt;    fontSize.value += 5.0
&lt;/pre&gt;

&lt;p&gt;If you want to be able to detect the lack of a value and handle it specially rather than getting a default value, declare &lt;code&gt;defaultValue&lt;/code&gt; to be optional and set it to &lt;code&gt;nil&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;    struct username: TSUD {
        static let defaultValue: String? = nil
    }
&lt;/pre&gt;

&lt;p&gt;Then use it like any other optional:&lt;/p&gt;

&lt;pre&gt;    if let username = username.value {
        field.string = username
    } else {
        username.value = promptForUsername()
    }
&lt;/pre&gt;

&lt;p&gt;By default, &lt;code&gt;TSUD&lt;/code&gt; types correspond to a &lt;code&gt;UserDefaults&lt;/code&gt; key matching their type name. These examples would be stored under &lt;code&gt;"fontSize"&lt;/code&gt; and &lt;code&gt;"username"&lt;/code&gt;. If you need to override this (for example, because you want to access a key that has a space in it, or you don't like the key's capitalization in your code), implement the &lt;code&gt;stringKey&lt;/code&gt; property:&lt;/p&gt;

&lt;pre&gt;    struct hasWidgets: TSUD {
        static let defaultValue = false
        static let stringKey = "Has Widgets"
    }
&lt;/pre&gt;

&lt;p&gt;Arbitrary &lt;code&gt;Codable&lt;/code&gt; types are supported. They are encoded as property list objects:&lt;/p&gt;

&lt;pre&gt;    struct Person: Codable {
        var name: String
        var quest: String
        var age: Int
    }

    struct testPerson: TSUD {
        static let defaultValue: Person? = nil
    }
&lt;/pre&gt;

&lt;p&gt;If you prefer, you can also use methods to get and set the value:&lt;/p&gt;

&lt;pre&gt;    if hasWidgets.get() {
        hasWidgets.set(false)
    }
&lt;/pre&gt;

&lt;p&gt;These methods allow you to specify the &lt;code&gt;UserDefaults&lt;/code&gt; object to work with, in the unlikely event that you want to work with something other than &lt;code&gt;UserDefaults.standard&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;    let otherDefaults = UserDefaults(suiteName: "...")!
    if hasWidgets.get(otherDefaults) {
        // That other thing has widgets!
    }
&lt;/pre&gt;

&lt;p&gt;If you want to access the value in another &lt;code&gt;UserDefaults&lt;/code&gt; instance as a mutable value, there's a subscript which takes a &lt;code&gt;UserDefaults&lt;/code&gt; instance and provides the value. Unfortunately, Swift doesn't allow &lt;code&gt;static&lt;/code&gt; subscripts, so you have to instantiate the key type:&lt;/p&gt;

&lt;pre&gt;    fontSize()[otherDefaults] += 10.0
&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;Implementation&lt;/b&gt;&lt;br&gt;We'll start with the protocol itself. It contains an &lt;code&gt;associatedtype&lt;/code&gt; for the value type, an empty &lt;code&gt;init&lt;/code&gt; method so the type can be instantiated, and the two properties discussed above:&lt;/p&gt;

&lt;pre&gt;    public protocol TSUD {
        associatedtype ValueType: Codable

        init()

        static var defaultValue: ValueType { get }

        static var stringKey: String { get }
    }
&lt;/pre&gt;

&lt;p&gt;We'll provide a default implementation for &lt;code&gt;stringKey&lt;/code&gt; that uses the type name:&lt;/p&gt;

&lt;pre&gt;    public extension TSUD {
        static var stringKey: String {
            let s = String(describing: Self.self)
&lt;/pre&gt;

&lt;p&gt;In certain circumstances, Swift adds a little numeric tag at the end like &lt;code&gt;"hasWidgets #1"&lt;/code&gt; If this name contains one, we strip it off. Otherwise we return the name directly:&lt;/p&gt;

&lt;pre&gt;            if let index = s.index(of: " ") {
                return String(s[..&amp;lt;index])
            } else {
                return s
            }
        }
    }
&lt;/pre&gt;

&lt;p&gt;Later on, we'll need the ability to detect whether a value of &lt;code&gt;ValueType&lt;/code&gt; is an &lt;code&gt;Optional&lt;/code&gt; set to &lt;code&gt;nil&lt;/code&gt;. This is not as easy as checking it with &lt;code&gt;== nil&lt;/code&gt;, because it needs to work with arbitrary types wrapped in an &lt;code&gt;Optional&lt;/code&gt;. The best technique I could find was to create a small protocol with an &lt;code&gt;isNil&lt;/code&gt; property, and make &lt;code&gt;Optional&lt;/code&gt; conform to it. Code that wants to check for &lt;code&gt;nil&lt;/code&gt; can attempt to cast to the protocol, check &lt;code&gt;isNil&lt;/code&gt; on success, and assume &lt;code&gt;false&lt;/code&gt; on failure:&lt;/p&gt;

&lt;pre&gt;    private protocol OptionalP {
        var isNil: Bool { get }
    }

    extension Optional: OptionalP {
        var isNil: Bool { return self == nil }
    }
&lt;/pre&gt;

&lt;p&gt;The main API of &lt;code&gt;TSUD&lt;/code&gt; is implemented in an extension:&lt;/p&gt;

&lt;pre&gt;    extension TSUD {
&lt;/pre&gt;

&lt;p&gt;Getting and setting the value is implemented with subscripting. This calls helper methods to encode and decode the value:&lt;/p&gt;

&lt;pre&gt;        public subscript(nsud: UserDefaults) -&amp;gt; ValueType {
            get {
                return decode(nsud.object(forKey: Self.stringKey)) ?? Self.defaultValue
            }
            nonmutating set {
                nsud.set(encode(newValue), forKey: Self.stringKey)
            }
        }
&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;get&lt;/code&gt; and &lt;code&gt;set&lt;/code&gt; methods call through to the subscript. Because these are &lt;code&gt;static&lt;/code&gt; methods and we can't have a &lt;code&gt;static&lt;/code&gt; subscript, these instantiate &lt;code&gt;self&lt;/code&gt; using the empty &lt;code&gt;init()&lt;/code&gt; in the protocol:&lt;/p&gt;

&lt;pre&gt;        public static func get(_ nsud: UserDefaults = .standard) -&amp;gt; ValueType {
            return self.init()[nsud]
        }

        public static func set(_ value: ValueType, _ nsud: UserDefaults = .standard) {
            self.init()[nsud] = value
        }
&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;value&lt;/code&gt; is a computed property that calls &lt;code&gt;get&lt;/code&gt; and &lt;code&gt;set&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;        public static var value: ValueType {
            get {
                return get()
            }
            set {
                set(newValue)
            }
        }
&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;encode&lt;/code&gt; method takes care of transforming a value to a property list object suitable for &lt;code&gt;UserDefaults&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;        private func encode(_ value: ValueType) -&amp;gt; Any? {
&lt;/pre&gt;

&lt;p&gt;There are some special cases to handle. First, if &lt;code&gt;ValueType&lt;/code&gt; is an optional and &lt;code&gt;value&lt;/code&gt; contains &lt;code&gt;nil&lt;/code&gt;, we return nil:&lt;/p&gt;

&lt;pre&gt;            switch value {
            case let value as OptionalP where value.isNil: return nil
&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;Date&lt;/code&gt; and &lt;code&gt;Data&lt;/code&gt; are returned unchanged. For some reason, &lt;code&gt;Date&lt;/code&gt;'s implementation of &lt;code&gt;Codable&lt;/code&gt; encodes its value as a raw number, even though property lists support &lt;code&gt;Date&lt;/code&gt; values natively. Likewise, &lt;code&gt;Data&lt;/code&gt; encodes its value as an array of numbers even though &lt;code&gt;Data&lt;/code&gt; is a valid property list type. We get past that by avoiding any encoding for these types. It's possible that more property list types need this treatment, in which case it's easy to add them here:&lt;/p&gt;

&lt;pre&gt;            case is Date: return value
            case is Data: return value
&lt;/pre&gt;

&lt;p&gt;All other values get encoded. We use &lt;code&gt;PropertyListEncoder&lt;/code&gt; to encode the value:&lt;/p&gt;

&lt;pre&gt;            default:
                let data = try? PropertyListEncoder().encode([value])
&lt;/pre&gt;

&lt;p&gt;If this fails, we'll return &lt;code&gt;nil&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;                guard let dataUnwrapped = data else { return nil }
&lt;/pre&gt;

&lt;p&gt;You'll notice that we're not encoding the value directly, but rather an array containing the value. For some reason, &lt;code&gt;PropertyListEncoder&lt;/code&gt; does not support numbers or dates as top-level objects. Wrapping them in an array convinces it to work. We'll extract it back out afterwards.&lt;/p&gt;

&lt;p&gt;Unfortunately, there's a bit of an impedence mismatch here. &lt;code&gt;PropertyListEncoder&lt;/code&gt; produces binary data in property list format, but we want property list &lt;em&gt;objects&lt;/em&gt; which can be passed to &lt;code&gt;UserDefaults&lt;/code&gt;. We'll turn that data back into objects by using &lt;code&gt;PropertyListSerialization&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;                let wrappedPlist = (try? PropertyListSerialization.propertyList(from: dataUnwrapped, options: [], format: nil)) as? [Any]
&lt;/pre&gt;

&lt;p&gt;This is really ugly. It's unfortunate that &lt;code&gt;PropertyListEncoder&lt;/code&gt; doesn't have an option to skip the serialization step and provide objects directly. We could make our own, but that's more ambitious than I was willing to get for this.&lt;/p&gt;

&lt;p&gt;Once we have the property list object, we can index into the wrapper array to fetch the object we actually want:&lt;/p&gt;

&lt;pre&gt;                return wrappedPlist?[0]
            }
        }

            let data = try? PropertyListEncoder().encode([value])
            guard let dataUnwrapped = data else { return nil }
            let wrappedPlist = (try? PropertyListSerialization.propertyList(from: dataUnwrapped, options: [], format: nil)) as? [Any]
            return wrappedPlist?[0]
&lt;/pre&gt;

&lt;p&gt;Decoding is the same thing in reverse. The &lt;code&gt;decode&lt;/code&gt; method takes an optional to make it easier to handle &lt;code&gt;nil&lt;/code&gt; from &lt;code&gt;UserDefaults&lt;/code&gt;. It returns a &lt;code&gt;nil&lt;/code&gt; value in that case:&lt;/p&gt;

&lt;pre&gt;        private func decode(_ plist: Any?) -&amp;gt; ValueType? {
            guard let plist = plist else { return nil }
&lt;/pre&gt;

&lt;p&gt;As with encoding, decoding a &lt;code&gt;Date&lt;/code&gt; or &lt;code&gt;Data&lt;/code&gt; is special-cased to pass the value through:&lt;/p&gt;

&lt;pre&gt;            switch ValueType.self {
            case is Date.Type,
                 is Data.Type:
                return plist as? ValueType
&lt;/pre&gt;

&lt;p&gt;For all other values, we'll use &lt;code&gt;PropertyListDecoder&lt;/code&gt;. As with the encoder, there's no way to have &lt;code&gt;PropertyListDecoder&lt;/code&gt; work on property list objects directly. It only works with data, so we have to encode the object as data, then decode that:&lt;/p&gt;

&lt;pre&gt;            default:
                let data = try? PropertyListSerialization.data(fromPropertyList: plist, format: .binary, options: 0)
                guard let dataUnwrapped = data else { return nil }
                return try? PropertyListDecoder().decode(ValueType.self, from: dataUnwrapped)
            }
        }
&lt;/pre&gt;

&lt;p&gt;Unlike the encoder, the decoder is perfectly happy with a top-level number or date, so the array dance is not necessary on this end.&lt;/p&gt;

&lt;p&gt;With encoding and decoding implemented, we've reached the end!&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;br&gt;This was a fun project for an NSCoderNight, and it's interesting to see just how far it can go. The result ends up looking pretty nice, although the problem it's solving isn't particularly pressing. This code is mostly intended as an educational experiment, but it could be used for practical purposes too.&lt;/p&gt;

&lt;p&gt;That's it for today. Come back again for more terrifying tales of coding bravery. Friday Q&amp;amp;A is driven by reader ideas (some of them accidental), so as always, if you have a topic you'd like to see covered here, please &lt;a href="mailto:mike@mikaesh.com"&gt;send it in&lt;/a&gt;!&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;
</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/friday-qa-2017-10-06-type-safe-user-defaults.html</guid><pubDate>Fri, 06 Oct 2017 12:55:00 GMT</pubDate></item><item><title>Friday Q&amp;A 2017-09-22: Swift 4 Weak References
</title><link>http://www.mikeash.com/pyblog/friday-qa-2017-09-22-swift-4-weak-references.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;Friday Q&amp;amp;A 2017-09-22: Swift 4 Weak References
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2017 09 23  00 57"
                  tags="fridayqna swift"
                  translations="https://ddddxxx.github.io/2017/09/27/swift-4-weak-references/ Chinese Xiang Deng"
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;Friday Q&amp;amp;A 2017-09-22: Swift 4 Weak References
&lt;/div&gt;
              &lt;p&gt;Soon after Swift was initially open sourced, I wrote &lt;a href="https://mikeash.com/pyblog/friday-qa-2015-12-11-swift-weak-references.html"&gt;an article about how weak references are implemented&lt;/a&gt;. Time moves on and things change, and the implementation is different from what it once was. Today I'm going to talk about the current implementation and how it works compared to the old one, a topic suggested by Guillaume Lessard.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Old Implementation&lt;/b&gt;&lt;br&gt;For those of you who have forgotten the old implementation and don't feel like reading through the last article, let's briefly recall how it works.&lt;/p&gt;

&lt;p&gt;In the old implementation, Swift objects have two reference counts: a strong count and a weak count. When the strong count reaches zero while the weak count is still non-zero, the object is destroyed but its memory is not deallocated. This leaves a sort of zombie object sitting in memory, which the remaining weak references point to.&lt;/p&gt;

&lt;p&gt;When a weak reference is loaded, the runtime checks to see if the object is a zombie. If it is, it zeroes out the weak reference and decrements the weak reference count. Once the weak count reaches zero, the object's memory is deallocated. This means that zombie objects are eventually cleared out once all weak references to them are accessed.&lt;/p&gt;

&lt;p&gt;I loved the simplicity of this implementation, but it had some flaws. One flaw was that the zombie objects could stay in memory for a long time. For classes with large instances (because they contain a lot of properties, or use something like &lt;code&gt;ManagedBuffer&lt;/code&gt; to allocate extra memory inline), this could be a serious waste.&lt;/p&gt;

&lt;p&gt;Another problem, which &lt;a href="https://bugs.swift.org/browse/SR-192"&gt;I discovered&lt;/a&gt; after writing the old article, was that the implementation wasn't thread-safe for concurrent reads. Oops! This was patched, but the discussion around it revealed that the implementers wanted a better implementation of weak references anyway, which would be more resilient to such things.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Object Data&lt;/b&gt;&lt;br&gt;There are many pieces of data which make up "an object" in Swift.&lt;/p&gt;

&lt;p&gt;First, and most obviously, there are all of the stored properties declared in the source code. These are directly accessible by the programmer.&lt;/p&gt;

&lt;p&gt;Second, there is the object's class. This is used for dynamic dispatch and the &lt;code&gt;type(of:)&lt;/code&gt; built-in function. This is mostly hidden, although dynamic dispatch and &lt;code&gt;type(of:)&lt;/code&gt; imply its existence.&lt;/p&gt;

&lt;p&gt;Third, there are the various reference counts. These are completely hidden unless you do naughty things like read the raw memory of your object or convince the compiler to let you call &lt;code&gt;CFGetRetainCount&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Fourth, you have auxiliary information stored by the Objective-C runtime, like the list of Objective-C weak references (the Objective-C implementation of weak references tracks each weak reference individually) and associated objects.&lt;/p&gt;

&lt;p&gt;Where do you store all of this stuff?&lt;/p&gt;

&lt;p&gt;In Objective-C, the class and stored properties (i.e. instance variables) are stored inline in the object's memory. The class takes up the first pointer-sized chunk, and the instance variables come after. Auxiliary information is stored in external tables. When you manipulate an associated object, the runtime looks it up in a big hash table which is keyed by the object's address. This is somewhat slow and requires locking so that multithreaded access doesn't fail. The reference count is sometimes stored in the object's memory and sometimes stored in an external table, depending on which OS version you're running and which CPU architecture.&lt;/p&gt;

&lt;p&gt;In Swift's old implementation, the class, reference counts, and stored properties were all stored inline. Auxiliary information was still stored in a separate table.&lt;/p&gt;

&lt;p&gt;Putting aside how these languages actually do it, let's ask the question: how &lt;em&gt;should&lt;/em&gt; they do it?&lt;/p&gt;

&lt;p&gt;Each location has tradeoffs. Data stored in the object's memory is fast to access but always takes up space. Data stored in an external table is slower to access but takes up zero space for objects which don't need it.&lt;/p&gt;

&lt;p&gt;This is at least part of why Objective-C traditionally didn't store the reference count in the object itself. Objective-C reference counting was created when computers were much less capable than they were now, and memory was extremely limited. Most objects in a typical Objective-C program have a single owner, and thus a reference count of 1. Reserving four bytes of the object's memory to store &lt;code&gt;1&lt;/code&gt; all the time would be wasteful. By using an external table, the common value of &lt;code&gt;1&lt;/code&gt; could be represented by the absence of an entry, reducing memory usage.&lt;/p&gt;

&lt;p&gt;Every object has a class, and it is constantly accessed. Every dynamic method call needs it. This should go directly in the object's memory. There's no savings from storing it externally.&lt;/p&gt;

&lt;p&gt;Stored properties are expected to be fast. Whether an object has them is determined at compile time. Objects with no stored properties can allocate zero space for them even when stored in the object's memory, so they should go there.&lt;/p&gt;

&lt;p&gt;Every object has reference counts. Not every object has reference counts that aren't &lt;code&gt;1&lt;/code&gt;, but it's still pretty common, and memory is a lot bigger these days. This should probably go in the object's memory.&lt;/p&gt;

&lt;p&gt;Most objects don't have any weak references or associated objects. Dedicating space within the object's memory for these would be wasteful. These should be stored externally.&lt;/p&gt;

&lt;p&gt;This is the right tradeoff, but it's annoying. For objects that have weak references and associated objects, they're pretty slow. How can we fix this?&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Side Tables&lt;/b&gt;&lt;br&gt;Swift's new implementation of weak references brings with it the concept of &lt;em&gt;side tables&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;A side table is a separate chunk of memory which stores extra information about an object. It's &lt;em&gt;optional&lt;/em&gt;, meaning that an object may have a side table, or it may not. Objects which need the functionality of a side table can incur the extra cost, and objects which don't need it don't pay for it.&lt;/p&gt;

&lt;p&gt;Each object has a pointer to its side table, and the side table has a pointer back to the object. The side table can then store other information, like associated object data.&lt;/p&gt;

&lt;p&gt;To avoid reserving eight bytes for the side table, Swift makes a nifty optimization. Initially, the first word of an object is the class, and the next word stores the reference counts. When an object needs a side table, that second word is repurposed to be a side table pointer instead. Since the object still needs reference counts, the reference counts are stored in the side table. The two cases are distinguished by setting a bit in this field that indicates whether it holds reference counts or a pointer to the side table.&lt;/p&gt;

&lt;p&gt;The side table allows Swift to maintain the basic form of the old weak reference system while fixing its flaws. Instead of pointing to the object, as it used to work, weak references now point directly at the side table.&lt;/p&gt;

&lt;p&gt;Because the side table is known to be small, there's no issue of wasting a lot of memory for weak references to large objects, so that problem goes away. This also points to a simple solution for the thread safety problem: don't preemptively zero out weak references. Since the side table is known to be small, weak references to it can be left alone until those references themselves are overwritten or destroyed.&lt;/p&gt;

&lt;p&gt;I should note that the current side table implementation only holds reference counts and a pointer to the original object. Additional uses like associated objects are currently hypothetical. Swift has no built-in associated object functionality, and the Objective-C API still uses a global table.&lt;/p&gt;

&lt;p&gt;The technique has a lot of potential, and we'll probably see something like associated objects using it before too long. I'm hopeful that this will open the door to stored properties in extensions class types and other nifty features.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Code&lt;/b&gt;&lt;br&gt;Since Swift is open source, all of the code for this stuff is accessible.&lt;/p&gt;

&lt;p&gt;Most of the side table stuff can be found in &lt;a href="https://github.com/apple/swift/blob/c262440e70896299118a0a050c8a834e1270b606/stdlib/public/SwiftShims/RefCount.h"&gt;stdlib/public/SwiftShims/RefCount.h&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The high-level weak reference API, along with juicy comments about the system, can be found in &lt;a href="https://github.com/apple/swift/blob/c262440e70896299118a0a050c8a834e1270b606/stdlib/public/runtime/WeakReference.h"&gt;swift/stdlib/public/runtime/WeakReference.h&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Some more implementation and comments about how heap-allocated objects work can be found in &lt;a href="https://github.com/apple/swift/blob/c262440e70896299118a0a050c8a834e1270b606/stdlib/public/runtime/HeapObject.cpp"&gt;stdlib/public/runtime/HeapObject.cpp&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I've linked to specific commits of these files, so that people reading from the far future can still see what I'm talking about. If you want to see the latest and greatest, be sure to switch over to the &lt;code&gt;master&lt;/code&gt; branch, or whatever is relevant to your interests, after you click the links.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;br&gt;Weak references are an important language feature. Swift's original implementation was wonderfully clever and had some nice properties, but also had some problems. By adding an optional side table, Swift's engineers were able to solve those problems while keeping the nice, clever properties of the original. The side table implementation also opens up a lot of possibilities for great new features in the future.&lt;/p&gt;

&lt;p&gt;That's it for today. Come back again for more crazy programming-related ghost stories. Until then, if you have a topic you'd like to see covered here, please &lt;a href="mailto:mike@mikeash.com"&gt;send it in&lt;/a&gt;!&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;
</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/friday-qa-2017-09-22-swift-4-weak-references.html</guid><pubDate>Sat, 23 Sep 2017 00:57:00 GMT</pubDate></item><item><title>The Best New Features in Swift 4
</title><link>http://www.mikeash.com/pyblog/the-best-new-features-in-swift-4.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;The Best New Features in Swift 4
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2017 09 15  14 43"
                  tags=""
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;The Best New Features in Swift 4
&lt;/div&gt;
              &lt;p&gt;I'm afraid I once again don't have a Friday Q&amp;amp;A for you today, but I wrote up &lt;a href="https://plausible.coop/blog/2017/09/13/best-new-features-in-swift-4"&gt;the best new features in Swift 4&lt;/a&gt; for the Plausible Labs blog, which is &lt;em&gt;almost&lt;/em&gt; as good. &lt;a href="https://plausible.coop/blog/2017/09/13/best-new-features-in-swift-4"&gt;Check it out over there!&lt;/a&gt;&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/the-best-new-features-in-swift-4.html</guid><pubDate>Fri, 15 Sep 2017 14:43:00 GMT</pubDate></item><item><title>Corporate Training, NYC Workshop, and Book Update
</title><link>http://www.mikeash.com/pyblog/corporate-training-nyc-workshop-and-book-update.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;Corporate Training, NYC Workshop, and Book Update
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2017 09 08  19 07"
                  tags=""
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;Corporate Training, NYC Workshop, and Book Update
&lt;/div&gt;
              &lt;p&gt;I'm afraid I ran out of time for Friday Q&amp;amp;A this week. Will shoot for next week instead. Instead, I present a little update about various other things in my world.&lt;/p&gt;

&lt;p&gt;Over at &lt;a href="https://plausible.coop"&gt;Plausible Labs&lt;/a&gt;, we're introducing a &lt;a href="https://plausible.coop/training/corporate"&gt;corporate training program&lt;/a&gt;, where companies can bring us (me, mostly) in to teach their programmers the same sort of wild and wonderful stuff I blog about. We're concentrating on Swift, since it's the hot new thing, but we can do any topic we know about if there's demand. If you work at a company that might be interested in having us, or just know of one, please get in touch. It's all the Friday Q&amp;amp;A goodness you love, concentrated, personalized, and made huggable.&lt;/p&gt;

&lt;p&gt;On a similar note, we have another Swift workshop coming up in New York City on Friday, September 29th. Come to this one and I'll teach you all about the deeper darker secrets of protocols, generics, introspection, pointers, and more. Check out &lt;a href="https://www.eventbrite.com/e/plausible-labs-swift-workshop-with-mike-ash-tickets-36936899260"&gt;the workshop event page&lt;/a&gt; for more info or to get tickets.&lt;/p&gt;

&lt;p&gt;Finally, the next edition of &lt;em&gt;The Complete Friday Q&amp;amp;A&lt;/em&gt; is coming very close to completion. I need to get a cover finished for it, and then there's some administrative mumbo jumbo for getting an ISBN and uploading it and such, and I need to write a few more little secret bonus pages, and then it should be ready to go. I will, of course, announce it here when it's available. Stay tuned!&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/corporate-training-nyc-workshop-and-book-update.html</guid><pubDate>Fri, 08 Sep 2017 19:07:00 GMT</pubDate></item><item><title>Friday Q&amp;A 2017-08-25: Swift Error Handling Implementation
</title><link>http://www.mikeash.com/pyblog/friday-qa-2017-08-25-swift-error-handling-implementation.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;Friday Q&amp;amp;A 2017-08-25: Swift Error Handling Implementation
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2017 08 25  13 13"
                  tags="fridayqna swift assembly"
                  translations="https://pilgwon.github.io/blog/2017/09/03/Swift-Error-Handling-Implementation.html Korean pilgwon"
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;Friday Q&amp;amp;A 2017-08-25: Swift Error Handling Implementation
&lt;/div&gt;
              &lt;p&gt;Swift's error handling is a unique feature of the language. It looks a lot like exceptions in other languages, but the syntax is not quite the same, and it doesn't quite work the same either. Today I'm going to take a look at how Swift errors work on the inside.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Semantics&lt;/b&gt;&lt;br&gt;Let's start with a quick refresher on how Swift errors work at the language level.&lt;/p&gt;

&lt;p&gt;Any Swift function can be decorated with a &lt;code&gt;throws&lt;/code&gt; keyword, which indicates that it can throw an error:&lt;/p&gt;

&lt;pre&gt;    func getStringMightFail() throws -&amp;gt; String { ...
&lt;/pre&gt;

&lt;p&gt;To actually throw an error from such a function, use the &lt;code&gt;throw&lt;/code&gt; keyword with a value that conforms to the &lt;code&gt;Error&lt;/code&gt; protocol:&lt;/p&gt;

&lt;pre&gt;        throw MyError.brainNotFound
&lt;/pre&gt;

&lt;p&gt;When calling a &lt;code&gt;throws&lt;/code&gt; function, you must include the &lt;code&gt;try&lt;/code&gt; keyword:&lt;/p&gt;

&lt;pre&gt;    let string = try getStringMightFail()
&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;try&lt;/code&gt; keyword doesn't do anything, but is a required marker to indicate that the function might throw an error. The call must be in a context where throwing an error is allowed, either in a &lt;code&gt;throws&lt;/code&gt; function, or in a &lt;code&gt;do&lt;/code&gt; block with a &lt;code&gt;catch&lt;/code&gt; handler.&lt;/p&gt;

&lt;p&gt;To write a &lt;code&gt;catch&lt;/code&gt; handler, place the &lt;code&gt;try&lt;/code&gt; call in a &lt;code&gt;do&lt;/code&gt; block, and add a &lt;code&gt;catch&lt;/code&gt; block:&lt;/p&gt;

&lt;pre&gt;    do {
        let string = try getStringMightFail()
        ...
    } catch {
        print("Got an error: \(error)")
    }
&lt;/pre&gt;

&lt;p&gt;When an error is thrown, execution jumps to the &lt;code&gt;catch&lt;/code&gt; block. The value that was thrown is available in &lt;code&gt;error&lt;/code&gt;. You can get fancy with type checking and conditions and multiple &lt;code&gt;catch&lt;/code&gt; clauses, but these are the basics. For more information about all the details, see the &lt;a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html"&gt;Error Handling&lt;/a&gt; section of &lt;a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/index.html"&gt;The Swift Programming Language&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That's what it does. How does it work?&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Implementation&lt;/b&gt;&lt;br&gt;To find out how it works, I wrote some dummy code with error handling that I could disassemble:&lt;/p&gt;

&lt;pre&gt;    struct MyError: Error {
        var x: Int
        var y: Int
        var z: Int
    }

    func Thrower(x: Int, y: Int, z: Int) throws -&amp;gt; Int {
        throw MyError(x: x, y: y, z: z)
    }

    func Catcher(f: (Int, Int, Int) throws -&amp;gt; Int) {
        do {
            let x = try f(1, 2, 3)
            print("Received \(x)")
        } catch {
            print("Caught \(error)")
        }
    }
&lt;/pre&gt;

&lt;p&gt;Of course, now that Swift is open source, I could just go look at the compiler code and see what it does. But that's no fun, and this is easier.&lt;/p&gt;

&lt;p&gt;It turns out that Swift 3 and Swift 4 do it differently. I'll briefly discuss Swift 3, then look a bit deeper at Swift 4, since that's up and coming.&lt;/p&gt;

&lt;p&gt;Swift 3 works by essentially automating Objective-C's &lt;code&gt;NSError&lt;/code&gt; convention. The compiler inserts an extra, hidden parameter which is essentially &lt;code&gt;Error *&lt;/code&gt;, or &lt;code&gt;NSError **&lt;/code&gt;. Throwing an error consists of writing the error object to the pointer passed in that parameter. The caller allocates some stack space and passes its address in that parameter. On return, it checks to see if that space now contains an error. If it does, it jumps to the &lt;code&gt;catch&lt;/code&gt; block.&lt;/p&gt;

&lt;p&gt;Swift 4 gets a little fancier. The basic idea is the same, but instead of a normal extra parameter, a special register is reserved for the error return. Here's what the relevant assembly code in &lt;code&gt;Thrower&lt;/code&gt; looks like:&lt;/p&gt;

&lt;pre&gt;    call       imp___stubs__swift_allocError
    mov        qword [rdx], rbx
    mov        qword [rdx+8], r15
    mov        qword [rdx+0x10], r14
    mov        r12, rax
&lt;/pre&gt;

&lt;p&gt;This calls into the Swift runtime to allocate a new error, fills it out with the relevant values, and then places the pointer into &lt;code&gt;r12&lt;/code&gt;. It then returns to the caller. The relevant code in &lt;code&gt;Catcher&lt;/code&gt; looks like this:&lt;/p&gt;

&lt;pre&gt;    call       r14
    mov        r15, rax
    test       r12, r12
    je         loc_100002cec
&lt;/pre&gt;

&lt;p&gt;It makes the call, then checks if &lt;code&gt;r12&lt;/code&gt; contains anything. If it does, it jumps to the &lt;code&gt;catch&lt;/code&gt; block. The technique on ARM64 is almost the same, with the &lt;code&gt;x21&lt;/code&gt; register serving as the error pointer.&lt;/p&gt;

&lt;p&gt;Internally, it looks a lot like returning a &lt;code&gt;Result&lt;/code&gt; type, or otherwise returning some sort of error code. The &lt;code&gt;throws&lt;/code&gt; function returns the thrown error to the caller in a special place. The caller checks that place for an error, and jumps to the error handling code if so. The generated code looks similar to Objective-C code using an &lt;code&gt;NSError **&lt;/code&gt; parameter, and in fact Swift 3's version of it is identical.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Comparison With Exceptions&lt;/b&gt;&lt;br&gt;Swift is careful never to use the word "exception" when discussing its error handling system, but it looks a lot like exceptions in other languages. How does its implementation compare? There are a lot of languages out there with exceptions, and many of them do things differently, but the natural comparison is C++. Objective-C exceptions (which do exist, although pretty much nobody uses them) use C++'s exceptions mechanism on the modern runtime.&lt;/p&gt;

&lt;p&gt;A full exploration of how C++ exceptions work could fill a book, so we'll have to settle for a brief description.&lt;/p&gt;

&lt;p&gt;C++ code that calls throwing functions (which is the default for C++ functions) produces assembly exactly as if it called non-throwing functions. Which is to say, it passes in parameters and retrieves return values and gives no thought to the possibility of exceptions.&lt;/p&gt;

&lt;p&gt;How can this possibly work? In addition to generating the no-exceptions code, the compiler also generates a table with information about how (and whether) the code handles exceptions and how to safely unwind the stack to exit out of the function in the event that an exception is thrown.&lt;/p&gt;

&lt;p&gt;When some function throws an exception, it walks up the stack, looking up each function's information and using that to unwind the stack to the next function, until it either finds an exception handler or runs off the end. If it finds an exception handler, it transfers control to that handler which then runs the code in the &lt;code&gt;catch&lt;/code&gt; block.&lt;/p&gt;

&lt;p&gt;For more information about how C++ exceptions work, see &lt;a href="https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html"&gt;C++ ABI for Itanium: Exception Handling&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This system is called "zero-cost" exception handling. The term "zero-cost" refers to what happens when no exceptions are ever thrown. Because that code is compiled exactly as it would be without exceptions, there's no runtime overhead for supporting exceptions. Calling potentially-throwing functions is just as fast as calling functions that don't throw, and adding &lt;code&gt;try&lt;/code&gt; blocks to your code doesn't result in any additional work done at runtime.&lt;/p&gt;

&lt;p&gt;When an exception &lt;em&gt;is&lt;/em&gt; thrown, the concept of "zero-cost" goes out the window. Unwinding the stack using the tables is an expensive process and takes a substantial amount of time. The system is designed around the idea that exceptions are thrown rarely, and performance in the case where no exceptions are ever thrown is more important. This assumption is likely to be true in almost all code.&lt;/p&gt;

&lt;p&gt;Compared to this, Swift's system is extremely simple. It makes no attempt to generate the same code for &lt;code&gt;throws&lt;/code&gt; and non-&lt;code&gt;throws&lt;/code&gt; functions. Instead, every call to a &lt;code&gt;throws&lt;/code&gt; function is followed by a check to see if an error was returned, and a jump to the appropriate error handling code if so. These checks aren't free, although they should be pretty cheap.&lt;/p&gt;

&lt;p&gt;The tradeoff makes a lot of sense for Swift. Swift errors look a lot like C++ exceptions, but in practice they're used differently. Nearly any C++ call can potentially throw, and even basic stuff like the &lt;code&gt;new&lt;/code&gt; operator will throw to indicate an error. Explicitly checking for a thrown exception after every call would add a lot of extra checks. In contrast, few Swift calls are marked &lt;code&gt;throws&lt;/code&gt; in typical codebases, so the cost of explicit checks is low.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;br&gt;Swift's error handling invites comparison with exceptions in other languages, such as C++. C++'s exception handling is extremely complicated internally, but Swift takes a different approach. Instead of unwind tables to achieve "zero-cost" in the common case, Swift returns thrown errors in a special register, and the caller checks that register to see if an error has been thrown. This adds a bit of overhead when errors aren't thrown, but avoids making things enormously complicated the way C++ does. It would take serious effort to write Swift code where the overhead from error handling makes any noticeable difference.&lt;/p&gt;

&lt;p&gt;That's it for today! Come back again for more excitement, fun, and horror. As I have occasionally mentioned before, Friday Q&amp;amp;A is driven by reader suggestions. As always, if you have a topic you'd like to see covered here, &lt;a href="mailto:mike@mikeash.com"&gt;send it in&lt;/a&gt;!&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;
</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/friday-qa-2017-08-25-swift-error-handling-implementation.html</guid><pubDate>Fri, 25 Aug 2017 13:13:00 GMT</pubDate></item><item><title>Friday Q&amp;A 2017-08-11: Swift.Unmanaged
</title><link>http://www.mikeash.com/pyblog/friday-qa-2017-08-11-swiftunmanaged.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;Friday Q&amp;amp;A 2017-08-11: Swift.Unmanaged
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2017 08 11  13 14"
                  tags="fridayqna swift c"
                  translations="https://www.autonvaraosatpro.fi/blogi/2018/02/26/2-12/ Estonian Johanne Teerink"
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;Friday Q&amp;amp;A 2017-08-11: Swift.Unmanaged
&lt;/div&gt;
              &lt;p&gt;In order to work with C APIs, we sometimes need to convert Swift object references to and from raw pointers. Swift's &lt;code&gt;Unmanaged&lt;/code&gt; &lt;code&gt;struct&lt;/code&gt; is the standard API for handling this. Today, I'd like to talk about what it does and how to use it.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Overview&lt;/b&gt;&lt;br&gt;Getting Swift references into and out of the world of C encompasses two separate tasks.&lt;/p&gt;

&lt;p&gt;The first task is converting a reference into the raw bytes for a &lt;code&gt;void *&lt;/code&gt;, or converting the raw bytes of a &lt;code&gt;void *&lt;/code&gt; back to a reference. Since Swift references are implemented as pointers, this is straightforward. It's really just a matter of getting the type system to cooperate. You can do this with &lt;code&gt;unsafeBitCast&lt;/code&gt;, although I strongly recommend against it. If these details ever changed, you'd be in trouble, whereas &lt;code&gt;Unmanaged&lt;/code&gt; will continue to work.&lt;/p&gt;

&lt;p&gt;The second task is making Swift's memory management work with a pointer that Swift can't see. Swift's ARC memory management requires the compiler to manage references to each object so that it can insert the appropriate retain and release calls. Once the pointer gets passed into C, it can no longer do this. Instead, you must manually tell the system how to handle memory management at each stage. &lt;code&gt;Unmanaged&lt;/code&gt; provides the facilities to do this.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Unmanaged&lt;/code&gt; wraps a reference to an object. It is possible to keep &lt;code&gt;Unmanaged&lt;/code&gt; values around long-term, but you typically use it as a brief, temporary stop on the way to or from a raw pointer.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;From Swift to C&lt;/b&gt;&lt;br&gt;To pass a reference from Swift to C, you must create an &lt;code&gt;Unmanaged&lt;/code&gt; value from that reference, then ask it for a raw pointer. There are two ways to create an &lt;code&gt;Unmanaged&lt;/code&gt; value from a Swift reference. To figure out which one you need, you must figure out your memory management requirements.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;Unmanaged.passRetained(obj)&lt;/code&gt; will perform an unbalanced retain on the object. This effectively confers ownership on whatever C API you're passing the pointer to. It must be balanced with a release at a later point, or the object will leak.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;Unmanaged.passUnretained(obj)&lt;/code&gt; will leave the object's retain count unchanged. This does not confer any ownership on the C API you pass the pointer to. This is suitable when passing to a C API which takes ownership internally (for example, passing an object into a &lt;code&gt;CFArray&lt;/code&gt; which will retain it) and when passing to a C API which uses the value immediately but doesn't hold onto it long-term. If you pass such a value to a C API that holds the pointer long-term and doesn't take ownership, your object may be deallocated prematurely resulting in a crash or worse.&lt;/p&gt;

&lt;p&gt;Once you have the &lt;code&gt;Unmanaged&lt;/code&gt; value, you can obtain a raw pointer from it using the &lt;code&gt;toOpaque&lt;/code&gt; method. Since you already decided how memory management needs to be handled, there's only one call here, and no decisions to make at this point.&lt;/p&gt;

&lt;p&gt;Here's an example of how you'd get a raw pointer with ownership:&lt;/p&gt;

&lt;pre&gt;    let ptr = Unmanaged.passRetained(obj).toOpaque()
    FunctionCall(ptr)
&lt;/pre&gt;

&lt;p&gt;And here's an example without ownership:&lt;/p&gt;

&lt;pre&gt;    let ptr = Unmanaged.passUnretained(obj).toOpaque()
    FunctionCall(ptr)
&lt;/pre&gt;

&lt;p&gt;In both cases, &lt;code&gt;ptr&lt;/code&gt; is an &lt;code&gt;UnsafeMutableRawPointer&lt;/code&gt; which is Swift's equivalent to C's &lt;code&gt;void *&lt;/code&gt;, and can be passed into C APIs.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;From C to Swift&lt;/b&gt;&lt;br&gt;To retrieve a reference from C into Swift code, you create an &lt;code&gt;Unmanaged&lt;/code&gt; value from the raw pointer, then take a reference from it. There are two ways to take a reference: one which consumes an unbalanced retain, and once which performs no memory management.&lt;/p&gt;

&lt;p&gt;To create the &lt;code&gt;Unmanaged&lt;/code&gt; value, use &lt;code&gt;fromOpaque&lt;/code&gt;. Unlike &lt;code&gt;passRetained&lt;/code&gt; and &lt;code&gt;passUnretained&lt;/code&gt;, you must specify the generic type for &lt;code&gt;Unmanaged&lt;/code&gt; when using &lt;code&gt;fromOpaque&lt;/code&gt; so that the compiler knows which class you're expecting to receive.&lt;/p&gt;

&lt;p&gt;Once you have the &lt;code&gt;Unmanaged&lt;/code&gt; value, you can get a reference out of it. Using &lt;code&gt;takeRetainedValue&lt;/code&gt; will perform an unbalanced release, which will balance an unbalanced retain previously performed with &lt;code&gt;passRetained&lt;/code&gt;, or by C code which confers ownership on your code. Using &lt;code&gt;takeUnretainedValue&lt;/code&gt; will obtain the object reference without performing any retain or release.&lt;/p&gt;

&lt;p&gt;Here's an example of getting a reference from a pointer while performing an unbalanced release:&lt;/p&gt;

&lt;pre&gt;    let obj = Unmanaged&amp;lt;MyClass&amp;gt;.fromOpaque(ptr).takeRetainedValue()
    obj.method()
&lt;/pre&gt;

&lt;p&gt;And without a retain or release:&lt;/p&gt;

&lt;pre&gt;    let obj = Unmanaged&amp;lt;MyClass&amp;gt;.fromOpaque(ptr).takeUnretainedValue()
    obj.method()
&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;Patterns&lt;/b&gt;&lt;br&gt;It's possible to use &lt;code&gt;Umanaged&lt;/code&gt; in a one-sided way, with C APIs which take a pointer and do stuff with it, or which return a pointer. But the most common way to use &lt;code&gt;Unmanaged&lt;/code&gt; is with C APIs which pass around a context pointer. In this situation, you control both sides, and you need to figure out your memory management to avoid crashing or leaking. How you do that will depend on what you're doing. There are a few fundamental patterns to use.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Synchronous Callback&lt;/b&gt;&lt;br&gt;Some APIs call you back immediately with your context pointer, completing all of their work before they return. In this case, you can pass the object unretained, and take it unretained. For example, here's some code that calls &lt;code&gt;CFArrayApplyFunction&lt;/code&gt; and calls a method on &lt;code&gt;self&lt;/code&gt; for each element in the array:&lt;/p&gt;

&lt;pre&gt;    let context = Unmanaged.passUnretained(self).toOpaque()
    let range = CFRangeMake(0, CFArrayGetCount(array))
    CFArrayApplyFunction(array, range, { element, context in
        let innerSelf = Unmanaged&amp;lt;MyClass&amp;gt;.fromOpaque(context!).takeUnretainedValue()
        innerSelf.method(element)
    }, context)
&lt;/pre&gt;

&lt;p&gt;Because the function runs immediately, and we know that &lt;code&gt;self&lt;/code&gt; will stay alive for the duration, we don't need to do any memory management on it.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Asynchronous One-Shot Callback&lt;/b&gt;&lt;br&gt;Some APIs perform a single callback later on, for example to inform you that some task has completed. In this case, you want to retain going in, and release in the callback. Here's an example using &lt;code&gt;CFHost&lt;/code&gt; to perform asynchronous DNS resolution. As a bonus, this code also contains a one-sided use of &lt;code&gt;Unmanaged&lt;/code&gt; to retrieve the return value of &lt;code&gt;CFHostCreateWithName&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It starts by creating the host. &lt;code&gt;CFHostCreateWithName&lt;/code&gt; returns an &lt;code&gt;Unmanaged&lt;/code&gt;, presumably because it hasn't had any curated bridging done to it. Since it returns a value that we own, we use &lt;code&gt;takeRetainedValue()&lt;/code&gt; on it to get the underlying value out:&lt;/p&gt;

&lt;pre&gt;    let host = CFHostCreateWithName(nil, "mikeash.com" as CFString).takeRetainedValue()
&lt;/pre&gt;

&lt;p&gt;We need a context to pass to the host object. We'll ignore all fields of this context except &lt;code&gt;info&lt;/code&gt;, which is the raw pointer where we'll put the pointer to &lt;code&gt;self&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;    var context = CFHostClientContext()
    context.info = Unmanaged.passRetained(self).toOpaque()
&lt;/pre&gt;

&lt;p&gt;We'll set the callback using &lt;code&gt;CFHostSetClient&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;    CFHostSetClient(host, { host, typeInfo, error, info in
&lt;/pre&gt;

&lt;p&gt;Here, we retrieve &lt;code&gt;self&lt;/code&gt; passed through &lt;code&gt;info&lt;/code&gt; by using &lt;code&gt;Unmanaged&lt;/code&gt; again. Since we passed it retained, we use &lt;code&gt;takeRetainedValue&lt;/code&gt; here to balance it out. We can then use the resulting reference.&lt;/p&gt;

&lt;pre&gt;        let innerSelf = Unmanaged&amp;lt;MyClass&amp;gt;.fromOpaque(info!).takeRetainedValue()
        innerSelf.resolved(host)
    }, &amp;amp;context)
&lt;/pre&gt;

&lt;p&gt;This code starts the asynchronous resolution process:&lt;/p&gt;

&lt;pre&gt;    CFHostScheduleWithRunLoop(host, CFRunLoopGetCurrent(), CFRunLoopMode.commonModes.rawValue)
    CFHostStartInfoResolution(host, .addresses, nil)
&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;Asynchronous Multi-Shot Callback&lt;/b&gt;&lt;br&gt;Finally, some APIs take a callback which they invoke many times later on. To handle this, you need to retain going in to ensure that the object stays alive. You must &lt;em&gt;not&lt;/em&gt; release in the callback, since the first callback would destroy the object and leave subsequent callbacks with a pointer to junk. Instead, you must release the value later on, when all of the callbacks are done. Typically the API will provide a separate destruction callback to handle this.&lt;/p&gt;

&lt;p&gt;Here's an example using &lt;code&gt;CFRunLoopTimer&lt;/code&gt; to perform a task once per second. It starts by creating a context and filling out the &lt;code&gt;info&lt;/code&gt; field like above:&lt;/p&gt;

&lt;pre&gt;    var context = CFRunLoopTimerContext()
    context.info = Unmanaged.passRetained(self).toOpaque()
&lt;/pre&gt;

&lt;p&gt;This example also fills out the context's &lt;code&gt;release&lt;/code&gt; field. In the &lt;code&gt;release&lt;/code&gt; callback, it uses &lt;code&gt;fromOpaque&lt;/code&gt; to get an &lt;code&gt;Unmanaged&lt;/code&gt; for the &lt;code&gt;info&lt;/code&gt; pointer, and then calls &lt;code&gt;release&lt;/code&gt; to balance the retain:&lt;/p&gt;

&lt;pre&gt;    context.release = { Unmanaged&amp;lt;MyClass&amp;gt;.fromOpaque($0!).release() }
&lt;/pre&gt;

&lt;p&gt;It then creates the timer and passes a callback:&lt;/p&gt;

&lt;pre&gt;    let timer = CFRunLoopTimerCreate(nil, 0, 1, 0, 0, { timer, info in
&lt;/pre&gt;

&lt;p&gt;It retrieves &lt;code&gt;self&lt;/code&gt; using &lt;code&gt;Unmanaged&lt;/code&gt;. It uses &lt;code&gt;takeUnretainedValue&lt;/code&gt; here, since we don't want to modify the object's retain count:&lt;/p&gt;

&lt;pre&gt;        let innerSelf = Unmanaged&amp;lt;MyClass&amp;gt;.fromOpaque(info!).takeUnretainedValue()
        innerSelf.doStuff()
    }, &amp;amp;context)
&lt;/pre&gt;

&lt;p&gt;Finally, it adds the timer to the runloop so the timer will fire:&lt;/p&gt;

&lt;pre&gt;    CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, CFRunLoopMode.commonModes)
&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;Miscellaneous Functionality&lt;/b&gt;&lt;br&gt;Most of what you need from &lt;code&gt;Unmanaged&lt;/code&gt; can be done using &lt;code&gt;fromOpaque&lt;/code&gt;, &lt;code&gt;passRetained&lt;/code&gt;, &lt;code&gt;passUnretained&lt;/code&gt;, &lt;code&gt;takeRetainedValue&lt;/code&gt;, &lt;code&gt;takeUnretainedValue&lt;/code&gt;, and &lt;code&gt;toOpaque&lt;/code&gt;. However, &lt;code&gt;Unmanaged&lt;/code&gt; also exposes methods for performing memory management directly on objects without the window dressing of passing or taking values. We saw that briefly in the previous example with the call to &lt;code&gt;release&lt;/code&gt;. &lt;code&gt;Unmanaged&lt;/code&gt; provides three plain memory management calls:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;retain&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;release&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;autorelease&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These all do exactly what their names indicate. It's rare to need these, aside from &lt;code&gt;release&lt;/code&gt; to balance retains as shown above, but they're there if you do.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;br&gt;One of Swift's great strengths is the smoothness with which it interoperates with C code. The &lt;code&gt;Unmanaged&lt;/code&gt; API is a key component of that. Unfortunately, it requires the programmer to make memory management decisions as pointers move in and out of Swift, but that's an inherent part of the job it does. Once you have the memory management figured out, it's mostly straightforward to use.&lt;/p&gt;

&lt;p&gt;That wraps things up for now. Come back soon for more programming-related mystery goo. Until then, Friday Q&amp;amp;A is driven by reader ideas, so if you have a topic you'd like to see covered, please &lt;a href="mailto:mike@mikeash.com"&gt;send it in&lt;/a&gt;!&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;
</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/friday-qa-2017-08-11-swiftunmanaged.html</guid><pubDate>Fri, 11 Aug 2017 13:14:00 GMT</pubDate></item><item><title>Friday Q&amp;A 2017-07-28: A Binary Coder for Swift
</title><link>http://www.mikeash.com/pyblog/friday-qa-2017-07-28-a-binary-coder-for-swift.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;Friday Q&amp;amp;A 2017-07-28: A Binary Coder for Swift
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2017 07 28  12 44"
                  tags="fridayqna swift serialization"
                  translations="http://www.forallworld.com/binaris-coder-valo-swift/ Hungarian Zsolt Boros"
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;Friday Q&amp;amp;A 2017-07-28: A Binary Coder for Swift
&lt;/div&gt;
              &lt;p&gt;In &lt;a href="friday-qa-2017-07-14-swiftcodable.html"&gt;my last article&lt;/a&gt; I discussed the basics of Swift's new &lt;code&gt;Codable&lt;/code&gt; protocol, briefly discussed how to implement your own encoder and decoder, and promised another article about a custom binary coder I've been working on. Today, I'm going to present that binary coder.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Source Code&lt;/b&gt;&lt;br&gt;As usual, the source code is available on GitHub:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mikeash/BinaryCoder/tree/887cecd70c070d86f338065f59ed027c13952c83"&gt;https://github.com/mikeash/BinaryCoder/tree/887cecd70c070d86f338065f59ed027c13952c83&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Concept and Approach&lt;/b&gt;&lt;br&gt;This coder serializes fields by writing them out sequentially as raw bytes, with no metadata. For example:&lt;/p&gt;

&lt;pre&gt;    struct S {
        var a: Int16
        var b: Int32
        var c: Int64
    }
&lt;/pre&gt;

&lt;p&gt;The result of encoding an instance of &lt;code&gt;S&lt;/code&gt; is fourteen bytes long, with two bytes for &lt;code&gt;a&lt;/code&gt;, four bytes for &lt;code&gt;b&lt;/code&gt;, and eight bytes for &lt;code&gt;c&lt;/code&gt;. The result is &lt;em&gt;almost&lt;/em&gt; the same as writing out the raw underlying memory of &lt;code&gt;S&lt;/code&gt;, except there's no padding, the numbers are byte-swapped to be endian agnostic, and it's able to intelligently chase down references and do custom encoding when needed.&lt;/p&gt;

&lt;p&gt;This type of straightforward binary encoding is a little hobby of mine, and I've previously experimented with other approaches to it in Swift, none of which were satisfactory. When the Swift 4 beta became available with &lt;code&gt;Codable&lt;/code&gt;, I looked to see if it would work for this, and it did!&lt;/p&gt;

&lt;p&gt;My use of &lt;code&gt;Codable&lt;/code&gt; is somewhat abusive. I want to take advantage of the compiler-generated &lt;code&gt;Encodable&lt;/code&gt; and &lt;code&gt;Decodable&lt;/code&gt; implementations, but those use keyed coding, whereas the straight-line no-metadata binary format is pretty much the polar opposite of keyed coding. The solution is simple: ignore the keys, and rely on the encoding and decoding order to be consistent. This is ugly, and a bad idea in general, but it does work, and even got &lt;a href="https://twitter.com/jckarter/status/874079604612505601"&gt;a tweet from a member of the Swift core team&lt;/a&gt; indicating it might be OK. This approach is obviously &lt;em&gt;not&lt;/em&gt; resilient to changes in your field layout or field types, but as long as you're aware of this and understand it, that's acceptable.&lt;/p&gt;

&lt;p&gt;It does mean that arbitrary implementations of &lt;code&gt;Codable&lt;/code&gt; can't be trusted to work with this coder. We know that the compiler-generated implementations work, with limitations, but there may be implementations in the standard library (for example, the implementation for &lt;code&gt;Array&lt;/code&gt;) which rely on semantics that this coder doesn't support. In order to ensure that types don't partipate in binary coding without some vetting, I created my own protocols for binary coding:&lt;/p&gt;

&lt;pre&gt;    public protocol BinaryEncodable: Encodable {
        func binaryEncode(to encoder: BinaryEncoder) throws
    }

    public protocol BinaryDecodable: Decodable {
        init(fromBinary decoder: BinaryDecoder) throws
    }

    public typealias BinaryCodable = BinaryEncodable &amp;amp; BinaryDecodable
&lt;/pre&gt;

&lt;p&gt;I wrote extensions to simplify the common case where you just want to use the compiler's implementation of &lt;code&gt;Codable&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;    public extension BinaryEncodable {
        func binaryEncode(to encoder: BinaryEncoder) throws {
            try self.encode(to: encoder)
        }
    }

    public extension BinaryDecodable {
        public init(fromBinary decoder: BinaryDecoder) throws {
            try self.init(from: decoder)
        }
    }
&lt;/pre&gt;

&lt;p&gt;This way, your own types can just conform to &lt;code&gt;BinaryCodable&lt;/code&gt;, and they'll get a default implementation of everything they need, as long as they meet the requirements. It's required that all fields must be &lt;code&gt;Codable&lt;/code&gt;, but we can't require all fields to be &lt;code&gt;BinaryCodable&lt;/code&gt;. That type checking has to be done at runtime, which is unfortunate, but acceptable.&lt;/p&gt;

&lt;p&gt;The encoder and decoder implementation are straightforward: they encode/decode everything in order, ignoring the keys. The encoder produces bytes corresponding to the values that are encoded, and the decoder produces values from the bytes it has stored.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;code&gt;BinaryEncoder&lt;/code&gt; Basics&lt;/b&gt;&lt;br&gt;The encoder is a public class:&lt;/p&gt;

&lt;pre&gt;    public class BinaryEncoder {
&lt;/pre&gt;

&lt;p&gt;It has one field, which is the data it has encoded so far:&lt;/p&gt;

&lt;pre&gt;    fileprivate var data: [UInt8] = []
&lt;/pre&gt;

&lt;p&gt;This data starts out empty, and bytes are appended to it as values are encoded.&lt;/p&gt;

&lt;p&gt;A convenience method wraps up the process of creating an encoder instance, encoding an object into it, and returning the instance's data:&lt;/p&gt;

&lt;pre&gt;    static func encode(_ value: BinaryEncodable) throws -&amp;gt; [UInt8] {
        let encoder = BinaryEncoder()
        try value.binaryEncode(to: encoder)
        return encoder.data
    }
&lt;/pre&gt;

&lt;p&gt;The encoding process can throw runtime errors, so the encoder needs an error type:&lt;/p&gt;

&lt;pre&gt;    enum Error: Swift.Error {
        case typeNotConformingToBinaryEncodable(Encodable.Type)
        case typeNotConformingToEncodable(Any.Type)
    }
&lt;/pre&gt;

&lt;p&gt;Let's move on to the low-level encoding methods. We'll start with a generic method which will encode the raw bytes of a value:&lt;/p&gt;

&lt;pre&gt;    func appendBytes&amp;lt;T&amp;gt;(of: T) {
        var target = of
        withUnsafeBytes(of: &amp;amp;target) {
            data.append(contentsOf: $0)
        }
    }
&lt;/pre&gt;

&lt;p&gt;This will form the basis for other encoding methods.&lt;/p&gt;

&lt;p&gt;Let's take a quick look at the methods for encoding &lt;code&gt;Float&lt;/code&gt; and &lt;code&gt;Double&lt;/code&gt; next. CoreFoundation has helper functions which take care of any byte swapping that's needed for them, so these methods call those functions and then call &lt;code&gt;appendBytes&lt;/code&gt; with the result:&lt;/p&gt;

&lt;pre&gt;    func encode(_ value: Float) {
        appendBytes(of: CFConvertFloatHostToSwapped(value))
    }

    func encode(_ value: Double) {
        appendBytes(of: CFConvertDoubleHostToSwapped(value))
    }
&lt;/pre&gt;

&lt;p&gt;While we're at it, here's the method for encoding a &lt;code&gt;Bool&lt;/code&gt;. It translates the &lt;code&gt;Bool&lt;/code&gt; to a &lt;code&gt;UInt8&lt;/code&gt; containing a &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;1&lt;/code&gt; and encodes that:&lt;/p&gt;

&lt;pre&gt;    func encode(_ value: Bool) throws {
        try encode(value ? 1 as UInt8 : 0 as UInt8)
    }
&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;BinaryEncoder&lt;/code&gt; has one more &lt;code&gt;encode&lt;/code&gt; method, which takes care of encoding all other &lt;code&gt;Encodable&lt;/code&gt; types:&lt;/p&gt;

&lt;pre&gt;    func encode(_ encodable: Encodable) throws {
&lt;/pre&gt;

&lt;p&gt;This has special cases for various types, so it switches on the parameter:&lt;/p&gt;

&lt;pre&gt;        switch encodable {
&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;Int&lt;/code&gt; and &lt;code&gt;UInt&lt;/code&gt; need special handling, because their sizes aren't consistent. Depending on the target platform, they may be 32 bits or 64 bits. To solve this, we convert them to &lt;code&gt;Int64&lt;/code&gt; or &lt;code&gt;UInt64&lt;/code&gt; and then encode that value:&lt;/p&gt;

&lt;pre&gt;        case let v as Int:
            try encode(Int64(v))
        case let v as UInt:
            try encode(UInt64(v))
&lt;/pre&gt;

&lt;p&gt;All other integer types are handled with the &lt;code&gt;FixedWidthInteger&lt;/code&gt; protocol, which exposes enough functionality to do the necessary byte swapping for encoding values. Because &lt;code&gt;FixedWidthInteger&lt;/code&gt; uses &lt;code&gt;Self&lt;/code&gt; for some return types, I wasn't able to do the work directly here. Instead, I extended &lt;code&gt;FixedWidthInteger&lt;/code&gt; with a &lt;code&gt;binaryEncode&lt;/code&gt; method that handles the work:&lt;/p&gt;

&lt;pre&gt;        case let v as FixedWidthInteger:
            v.binaryEncode(to: self)
&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;Float&lt;/code&gt;, &lt;code&gt;Double&lt;/code&gt;, and &lt;code&gt;Bool&lt;/code&gt; call the type-specific methods above:&lt;/p&gt;

&lt;pre&gt;        case let v as Float:
            encode(v)
        case let v as Double:
            encode(v)
        case let v as Bool:
            try encode(v)
&lt;/pre&gt;

&lt;p&gt;Anything that's &lt;code&gt;BinaryEncodable&lt;/code&gt; is encoded by calling its &lt;code&gt;binaryEncode&lt;/code&gt; method and passing &lt;code&gt;self&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;        case let binary as BinaryEncodable:
            try binary.binaryEncode(to: self)
&lt;/pre&gt;

&lt;p&gt;There's one more case to handle. Any value that gets this far is not a type that we know how to encode natively, nor is it &lt;code&gt;BinaryEncodable&lt;/code&gt;. In this case, we throw an error to inform the caller that this value doesn't conform to the protocol:&lt;/p&gt;

&lt;pre&gt;        default:
            throw Error.typeNotConformingToBinaryEncodable(type(of: encodable))
        }
    }
&lt;/pre&gt;

&lt;p&gt;Finally, let's look at the &lt;code&gt;FixedWidthInteger&lt;/code&gt; extension. All this has to do is call &lt;code&gt;self.bigEndian&lt;/code&gt; to get a portable representation of the integer type, and then call &lt;code&gt;appendBytes&lt;/code&gt; on the encoder to encode that representation:&lt;/p&gt;

&lt;pre&gt;    private extension FixedWidthInteger {
        func binaryEncode(to encoder: BinaryEncoder) {
            encoder.appendBytes(of: self.bigEndian)
        }
    }
&lt;/pre&gt;

&lt;p&gt;We now have all the important parts of binary encoding, but we still don't have an &lt;code&gt;Encoder&lt;/code&gt; implementation. To accomplish that, we'll create implementations of the container protocols which call back to the &lt;code&gt;BinaryEncoder&lt;/code&gt; to do the work.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;code&gt;BinaryEncoder&lt;/code&gt; &lt;code&gt;Encoder&lt;/code&gt; Implementation&lt;/b&gt;&lt;br&gt;Let's start by looking at the implementations of the containers. We'll start with the &lt;code&gt;KeyedEncodingContainerProtocol&lt;/code&gt; implementation:&lt;/p&gt;

&lt;pre&gt;    private struct KeyedContainer&amp;lt;Key: CodingKey&amp;gt;: KeyedEncodingContainerProtocol {
&lt;/pre&gt;

&lt;p&gt;The implementation needs a reference to the binary encoder that it's working in:&lt;/p&gt;

&lt;pre&gt;        var encoder: BinaryEncoder
&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;Encoder&lt;/code&gt; requires a &lt;code&gt;codingPath&lt;/code&gt; property which returns an array of &lt;code&gt;CodingKey&lt;/code&gt; values indicating the current path into the encoder. Since this encoder doesn't really support keys in the first place, we always return an empty array:&lt;/p&gt;

&lt;pre&gt;        public var codingPath: [CodingKey] { return [] }
&lt;/pre&gt;

&lt;p&gt;Code which uses this class will have to be implemented not to require this value to make any sense.&lt;/p&gt;

&lt;p&gt;The protocol then has a ton of methods for encoding all of the various types that it supports:&lt;/p&gt;

&lt;pre&gt;    public mutating func encode(_ value: Bool, forKey key: Self.Key) throws
    public mutating func encode(_ value: Int, forKey key: Self.Key) throws
    public mutating func encode(_ value: Int8, forKey key: Self.Key) throws
    public mutating func encode(_ value: Int16, forKey key: Self.Key) throws
    public mutating func encode(_ value: Int32, forKey key: Self.Key) throws
    public mutating func encode(_ value: Int64, forKey key: Self.Key) throws
    public mutating func encode(_ value: UInt, forKey key: Self.Key) throws
    public mutating func encode(_ value: UInt8, forKey key: Self.Key) throws
    public mutating func encode(_ value: UInt16, forKey key: Self.Key) throws
    public mutating func encode(_ value: UInt32, forKey key: Self.Key) throws
    public mutating func encode(_ value: UInt64, forKey key: Self.Key) throws
    public mutating func encode(_ value: Float, forKey key: Self.Key) throws
    public mutating func encode(_ value: Double, forKey key: Self.Key) throws
    public mutating func encode(_ value: String, forKey key: Self.Key) throws
    public mutating func encode&amp;lt;T&amp;gt;(_ value: T, forKey key: Self.Key) throws where T : Encodable
&lt;/pre&gt;

&lt;p&gt;We'll have to implement all of those one by one. Let's start with the last one, which handles generic &lt;code&gt;Encodable&lt;/code&gt; values. It just needs to call through to &lt;code&gt;BinaryEncoder&lt;/code&gt;'s &lt;code&gt;encode&lt;/code&gt; method:&lt;/p&gt;

&lt;pre&gt;        func encode&amp;lt;T&amp;gt;(_ value: T, forKey key: Key) throws where T : Encodable {
            try encoder.encode(value)
        }
&lt;/pre&gt;

&lt;p&gt;We can use a similar technique to implement the other methods, and... what's this? All of the compiler errors about protocol conformance have gone away?&lt;/p&gt;

&lt;p&gt;It turns out that this one implementation of &lt;code&gt;encode&lt;/code&gt; satisfies &lt;em&gt;all&lt;/em&gt; of the &lt;code&gt;encode&lt;/code&gt; methods in the protocol, because all of the other types are &lt;code&gt;Encodable&lt;/code&gt;. A suitable generic method will fulfill any matching protocol requirements. It's obvious in retrospect, but I didn't realize it until I was halfway done with this code and saw that errors didn't appear when I deleted type-specific methods.&lt;/p&gt;

&lt;p&gt;Now we can see why I implemented &lt;code&gt;BinaryEncoder&lt;/code&gt;'s &lt;code&gt;encode&lt;/code&gt; method with a big &lt;code&gt;switch&lt;/code&gt; statement instead of using separate implementations for all of the various supported types. Overloaded methods are resolved at compile time based on the static type that's available at the call site. The above call to &lt;code&gt;encoder.encode(value)&lt;/code&gt; will always call &lt;code&gt;func encode(_ encodable: Encodable)&lt;/code&gt; even if the actual value passed in is, say, a &lt;code&gt;Double&lt;/code&gt; or a &lt;code&gt;Bool&lt;/code&gt;. In order to allow for this simple wrapper, the implementation in &lt;code&gt;BinaryEncoder&lt;/code&gt; has to work with a single entry point, which means it needs to be a big &lt;code&gt;switch&lt;/code&gt; statement.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;KeyedEncodingContainerProtocol&lt;/code&gt; requires a few other methods. There's one for encoding nil, which we implement to do nothing:&lt;/p&gt;

&lt;pre&gt;        func encodeNil(forKey key: Key) throws {}
&lt;/pre&gt;

&lt;p&gt;Then there are four methods for returning nested containers or superclass encoders. We don't do anything clever here, so this just delegates back to the encoder:&lt;/p&gt;

&lt;pre&gt;        func nestedContainer&amp;lt;NestedKey&amp;gt;(keyedBy keyType: NestedKey.Type, forKey key: Key) -&amp;gt; KeyedEncodingContainer&amp;lt;NestedKey&amp;gt; where NestedKey : CodingKey {
            return encoder.container(keyedBy: keyType)
        }

        func nestedUnkeyedContainer(forKey key: Key) -&amp;gt; UnkeyedEncodingContainer {
            return encoder.unkeyedContainer()
        }

        func superEncoder() -&amp;gt; Encoder {
            return encoder
        }

        func superEncoder(forKey key: Key) -&amp;gt; Encoder {
            return encoder
        }
    }
&lt;/pre&gt;

&lt;p&gt;We also need implementations of &lt;code&gt;UnkeyedEncodingContainer&lt;/code&gt; and &lt;code&gt;SingleValueEncodingContainer&lt;/code&gt;. It turns out that those protocols are similar enough that we can use a single implementation for both. The actual implementation is almost the same as it was for &lt;code&gt;KeyedEncodingContainerProtocol&lt;/code&gt;, with the addition of a dummy &lt;code&gt;count&lt;/code&gt; property:&lt;/p&gt;

&lt;pre&gt;    private struct UnkeyedContanier: UnkeyedEncodingContainer, SingleValueEncodingContainer {
        var encoder: BinaryEncoder

        var codingPath: [CodingKey] { return [] }

        var count: Int { return 0 }

        func nestedContainer&amp;lt;NestedKey&amp;gt;(keyedBy keyType: NestedKey.Type) -&amp;gt; KeyedEncodingContainer&amp;lt;NestedKey&amp;gt; where NestedKey : CodingKey {
            return encoder.container(keyedBy: keyType)
        }

        func nestedUnkeyedContainer() -&amp;gt; UnkeyedEncodingContainer {
            return self
        }

        func superEncoder() -&amp;gt; Encoder {
            return encoder
        }

        func encodeNil() throws {}

        func encode&amp;lt;T&amp;gt;(_ value: T) throws where T : Encodable {
            try encoder.encode(value)
        }
    }
&lt;/pre&gt;

&lt;p&gt;Using these containers, we'll make &lt;code&gt;BinaryEncoder&lt;/code&gt; conform to &lt;code&gt;Encoder&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Encoder&lt;/code&gt; requires a &lt;code&gt;codingPath&lt;/code&gt; property like the containers do:&lt;/p&gt;

&lt;pre&gt;    public var codingPath: [CodingKey] { return [] }
&lt;/pre&gt;

&lt;p&gt;It also requires a &lt;code&gt;userInfo&lt;/code&gt; property. We don't support that either, so it returns an empty dictionary:&lt;/p&gt;

&lt;pre&gt;    public var userInfo: [CodingUserInfoKey : Any] { return [:] }
&lt;/pre&gt;

&lt;p&gt;Then there are three methods which return containers:&lt;/p&gt;

&lt;pre&gt;    public func container&amp;lt;Key&amp;gt;(keyedBy type: Key.Type) -&amp;gt; KeyedEncodingContainer&amp;lt;Key&amp;gt; where Key : CodingKey {
        return KeyedEncodingContainer(KeyedContainer&amp;lt;Key&amp;gt;(encoder: self))
    }

    public func unkeyedContainer() -&amp;gt; UnkeyedEncodingContainer {
        return UnkeyedContanier(encoder: self)
    }

    public func singleValueContainer() -&amp;gt; SingleValueEncodingContainer {
        return UnkeyedContanier(encoder: self)
    }
&lt;/pre&gt;

&lt;p&gt;That's the end of &lt;code&gt;BinaryEncoder&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;code&gt;BinaryDecoder&lt;/code&gt; Basics&lt;/b&gt;&lt;br&gt;The decoder is a public class too:&lt;/p&gt;

&lt;pre&gt;    public class BinaryDecoder {
&lt;/pre&gt;

&lt;p&gt;Like the encoder, it has some data:&lt;/p&gt;

&lt;pre&gt;    fileprivate let data: [UInt8]
&lt;/pre&gt;

&lt;p&gt;Unlike the encoder, the decoder's data is loaded into the object when it's created. The caller provides the data that the decoder will decode from:&lt;/p&gt;

&lt;pre&gt;    public init(data: [UInt8]) {
        self.data = data
    }
&lt;/pre&gt;

&lt;p&gt;The decoder also needs to keep track of where it is inside the data it's decoding. It does that with a &lt;code&gt;cursor&lt;/code&gt; property, which starts out at the beginning of the data:&lt;/p&gt;

&lt;pre&gt;    fileprivate var cursor = 0
&lt;/pre&gt;

&lt;p&gt;A convenience method wraps up the process of creating a decoder and decoding a value:&lt;/p&gt;

&lt;pre&gt;    static func decode&amp;lt;T: BinaryDecodable&amp;gt;(_ type: T.Type, data: [UInt8]) throws -&amp;gt; T {
        return try BinaryDecoder(data: data).decode(T.self)
    }
&lt;/pre&gt;

&lt;p&gt;The decoder has its own errors it can throw during the decoding process. Decoding can fail in many more ways than encoding, so &lt;code&gt;BinaryDecoder&lt;/code&gt;'s &lt;code&gt;Error&lt;/code&gt; type has a lot more cases:&lt;/p&gt;

&lt;pre&gt;    enum Error: Swift.Error {
        case prematureEndOfData
        case typeNotConformingToBinaryDecodable(Decodable.Type)
        case typeNotConformingToDecodable(Any.Type)
        case intOutOfRange(Int64)
        case uintOutOfRange(UInt64)
        case boolOutOfRange(UInt8)
        case invalidUTF8([UInt8])
    }
&lt;/pre&gt;

&lt;p&gt;Now we can get on to actual decoding. The lowest level method reads a certain number of bytes out of &lt;code&gt;data&lt;/code&gt; into a pointer, advancing &lt;code&gt;cursor&lt;/code&gt;, or throwing &lt;code&gt;prematureEndOfData&lt;/code&gt; if &lt;code&gt;data&lt;/code&gt; doesn't have enough bytes in it:&lt;/p&gt;

&lt;pre&gt;    func read(_ byteCount: Int, into: UnsafeMutableRawPointer) throws {
        if cursor + byteCount &amp;gt; data.count {
            throw Error.prematureEndOfData
        }

        data.withUnsafeBytes({
            let from = $0.baseAddress! + cursor
            memcpy(into, from, byteCount)
        })

        cursor += byteCount
    }
&lt;/pre&gt;

&lt;p&gt;There's also a small generic wrapper which takes an &lt;code&gt;inout T&lt;/code&gt; and reads into that value, using &lt;code&gt;MemoryLayout&lt;/code&gt; to figure out how many bytes to read.&lt;/p&gt;

&lt;pre&gt;    func read&amp;lt;T&amp;gt;(into: inout T) throws {
        try read(MemoryLayout&amp;lt;T&amp;gt;.size, into: &amp;amp;into)
    }
&lt;/pre&gt;

&lt;p&gt;Like &lt;code&gt;BinaryEncoder&lt;/code&gt;, &lt;code&gt;BinaryDecoder&lt;/code&gt; has methods for decoding floating-point types. For these, it creates an empty &lt;code&gt;CFSwappedFloat&lt;/code&gt; value, reads into it, and then calls the appropriate CF function to convert it to the floating-point type in question:&lt;/p&gt;

&lt;pre&gt;    func decode(_ type: Float.Type) throws -&amp;gt; Float {
        var swapped = CFSwappedFloat32()
        try read(into: &amp;amp;swapped)
        return CFConvertFloatSwappedToHost(swapped)
    }

    func decode(_ type: Double.Type) throws -&amp;gt; Double {
        var swapped = CFSwappedFloat64()
        try read(into: &amp;amp;swapped)
        return CFConvertDoubleSwappedToHost(swapped)
    }
&lt;/pre&gt;

&lt;p&gt;The method for decoding &lt;code&gt;Bool&lt;/code&gt; decodes a &lt;code&gt;UInt8&lt;/code&gt; and then returns false if it's &lt;code&gt;0&lt;/code&gt;, true if it's &lt;code&gt;1&lt;/code&gt;, and otherwise throws an error:&lt;/p&gt;

&lt;pre&gt;    func decode(_ type: Bool.Type) throws -&amp;gt; Bool {
        switch try decode(UInt8.self) {
        case 0: return false
        case 1: return true
        case let x: throw Error.boolOutOfRange(x)
        }
    }
&lt;/pre&gt;

&lt;p&gt;The general &lt;code&gt;decode&lt;/code&gt; method for &lt;code&gt;Decodable&lt;/code&gt; uses a big &lt;code&gt;switch&lt;/code&gt; statement to decode various specific types:&lt;/p&gt;

&lt;pre&gt;    func decode&amp;lt;T: Decodable&amp;gt;(_ type: T.Type) throws -&amp;gt; T {
        switch type {
&lt;/pre&gt;

&lt;p&gt;For &lt;code&gt;Int&lt;/code&gt; and &lt;code&gt;UInt&lt;/code&gt;, it decodes an &lt;code&gt;Int64&lt;/code&gt; or &lt;code&gt;UInt64&lt;/code&gt;, then converts to an &lt;code&gt;Int&lt;/code&gt; or &lt;code&gt;UInt&lt;/code&gt;, or throws an error:&lt;/p&gt;

&lt;pre&gt;        case is Int.Type:
            let v = try decode(Int64.self)
            if let v = Int(exactly: v) {
                return v as! T
            } else {
                throw Error.intOutOfRange(v)
            }
        case is UInt.Type:
            let v = try decode(UInt64.self)
            if let v = UInt(exactly: v) {
                return v as! T
            } else {
                throw Error.uintOutOfRange(v)
            }
&lt;/pre&gt;

&lt;p&gt;The compiler doesn't realize that &lt;code&gt;T&lt;/code&gt;'s type must match the values being produced, so the &lt;code&gt;as! T&lt;/code&gt; convinces it to compile this code.&lt;/p&gt;

&lt;p&gt;Other integers are handled through &lt;code&gt;FixedWidthInteger&lt;/code&gt; using an extension method:&lt;/p&gt;

&lt;pre&gt;        case let intT as FixedWidthInteger.Type:
            return try intT.from(binaryDecoder: self) as! T
&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;Float&lt;/code&gt;, &lt;code&gt;Double&lt;/code&gt;, and &lt;code&gt;Bool&lt;/code&gt; all call their type-specific decoding methods:&lt;/p&gt;

&lt;pre&gt;        case is Float.Type:
            return try decode(Float.self) as! T
        case is Double.Type:
            return try decode(Double.self) as! T
        case is Bool.Type:
            return try decode(Bool.self) as! T
&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;BinaryDecodable&lt;/code&gt; types use the initializer defined in that protocol, passing &lt;code&gt;self&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;        case let binaryT as BinaryDecodable.Type:
            return try binaryT.init(fromBinary: self) as! T
&lt;/pre&gt;

&lt;p&gt;If none of the cases are hit, then throw an error:&lt;/p&gt;

&lt;pre&gt;        default:
            throw Error.typeNotConformingToBinaryDecodable(type)
        }
    }
&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;FixedWidthInteger&lt;/code&gt; method uses &lt;code&gt;Self.init()&lt;/code&gt; to make a value, reads bytes into it, and then uses the &lt;code&gt;bigEndian:&lt;/code&gt; initializer to perform byte swapping:&lt;/p&gt;

&lt;pre&gt;    private extension FixedWidthInteger {
        static func from(binaryDecoder: BinaryDecoder) throws -&amp;gt; Self {
            var v = Self.init()
            try binaryDecoder.read(into: &amp;amp;v)
            return self.init(bigEndian: v)
        }
    }
&lt;/pre&gt;

&lt;p&gt;That takes care of the foundation. Now to implement &lt;code&gt;Decoder&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;code&gt;BinaryDecoder&lt;/code&gt; &lt;code&gt;Decoder&lt;/code&gt; Implementation&lt;/b&gt;&lt;br&gt;As before, we implement the three container protocols. We'll start with the keyed container:&lt;/p&gt;

&lt;pre&gt;    private struct KeyedContainer&amp;lt;Key: CodingKey&amp;gt;: KeyedDecodingContainerProtocol {
&lt;/pre&gt;

&lt;p&gt;It delegates everything to the decoder, so it needs a reference to that:&lt;/p&gt;

&lt;pre&gt;        var decoder: BinaryDecoder
&lt;/pre&gt;

&lt;p&gt;The protocol requires &lt;code&gt;codingPath&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;        var codingPath: [CodingKey] { return [] }
&lt;/pre&gt;

&lt;p&gt;It also requires &lt;code&gt;allKeys&lt;/code&gt;, which returns all keys that the container knows about. Since we don't really support keys in the first place, this returns an empty array:&lt;/p&gt;

&lt;pre&gt;        var allKeys: [Key] { return [] }
&lt;/pre&gt;

&lt;p&gt;There's also a method to see if the container contains a given key. We'll just blindly say "yes" to all such questions:&lt;/p&gt;

&lt;pre&gt;        func contains(_ key: Key) -&amp;gt; Bool {
            return true
        }
&lt;/pre&gt;

&lt;p&gt;As before, &lt;code&gt;KeyedDecodingContainerProtocol&lt;/code&gt; has a ton of different &lt;code&gt;decode&lt;/code&gt; methods which can all be satisfied with a single generic method for &lt;code&gt;Decodable&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;        func decode&amp;lt;T&amp;gt;(_ type: T.Type, forKey key: Key) throws -&amp;gt; T where T : Decodable {
            return try decoder.decode(T.self)
        }
&lt;/pre&gt;

&lt;p&gt;There's also a &lt;code&gt;decodeNil&lt;/code&gt;, which we'll have do nothing and always succeed:&lt;/p&gt;

&lt;pre&gt;        func decodeNil(forKey key: Key) throws -&amp;gt; Bool {
            return true
        }
&lt;/pre&gt;

&lt;p&gt;Nested containers and superclass decodes delegate back to the decoder:&lt;/p&gt;

&lt;pre&gt;        func nestedContainer&amp;lt;NestedKey&amp;gt;(keyedBy type: NestedKey.Type, forKey key: Key) throws -&amp;gt; KeyedDecodingContainer&amp;lt;NestedKey&amp;gt; where NestedKey : CodingKey {
            return try decoder.container(keyedBy: type)
        }

        func nestedUnkeyedContainer(forKey key: Key) throws -&amp;gt; UnkeyedDecodingContainer {
            return try decoder.unkeyedContainer()
        }

        func superDecoder() throws -&amp;gt; Decoder {
            return decoder
        }

        func superDecoder(forKey key: Key) throws -&amp;gt; Decoder {
            return decoder
        }
    }
&lt;/pre&gt;

&lt;p&gt;Like before, one type can implement both of the other container protocols:&lt;/p&gt;

&lt;pre&gt;    private struct UnkeyedContainer: UnkeyedDecodingContainer, SingleValueDecodingContainer {
        var decoder: BinaryDecoder

        var codingPath: [CodingKey] { return [] }

        var count: Int? { return nil }

        var currentIndex: Int { return 0 }

        var isAtEnd: Bool { return false }

        func decode&amp;lt;T&amp;gt;(_ type: T.Type) throws -&amp;gt; T where T : Decodable {
            return try decoder.decode(type)
        }

        func decodeNil() -&amp;gt; Bool {
            return true
        }

        func nestedContainer&amp;lt;NestedKey&amp;gt;(keyedBy type: NestedKey.Type) throws -&amp;gt; KeyedDecodingContainer&amp;lt;NestedKey&amp;gt; where NestedKey : CodingKey {
            return try decoder.container(keyedBy: type)
        }

        func nestedUnkeyedContainer() throws -&amp;gt; UnkeyedDecodingContainer {
            return self
        }

        func superDecoder() throws -&amp;gt; Decoder {
            return decoder
        }
    }
&lt;/pre&gt;

&lt;p&gt;Now &lt;code&gt;BinaryDecoder&lt;/code&gt; itself can provide dummy implementations of the properties required by &lt;code&gt;Decoder&lt;/code&gt; and implement methods to return instances of the containers:&lt;/p&gt;

&lt;pre&gt;    public var codingPath: [CodingKey] { return [] }

    public var userInfo: [CodingUserInfoKey : Any] { return [:] }

    public func container&amp;lt;Key&amp;gt;(keyedBy type: Key.Type) throws -&amp;gt; KeyedDecodingContainer&amp;lt;Key&amp;gt; where Key : CodingKey {
        return KeyedDecodingContainer(KeyedContainer&amp;lt;Key&amp;gt;(decoder: self))
    }

    public func unkeyedContainer() throws -&amp;gt; UnkeyedDecodingContainer {
        return UnkeyedContainer(decoder: self)
    }

    public func singleValueContainer() throws -&amp;gt; SingleValueDecodingContainer {
        return UnkeyedContainer(decoder: self)
    }
&lt;/pre&gt;

&lt;p&gt;That is the end of &lt;code&gt;BinaryDecoder&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;code&gt;Array&lt;/code&gt; and &lt;code&gt;String&lt;/code&gt; Extensions&lt;/b&gt;&lt;br&gt;In order to make the coders more useful, I implemented &lt;code&gt;BinaryCodable&lt;/code&gt; for &lt;code&gt;Array&lt;/code&gt; and &lt;code&gt;String&lt;/code&gt;. In theory I could call through to their &lt;code&gt;Codable&lt;/code&gt; implementation, but I can't count on that implementation to work with the limitations of the binary coders, and I wouldn't have control over the serialized representation. Instead, I manually implemented it.&lt;/p&gt;

&lt;p&gt;The plan is to have &lt;code&gt;Array&lt;/code&gt; encode its count, and then encode its elements. To decode, it can decode the count, then decode that many elements. &lt;code&gt;String&lt;/code&gt; will convert itself to UTF-8 in the form of &lt;code&gt;Array&amp;lt;UInt8&amp;gt;&lt;/code&gt; and then use &lt;code&gt;Array&lt;/code&gt;'s implementation to do the real work.&lt;/p&gt;

&lt;p&gt;Someday, when Swift gets &lt;a href="https://github.com/apple/swift-evolution/blob/master/proposals/0143-conditional-conformances.md"&gt;conditional conformances&lt;/a&gt;, we'll be able to write &lt;code&gt;extension Array: BinaryCodable where Element: BinaryCodable&lt;/code&gt; to indicate that &lt;code&gt;Array&lt;/code&gt; is is only codable when its contents are. For now, Swift can't express that notion. Instead, we have to say that &lt;code&gt;Array&lt;/code&gt; is always &lt;code&gt;BinaryCodable&lt;/code&gt;, and then do runtime type checks to ensure the content is suitable.&lt;/p&gt;

&lt;p&gt;Encoding is a matter of checking the type of &lt;code&gt;Element&lt;/code&gt;, encoding &lt;code&gt;self.count&lt;/code&gt;, then encoding all of the elements:&lt;/p&gt;

&lt;pre&gt;    extension Array: BinaryCodable {
        public func binaryEncode(to encoder: BinaryEncoder) throws {
            guard Element.self is Encodable.Type else {
                throw BinaryEncoder.Error.typeNotConformingToEncodable(Element.self)
            }

            try encoder.encode(self.count)
            for element in self {
                try (element as! Encodable).encode(to: encoder)
            }
        }
&lt;/pre&gt;

&lt;p&gt;Decoding is the opposite. Check the type, decode the count, then decode that many elements:&lt;/p&gt;

&lt;pre&gt;        public init(fromBinary decoder: BinaryDecoder) throws {
            guard let binaryElement = Element.self as? Decodable.Type else {
                throw BinaryDecoder.Error.typeNotConformingToDecodable(Element.self)
            }

            let count = try decoder.decode(Int.self)
            self.init()
            self.reserveCapacity(count)
            for _ in 0 ..&amp;lt; count {
                let decoded = try binaryElement.init(from: decoder)
                self.append(decoded as! Element)
            }
        }
    }
&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;String&lt;/code&gt; can then encode itself by creating an &lt;code&gt;Array&lt;/code&gt; from its &lt;code&gt;utf8&lt;/code&gt; property and encoding that:&lt;/p&gt;

&lt;pre&gt;    extension String: BinaryCodable {
        public func binaryEncode(to encoder: BinaryEncoder) throws {
            try Array(self.utf8).binaryEncode(to: encoder)
        }
&lt;/pre&gt;

&lt;p&gt;Decoding decodes the UTF-8 &lt;code&gt;Array&lt;/code&gt; and then creates a &lt;code&gt;String&lt;/code&gt; from it. This will fail if the decoded &lt;code&gt;Array&lt;/code&gt; isn't valid UTF-8, so there's a little extra code here to check for that and throw an error:&lt;/p&gt;

&lt;pre&gt;        public init(fromBinary decoder: BinaryDecoder) throws {
            let utf8: [UInt8] = try Array(fromBinary: decoder)
            if let str = String(bytes: utf8, encoding: .utf8) {
                self = str
            } else {
                throw BinaryDecoder.Error.invalidUTF8(utf8)
            }
        }
    }
&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;Example Use&lt;/b&gt;&lt;br&gt;That takes care of binary encoding and decoding. Use is simple. Declare conformance to &lt;code&gt;BinaryCodable&lt;/code&gt;, then use &lt;code&gt;BinaryEncoder&lt;/code&gt; and &lt;code&gt;BinaryDecoder&lt;/code&gt; on your types:&lt;/p&gt;

&lt;pre&gt;    struct Company: BinaryCodable {
        var name: String
        var employees: [Employee]
    }

    struct Employee: BinaryCodable {
        var name: String
        var jobTitle: String
        var age: Int
    }

    let company = Company(name: "Joe's Discount Airbags", employees: [
        Employee(name: "Joe Johnson", jobTitle: "CEO", age: 27),
        Employee(name: "Stan Lee", jobTitle: "Janitor", age: 87),
        Employee(name: "Dracula", jobTitle: "Dracula", age: 41),
        Employee(name: "Steve Jobs", jobTitle: "Visionary", age: 56),
    ])
    let data = try BinaryEncoder.encode(company)
    let roundtrippedCompany = try BinaryDecoder.decode(Company.self, data: data)
    // roundtrippedCompany contains the same data as company
&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;br&gt;Swift's new &lt;code&gt;Codable&lt;/code&gt; protocols are a welcome addition to the language to eliminate a lot of boilerplate code. It's flexible enough to make it straightforward to use/abuse it for things well beyond JSON and property list parsing. Unsophisticated binary formats such as this are not often called for, but they have their uses, and it's interesting to see how &lt;code&gt;Codable&lt;/code&gt; can be used for something so different from the built-in facilities. The &lt;code&gt;Encoder&lt;/code&gt; and &lt;code&gt;Decoder&lt;/code&gt; protocols are large, but judicious use of generics can cut down a lot of the repetitive code, and implementation is relatively simple in the end.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;BinaryCoder&lt;/code&gt; was written for exploratory and educational purposes, and it's probably not what you want to use in your own programs. However, there are cases where it could be suitable, as long as you understand the tradeoffs involved.&lt;/p&gt;

&lt;p&gt;That's it for today! Come back again for more exciting byte-related adventures. As always, Friday Q&amp;amp;A is driven by reader ideas, so if you have a topic you'd like to see covered, please &lt;a href="mailto:mike@mikeash.com"&gt;send it in&lt;/a&gt;!&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;
</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/friday-qa-2017-07-28-a-binary-coder-for-swift.html</guid><pubDate>Fri, 28 Jul 2017 12:44:00 GMT</pubDate></item><item><title>Friday Q&amp;A 2017-07-14: Swift.Codable
</title><link>http://www.mikeash.com/pyblog/friday-qa-2017-07-14-swiftcodable.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;Friday Q&amp;amp;A 2017-07-14: Swift.Codable
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2017 07 14  13 57"
                  tags="fridayqna swift serialization"
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;Friday Q&amp;amp;A 2017-07-14: Swift.Codable
&lt;/div&gt;
              &lt;p&gt;One of the interesting additions to Swift 4 is the &lt;code&gt;Codable&lt;/code&gt; protocol and the machinery around it. This is a subject near and dear to my heart, and I want to discuss what it is and how it works today.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Serialization&lt;/b&gt;&lt;br&gt;Serializing values to data that can be stored on disk or transmitted over a network is a common need. It's especially common in this age of always-connected mobile apps.&lt;/p&gt;

&lt;p&gt;So far, the options for serialization in Apple's ecosystem were limited:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;NSCoding&lt;/code&gt; provides intelligent serialization of complex object graphs and works with your own types, but works with a poorly documented serialization format not suitable for cross-platform work, and requires writing code to manually encode and decode your types.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;NSPropertyListSerialization&lt;/code&gt; and &lt;code&gt;NSJSONSerialization&lt;/code&gt; can convert between standard Cocoa types like &lt;code&gt;NSDictionary&lt;/code&gt;/&lt;code&gt;NSString&lt;/code&gt; and property lists or JSON. JSON in particular is used all over the place for server communication. Since these APIs provide low-level values, you have to write a bunch of code to extract meaning from those values. That code is often ad-hoc and handles bad data poorly.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;NSXMLParser&lt;/code&gt; and &lt;code&gt;NSXMLDocument&lt;/code&gt; are the choice of masochists or people stuck working with systems that use XML. Converting between the basic parsed data and more meaningful model objects is once again up to the programmer.&lt;/li&gt;
&lt;li&gt;Finally, there's always the option to build your own from scratch. This is fun, but a lot of work, and error-prone.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These approaches tend to result in a lot of boilerplate code, where you declare a property called &lt;code&gt;foo&lt;/code&gt; of type &lt;code&gt;String&lt;/code&gt; which is encoded by storing the &lt;code&gt;String&lt;/code&gt; stored in &lt;code&gt;foo&lt;/code&gt; under the key &lt;code&gt;"foo"&lt;/code&gt; and is decoded by retrieving the value for the key &lt;code&gt;"foo"&lt;/code&gt;, attempting to cast it to a &lt;code&gt;String&lt;/code&gt;, storing it into &lt;code&gt;foo&lt;/code&gt; on success, or throwing an error on failure. Then you declare a property called &lt;code&gt;bar&lt;/code&gt; of type &lt;code&gt;String&lt;/code&gt; which....&lt;/p&gt;

&lt;p&gt;Naturally, programmers dislike these repetitive tasks. Repitition is what computers are for. We want to be able to just write this:&lt;/p&gt;

&lt;pre&gt;    struct Whatever {
        var foo: String
        var bar: String
    }
&lt;/pre&gt;

&lt;p&gt;And have it be serializable. It ought to be possible: all the necessary information is already present.&lt;/p&gt;

&lt;p&gt;Reflection is a common way to accomplish this. A lot of Objective-C programmers have written code to automatically read and write Objective-C objects to and from JSON objects. The Objective-C runtime provides all of the information you need to do this automatically. For Swift, we can use the Objective-C runtime, or make do with Swift's Mirror and use wacky workarounds to compensate for its inability to mutate properties.&lt;/p&gt;

&lt;p&gt;Outside of Apple's ecosystem, this is a common approach in many languages. This has led to various &lt;a href="http://blog.codeclimate.com/blog/2013/01/10/rails-remote-code-execution-vulnerability-explained/"&gt;hilarious&lt;/a&gt; &lt;a href="https://github.com/GrrrDog/Java-Deserialization-Cheat-Sheet"&gt;security&lt;/a&gt; &lt;a href="https://www.cs.uic.edu/~s/musings/pickle/"&gt;bugs&lt;/a&gt; over the years.&lt;/p&gt;

&lt;p&gt;Reflection is not a particularly good solution to this problem. It's easy to get it wrong and create security bugs. It's less able to use static typing, so more errors happen at runtime rather than compile time. And it tends to be pretty slow, since the code has to be completely general and does lots of string lookups with type metadata.&lt;/p&gt;

&lt;p&gt;Swift has taken the approach of compile-time code generation rather than runtime reflection. This means that some of the knowledge has to be built in to the compiler, but the result is fast and takes advantage of static typing, while still remaining easy to use.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Overview&lt;/b&gt;&lt;br&gt;There are a few fundamental protocols that Swift's new encoding system is built around.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Encodable&lt;/code&gt; protocol is used for types which can be encoded. If you conform to this protocol and all stored properties in your type are themselves &lt;code&gt;Encodable&lt;/code&gt;, then the compiler will generate an implementation for you. If you don't meet the requirements, or you need special handling, you can implement it yourself.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Decodable&lt;/code&gt; protocol is the companion to the &lt;code&gt;Encodable&lt;/code&gt; protocol and denotes types which can be decoded. Like &lt;code&gt;Encodable&lt;/code&gt;, the compiler will generate an implementation for you if your stored properties are all &lt;code&gt;Decodable&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Because &lt;code&gt;Encodable&lt;/code&gt; and &lt;code&gt;Decodable&lt;/code&gt; usually go together, there's another protocol called &lt;code&gt;Codable&lt;/code&gt; which is just the two protocols glued together:&lt;/p&gt;

&lt;pre&gt;    typealias Codable = Decodable &amp;amp; Encodable
&lt;/pre&gt;

&lt;p&gt;These two protocols are really simple. Each one contains just one requirement:&lt;/p&gt;

&lt;pre&gt;    protocol Encodable {
        func encode(to encoder: Encoder) throws
    }

    protocol Decodable {
        init(from decoder: Decoder) throws
    }
&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;Encoder&lt;/code&gt; and &lt;code&gt;Decoder&lt;/code&gt; protocols specify how objects can actually encode and decode themselves. You don't have to worry about these for basic use, since the default implementation of &lt;code&gt;Codable&lt;/code&gt; handles all the details for you, but you need to use them if you write your own &lt;code&gt;Codable&lt;/code&gt; implementation. These are complex and we'll look at them later.&lt;/p&gt;

&lt;p&gt;Finally, there's a &lt;code&gt;CodingKey&lt;/code&gt; protocol which is used to denote keys used for encoding and decoding. This adds an extra layer of static type checking to the process compared to using plain strings everywhere. It provides a &lt;code&gt;String&lt;/code&gt;, and optionally an &lt;code&gt;Int&lt;/code&gt; for positional keys:&lt;/p&gt;

&lt;pre&gt;    protocol CodingKey {
        var stringValue: String { get }
        init?(stringValue: String)

        var intValue: Int? { get }
        public init?(intValue: Int)
    }
&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;Encoders and Decoders&lt;/b&gt;&lt;br&gt;The basic concept of &lt;code&gt;Encoder&lt;/code&gt; and &lt;code&gt;Decoder&lt;/code&gt; is similar to &lt;code&gt;NSCoder&lt;/code&gt;. Objects receive a coder and then call its methods to encode or decode themselves.&lt;/p&gt;

&lt;p&gt;The API of &lt;code&gt;NSCoder&lt;/code&gt; is straightforward. &lt;code&gt;NSCoder&lt;/code&gt; has a bunch of methods like &lt;code&gt;encodeObject:forKey:&lt;/code&gt; and &lt;code&gt;encodeInteger:forKey:&lt;/code&gt; which objects call to perform their coding. Objects can also use unkeyed methods like &lt;code&gt;encodeObject:&lt;/code&gt; and &lt;code&gt;encodeInteger:&lt;/code&gt; to do things positionally instead of by key.&lt;/p&gt;

&lt;p&gt;Swift's API is more indirect. &lt;code&gt;Encoder&lt;/code&gt; doesn't have any methods of its own for encoding values. Instead, it provides &lt;em&gt;containers&lt;/em&gt;, and those containers then have methods for encoding values. There's one container for keyed encoding, one for unkeyed encoding, and one for encoding a single value.&lt;/p&gt;

&lt;p&gt;This helps make things more explicit and fits better with portable serialization formats. &lt;code&gt;NSCoder&lt;/code&gt; only has to work with Apple's encoding format so it just needs to put the same thing out that it got in. &lt;code&gt;Encoder&lt;/code&gt; has to work with things like JSON. If an object encodes values with keys, that should produce a JSON dictionary. If it uses unkeyed encoding then that should produce a JSON array. What if the object is empty and encodes no values? With the &lt;code&gt;NSCoder&lt;/code&gt; approach, it would have no idea what to output. With &lt;code&gt;Encoder&lt;/code&gt;, the object will still request a keyed or unkeyed container and the encoder can figure it out from that.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Decoder&lt;/code&gt; works the same way. You don't decode values from it directly, but rather ask for a container, and then decode values from the container. Like &lt;code&gt;Encoder&lt;/code&gt;, &lt;code&gt;Decoder&lt;/code&gt; provides keyed, unkeyed, and single value containers.&lt;/p&gt;

&lt;p&gt;Because of this container design, the &lt;code&gt;Encoder&lt;/code&gt; and &lt;code&gt;Decoder&lt;/code&gt; protocols themselves are small.  They contain a bit of bookkeeping info, and methods for obtaining containers:&lt;/p&gt;

&lt;pre&gt;    protocol Encoder {
        var codingPath: [CodingKey?] { get }
        public var userInfo: [CodingUserInfoKey : Any] { get }

        func container&amp;lt;Key&amp;gt;(keyedBy type: Key.Type)
            -&amp;gt; KeyedEncodingContainer&amp;lt;Key&amp;gt; where Key : CodingKey
        func unkeyedContainer() -&amp;gt; UnkeyedEncodingContainer
        func singleValueContainer() -&amp;gt; SingleValueEncodingContainer
    }

    protocol Decoder {
        var codingPath: [CodingKey?] { get }
        var userInfo: [CodingUserInfoKey : Any] { get }

        func container&amp;lt;Key&amp;gt;(keyedBy type: Key.Type) throws
            -&amp;gt; KeyedDecodingContainer&amp;lt;Key&amp;gt; where Key : CodingKey
        func unkeyedContainer() throws -&amp;gt; UnkeyedDecodingContainer
        func singleValueContainer() throws -&amp;gt; SingleValueDecodingContainer
    }
&lt;/pre&gt;

&lt;p&gt;The complexity is in the container types. You can get pretty far by recursively walking through properties of &lt;code&gt;Codable&lt;/code&gt; types, but at some point you need to get down to some raw encodable types which can be directly encoded and decoded. For &lt;code&gt;Codable&lt;/code&gt;, those types include the various integer types, &lt;code&gt;Float&lt;/code&gt;, &lt;code&gt;Double&lt;/code&gt;, &lt;code&gt;Bool&lt;/code&gt;, and &lt;code&gt;String&lt;/code&gt;. That makes for a whole bunch of really similar encode/decode methods. Unkeyed containers also directly support encoding sequences of the raw encodable types.&lt;/p&gt;

&lt;p&gt;Beyond those basic methods, there are a bunch of methods that support exotic use cases. KeyedDecodingContainer has methods called &lt;code&gt;decodeIfPresent&lt;/code&gt; which return an optional and return &lt;code&gt;nil&lt;/code&gt; for missing keys instead of throwing. The encoding containers have methods for weak encoding, which encodes an object only if something else encodes it too (useful for parent references in a complex graph). There are methods for getting nested containers, which allows you to encode hierarchies. Finally, there are methods for getting a "super" encoder or decoder, which is intended to allow subclasses and superclasses to coexist peacefully when encoding and decoding. The subclass can encode itself directly, and then ask the superclass to encode itself with a "super" encoder, which ensures keys don't conflict.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Implementing &lt;code&gt;Codable&lt;/code&gt;&lt;/b&gt;&lt;br&gt;Implementing &lt;code&gt;Codable&lt;/code&gt; is easy: declare conformance and let the compiler generate it for you.&lt;/p&gt;

&lt;p&gt;It's useful to know just what it's doing, though. Let's take a look at what it ends up generating and how you would do it yourself. We'll start with an example &lt;code&gt;Codable&lt;/code&gt; type:&lt;/p&gt;

&lt;pre&gt;    struct Person: Codable {
        var name: String
        var age: Int
        var quest: String
    }
&lt;/pre&gt;

&lt;p&gt;The compiler generates a &lt;code&gt;CodingKeys&lt;/code&gt; type nested inside &lt;code&gt;Person&lt;/code&gt;. If we did it ourselves, that nested type would look like this:&lt;/p&gt;

&lt;pre&gt;    private enum CodingKeys: CodingKey {
        case name
        case age
        case quest
    }
&lt;/pre&gt;

&lt;p&gt;The case names match &lt;code&gt;Person&lt;/code&gt;'s property names. Compiler magic gives each CodingKeys case a string value which matches its case name, which means that the property names are also the keys used for encoding them.&lt;/p&gt;

&lt;p&gt;If we need different names, we can easily accomplish this by providing our own &lt;code&gt;CodingKeys&lt;/code&gt; with custom raw values. For example, we might write this:&lt;/p&gt;

&lt;pre&gt;    private enum CodingKeys: String, CodingKey {
        case name = "person_name"
        case age
        case quest
    }
&lt;/pre&gt;

&lt;p&gt;This will cause the &lt;code&gt;name&lt;/code&gt; property to be encoded and decoded under &lt;code&gt;person_name&lt;/code&gt;. And this is all we have to do. The compiler happily accepts our custom &lt;code&gt;CodingKeys&lt;/code&gt; type while still providing a default implementation for the rest of &lt;code&gt;Codable&lt;/code&gt;, and that default implementation uses our custom type. You can mix and match customizations with the compiler-provided code.&lt;/p&gt;

&lt;p&gt;The compiler also generates an implementation for &lt;code&gt;encode(to:)&lt;/code&gt; and &lt;code&gt;init(from:)&lt;/code&gt;. The implementation of &lt;code&gt;encode(to:)&lt;/code&gt; gets a keyed container and then encodes each property in turn:&lt;/p&gt;

&lt;pre&gt;    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)

        try container.encode(name, forKey: .name)
        try container.encode(age, forKey: .age)
        try container.encode(quest, forKey: .quest)
    }
&lt;/pre&gt;

&lt;p&gt;The compiler generates an implementation of &lt;code&gt;init(from:)&lt;/code&gt; which mirrors this:&lt;/p&gt;

&lt;pre&gt;    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        name = try container.decode(String.self, forKey: .name)
        age = try container.decode(Int.self, forKey: .age)
        quest = try container.decode(String.self, forKey: .quest)
    }
&lt;/pre&gt;

&lt;p&gt;That's all there is to it. Just like with &lt;code&gt;CodingKeys&lt;/code&gt;, if you need custom behavior here you can implement your own version of one of these methods while letting the compiler generate the rest. Unfortunately, there's no way to specify custom behavior for an individual property, so you have to write out the whole thing even if you want the default behavior for the rest. This is not particularly terrible, though.&lt;/p&gt;

&lt;p&gt;If you were to do it all by hand, the full implementation of &lt;code&gt;Codable&lt;/code&gt; for &lt;code&gt;Person&lt;/code&gt; would look like this:&lt;/p&gt;

&lt;pre&gt;    extension Person {
        private enum CodingKeys: CodingKey {
            case name
            case age
            case quest
        }

        func encode(to encoder: Encoder) throws {
            var container = encoder.container(keyedBy: CodingKeys.self)

            try container.encode(name, forKey: .name)
            try container.encode(age, forKey: .age)
            try container.encode(quest, forKey: .quest)
        }

        init(from decoder: Decoder) throws {
            let container = try decoder.container(keyedBy: CodingKeys.self)

            name = try container.decode(String.self, forKey: .name)
            age = try container.decode(Int.self, forKey: .age)
            quest = try container.decode(String.self, forKey: .quest)
        }
    }
&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;Implementing &lt;code&gt;Encoder&lt;/code&gt; and &lt;code&gt;Decoder&lt;/code&gt;&lt;/b&gt;&lt;br&gt;You may never need to implement your own &lt;code&gt;Encoder&lt;/code&gt; or &lt;code&gt;Decoder&lt;/code&gt;. Swift provides implementations for JSON and property lists, which take care of the common use cases.&lt;/p&gt;

&lt;p&gt;You can implement your own in order to support a custom format. The size of the container protocols means this will take some effort. Fortunately, it's mostly a matter of size, not complexity.&lt;/p&gt;

&lt;p&gt;To implement a custom &lt;code&gt;Encoder&lt;/code&gt;, you'll need something that implements the &lt;code&gt;Encoder&lt;/code&gt; protocol plus implementations of the container protocols. Implementing the three container protocols involves a lot of repetitive code to implement encoding or decoding methods for all of the various directly encodable types.&lt;/p&gt;

&lt;p&gt;How they work is up to you. The &lt;code&gt;Encoder&lt;/code&gt; will probably need to store the data being encoded, and the containers will inform the &lt;code&gt;Encoder&lt;/code&gt; of the various things they're encoding.&lt;/p&gt;

&lt;p&gt;Implementing a custom &lt;code&gt;Decoder&lt;/code&gt; is similar. You'll need to implement that protocol plus the container protocols. The decoder will hold the serialized data and the containers will communicate with it to provide the requested values.&lt;/p&gt;

&lt;p&gt;I've been experimenting with a custom binary encoder and decoder as a way to learn the protocols, and I hope to present that in a future article as an example of how to do it.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;br&gt;Swift 4's &lt;code&gt;Codable&lt;/code&gt; API looks great and ought to simplify a lot of common code. For typical JSON tasks, it's sufficient to declare conformance to &lt;code&gt;Codable&lt;/code&gt; in your model types and let the compiler do the rest. When needed, you can implement parts of the protocol yourself in order to handle things differently, and you can implement it all if needed.&lt;/p&gt;

&lt;p&gt;The companion &lt;code&gt;Encoder&lt;/code&gt; and &lt;code&gt;Decoder&lt;/code&gt; protocols are more complex, but justifiably so. Supporting a custom format by implementing your own &lt;code&gt;Encoder&lt;/code&gt; and &lt;code&gt;Decoder&lt;/code&gt; takes some work, but is mostly a matter of filling in a lot of similar blanks.&lt;/p&gt;

&lt;p&gt;That's it for today! Come back again for more exciting serialization-related material, and perhaps even things not related to serialization. Until then, Friday Q&amp;amp;A is driven by reader ideas, so if you have a topic you'd like to see covered here, please &lt;a href="mailto:mike@mikeash.com"&gt;send it in&lt;/a&gt;!&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;
</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/friday-qa-2017-07-14-swiftcodable.html</guid><pubDate>Fri, 14 Jul 2017 13:57:00 GMT</pubDate></item><item><title>Friday Q&amp;A 2017-06-30: Dissecting objc_msgSend on ARM64
</title><link>http://www.mikeash.com/pyblog/friday-qa-2017-06-30-dissecting-objc_msgsend-on-arm64.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;Friday Q&amp;amp;A 2017-06-30: Dissecting objc_msgSend on ARM64
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2017 07 01  04 23"
                  tags="fridayqna objectivec assembly"
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;Friday Q&amp;amp;A 2017-06-30: Dissecting objc_msgSend on ARM64
&lt;/div&gt;
              &lt;p&gt;We're back! During the week of WWDC, I spoke at CocoaConf Next Door, and one of my talks involved a dissection of &lt;code&gt;objc_msgSend&lt;/code&gt;'s ARM64 implementation. I thought that turning it into an article would make for a nice return to blogging for Friday Q&amp;amp;A.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Overview&lt;/b&gt;&lt;br&gt;Every Objective-C object has a class, and every Objective-C class has a list of methods. Each method has a selector, a function pointer to the implementation, and some metadata. The job of &lt;code&gt;objc_msgSend&lt;/code&gt; is to take the object and selector that's passed in, look up the corresponding method's function pointer, and then jump to that function pointer.&lt;/p&gt;

&lt;p&gt;Looking up a method can be extremely complicated. If a method isn't found on a class, then it needs to continue searching in the superclasses. If no method is found at all, then it needs to call into the runtime's message forwarding code. If this is the very first message being sent to a particular class, then it has to call that class's &lt;code&gt;+initialize&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Looking up a method also needs to be extremely fast in the common case, since it's done for every method call. This, of course, is in conflict with the complicated lookup process.&lt;/p&gt;

&lt;p&gt;Objective-C's solution to this conflict is the method cache. Each class has a cache which stores methods as pairs of selectors and function pointers, known in Objective-C as &lt;code&gt;IMP&lt;/code&gt;s. They're organized as a hash table so lookups are fast. When looking up a method, the runtime first consults the cache. If the method isn't in the cache, it follows the slow, complicated procedure, and then places the result into the cache so that the next time can be fast.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;objc_msgSend&lt;/code&gt; is written in assembly. There are two reasons for this: one is that it's not possible to write a function which preserves unknown arguments and jumps to an arbitrary function pointer in C. The language just doesn't have the necessary features to express such a thing. The other reason is that it's extremely important for &lt;code&gt;objc_msgSend&lt;/code&gt; to be fast, so every last instruction of it is written by hand so it can go as fast as possible.&lt;/p&gt;

&lt;p&gt;Naturally, you don't want to write the whole complicated message lookup procedure in assembly langauge. It's not necessary, either, because things are going to be slow no matter what the moment you start going through it. The message send code can be divided into two parts: there's the &lt;em&gt;fast path&lt;/em&gt; in &lt;code&gt;objc_msgSend&lt;/code&gt; itself, which is written in assembly, and the &lt;em&gt;slow path&lt;/em&gt; implemented in C. The assembly part looks up the method in the cache and jump to it if it's found. If the method is not in the cache, then it calls into the C code to handle things.&lt;/p&gt;

&lt;p&gt;Therefore, when looking at &lt;code&gt;objc_msgSend&lt;/code&gt; itself, it does the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Get the class of the object passed in.&lt;/li&gt;
&lt;li&gt;Get the method cache of that class.&lt;/li&gt;
&lt;li&gt;Use the selector passed in to look up the method in the cache.&lt;/li&gt;
&lt;li&gt;If it's not in the cache, call into the C code.&lt;/li&gt;
&lt;li&gt;Jump to the &lt;code&gt;IMP&lt;/code&gt; for the method.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;How does it do all of that? Let's see!&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Instruction by Instruction&lt;/b&gt;&lt;br&gt;&lt;code&gt;objc_msgSend&lt;/code&gt; has a few different paths it can take depending on circumstances. It has special code for handling things like messages to &lt;code&gt;nil&lt;/code&gt;, tagged pointers, and hash table collisions. I'll start by looking at the most common, straight-line case where a message is sent to a non-&lt;code&gt;nil&lt;/code&gt;, non-tagged pointer and the method is found in the cache without any need to scan. I'll note the various branching-off points as we go through them, and then once we're done with the common path I'll circle back and look at all of the others.&lt;/p&gt;

&lt;p&gt;I'll list each instruction or group of instructions followed by a description of what it does and why. Just remember to look &lt;em&gt;up&lt;/em&gt; to find the instruction any given piece of text is discussing.&lt;/p&gt;

&lt;p&gt;Each instruction is preceded by its offset from the beginning of the function. This serves as a counter, and lets you identify jump targets.&lt;/p&gt;

&lt;p&gt;ARM64 has 31 integer registers which are 64 bits wide. They're referred to with the notation &lt;code&gt;x0&lt;/code&gt; through &lt;code&gt;x30&lt;/code&gt;. It's also possible to access the lower 32 bits of each register as if it were a separate register, using &lt;code&gt;w0&lt;/code&gt; through &lt;code&gt;w30&lt;/code&gt;. Registers &lt;code&gt;x0&lt;/code&gt; through &lt;code&gt;x7&lt;/code&gt; are used to pass the first eight parameters to a function. That means that &lt;code&gt;objc_msgSend&lt;/code&gt; receives the &lt;code&gt;self&lt;/code&gt; parameter in &lt;code&gt;x0&lt;/code&gt; and the selector &lt;code&gt;_cmd&lt;/code&gt; parameter in &lt;code&gt;x1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's begin!&lt;/p&gt;

&lt;pre&gt;    0x0000 cmp     x0, #0x0
    0x0004 b.le    0x6c
&lt;/pre&gt;

&lt;p&gt;This performs a signed comparison of &lt;code&gt;self&lt;/code&gt; with &lt;code&gt;0&lt;/code&gt; and jumps elsewhere if the value is less than or equal to zero. A value of zero is &lt;code&gt;nil&lt;/code&gt;, so this handles the special case of messages to nil. This also handles &lt;a href="friday-qa-2012-07-27-lets-build-tagged-pointers.html"&gt;tagged pointers&lt;/a&gt;. Tagged pointers on ARM64 are indicated by setting the high bit of the pointer. (This is an interesting contrast with x86-64, where it's the low bit.) If the high bit is set, then the value is negative when interpreted as a signed integer. For the common case of &lt;code&gt;self&lt;/code&gt; being a normal pointer, the branch is not taken.&lt;/p&gt;

&lt;pre&gt;    0x0008 ldr    x13, [x0]
&lt;/pre&gt;

&lt;p&gt;This loads &lt;code&gt;self&lt;/code&gt;'s &lt;code&gt;isa&lt;/code&gt; by loading the 64-bit quantity pointed to by &lt;code&gt;x0&lt;/code&gt;, which contains &lt;code&gt;self&lt;/code&gt;. The &lt;code&gt;x13&lt;/code&gt; register now contains the &lt;code&gt;isa&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;    0x000c and    x16, x13, #0xffffffff8
&lt;/pre&gt;

&lt;p&gt;ARM64 can use &lt;a href="http://www.sealiesoftware.com/blog/archive/2013/09/24/objc_explain_Non-pointer_isa.html"&gt;non-pointer isas&lt;/a&gt;. Traditionally the &lt;code&gt;isa&lt;/code&gt; points to the object's class, but non-pointer &lt;code&gt;isa&lt;/code&gt; takes advantage of spare bits by cramming some other information into the &lt;code&gt;isa&lt;/code&gt; as well. This instruction performs a logical AND to mask off all the extra bits, and leaves the actual class pointer in &lt;code&gt;x16&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;    0x0010 ldp    x10, x11, [x16, #0x10]
&lt;/pre&gt;

&lt;p&gt;This is my favorite instruction in &lt;code&gt;objc_msgSend&lt;/code&gt;. It loads the class's cache information into &lt;code&gt;x10&lt;/code&gt; and &lt;code&gt;x11&lt;/code&gt;. The &lt;code&gt;ldp&lt;/code&gt; instruction loads &lt;em&gt;two&lt;/em&gt; registers' worth of data from memory into the registers named in the first two arguments. The third argument describes where to load the data, in this case at offset &lt;code&gt;16&lt;/code&gt; from &lt;code&gt;x16&lt;/code&gt;, which is the area of the class which holds the cache information. The cache itself looks like this:&lt;/p&gt;

&lt;pre&gt;    typedef uint32_t mask_t;

    struct cache_t {
        struct bucket_t *_buckets;
        mask_t _mask;
        mask_t _occupied;
    }
&lt;/pre&gt;

&lt;p&gt;Following the &lt;code&gt;ldp&lt;/code&gt; instruction, &lt;code&gt;x10&lt;/code&gt; contains the value of &lt;code&gt;_buckets&lt;/code&gt;, and &lt;code&gt;x11&lt;/code&gt; contains &lt;code&gt;_occupied&lt;/code&gt; in its high 32 bits, and &lt;code&gt;_mask&lt;/code&gt; in its low 32 bits.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;_occupied&lt;/code&gt; specifies how many entries the hash table contains, and plays no role in &lt;code&gt;objc_msgSend&lt;/code&gt;. &lt;code&gt;_mask&lt;/code&gt; is important: it describes the size of the hash table as a convenient AND-able mask. Its value is always a power of two minus 1, or in binary terms something that looks like &lt;code&gt;000000001111111&lt;/code&gt; with a variable number of 1s at the end. This value is needed to figure out the lookup index for a selector, and to wrap around the end when searching the table.&lt;/p&gt;

&lt;pre&gt;    0x0014 and    w12, w1, w11
&lt;/pre&gt;

&lt;p&gt;This instruction computes the starting hash table index for the selector passed in as &lt;code&gt;_cmd&lt;/code&gt;. &lt;code&gt;x1&lt;/code&gt; contains &lt;code&gt;_cmd&lt;/code&gt;, so &lt;code&gt;w1&lt;/code&gt; contains the bottom 32 bits of &lt;code&gt;_cmd&lt;/code&gt;. &lt;code&gt;w11&lt;/code&gt; contains &lt;code&gt;_mask&lt;/code&gt; as mentioned above. This instruction ANDs the two together and places the result into &lt;code&gt;w12&lt;/code&gt;. The result is the equivalent of computing &lt;code&gt;_cmd % table_size&lt;/code&gt; but without the expensive modulo operation.&lt;/p&gt;

&lt;pre&gt;    0x0018 add    x12, x10, x12, lsl #4
&lt;/pre&gt;

&lt;p&gt;The index is not enough. To start loading data from the table, we need the actual address to load from. This instruction computes that address by adding the table index to the table pointer. It shifts the table index left by &lt;code&gt;4&lt;/code&gt; bits first, which multiplies it by &lt;code&gt;16&lt;/code&gt;, because each table bucket is &lt;code&gt;16&lt;/code&gt; bytes. &lt;code&gt;x12&lt;/code&gt; now contains the address of the first bucket to search.&lt;/p&gt;

&lt;pre&gt;    0x001c ldp    x9, x17, [x12]
&lt;/pre&gt;

&lt;p&gt;Our friend &lt;code&gt;ldp&lt;/code&gt; makes another appearance. This time it's loading from the pointer in &lt;code&gt;x12&lt;/code&gt;, which points to the bucket to search. Each bucket contains a selector and an &lt;code&gt;IMP&lt;/code&gt;. &lt;code&gt;x9&lt;/code&gt; now contains the selector for the current bucket, and &lt;code&gt;x17&lt;/code&gt; contains the &lt;code&gt;IMP&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;    0x0020 cmp    x9, x1
    0x0024 b.ne   0x2c
&lt;/pre&gt;

&lt;p&gt;These instructions compare the bucket's selector in &lt;code&gt;x9&lt;/code&gt; with &lt;code&gt;_cmd&lt;/code&gt; in &lt;code&gt;x1&lt;/code&gt;. If they're not equal then this bucket does not contain an entry for the selector we're looking for, and in that case the second instruction jumps to offset &lt;code&gt;0x2c&lt;/code&gt;, which handles non-matching buckets. If the selectors do match, then we've found the entry we're looking for, and execution continues with the next instruction.&lt;/p&gt;

&lt;pre&gt;    0x0028 br    x17
&lt;/pre&gt;

&lt;p&gt;This performs an unconditional jump to &lt;code&gt;x17&lt;/code&gt;, which contains the &lt;code&gt;IMP&lt;/code&gt; loaded from the current bucket. From here, execution will continue in the actual implementation of the target method, and this is the end of &lt;code&gt;objc_msgSend's&lt;/code&gt; fast path. All of the argument registers have been left undisturbed, so the target method will receive all passed in arguments just as if it had been called directly.&lt;/p&gt;

&lt;p&gt;When everything is cached and all the stars align, this path can execute in less than 3 nanoseconds on modern hardware.&lt;/p&gt;

&lt;p&gt;That's the fast path, how about the rest of the code? Let's continue with the code for a non-matching bucket.&lt;/p&gt;

&lt;pre&gt;    0x002c cbz    x9, __objc_msgSend_uncached
&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;x9&lt;/code&gt; contains the selector loaded from the bucket. This instruction compares it with zero and jumps to &lt;code&gt;__objc_msgSend_uncached&lt;/code&gt; if it's zero. A zero selector indicates an empty bucket, and an empty bucket means that the search has failed. The target method isn't in the cache, and it's time to fall back to the C code that performs a more comprehensive lookup. &lt;code&gt;__objc_msgSend_uncached&lt;/code&gt; handles that. Otherwise, the bucket doesn't match but isn't empty, and the search continues.&lt;/p&gt;

&lt;pre&gt;    0x0030 cmp    x12, x10
    0x0034 b.eq   0x40
&lt;/pre&gt;

&lt;p&gt;This instruction compares the current bucket address in &lt;code&gt;x12&lt;/code&gt; with the beginning of the hash table in &lt;code&gt;x10&lt;/code&gt;. If they match, it jumps to code that wraps the search back to the end of the hash table. We haven't seen it yet, but the hash table search being performed here actually runs backwards. The search examines decreasing indexes until it hits the beginning of the table, then it starts over at the end. I'm not sure why it works this way rather than the more common approach of increasing addresses that wrap to the beginning, but it's a safe bet that it's because it ends up being faster this way.&lt;/p&gt;

&lt;p&gt;Offset &lt;code&gt;0x40&lt;/code&gt; handles the wraparound case. Otherwise, execution proceeds to the next instruction.&lt;/p&gt;

&lt;pre&gt;    0x0038 ldp    x9, x17, [x12, #-0x10]!
&lt;/pre&gt;

&lt;p&gt;Another &lt;code&gt;ldp&lt;/code&gt;, once again loading a cache bucket. This time, it loads from offset &lt;code&gt;0x10&lt;/code&gt; to the address of the current cache bucket. The exclamation point at the end of the address reference is an interesting feature. This indicates a register write-back, which means that the register is updated with the newly computed value. In this case, it's effectively doing &lt;code&gt;x12 -= 16&lt;/code&gt; in addition to loading the new bucket, which makes &lt;code&gt;x12&lt;/code&gt; point to that new bucket.&lt;/p&gt;

&lt;pre&gt;    0x003c b      0x20
&lt;/pre&gt;

&lt;p&gt;Now that the new bucket is loaded, execution can resume with the code that checks to see if the current bucket is a match. This loops back up to the instruction labeled &lt;code&gt;0x0020&lt;/code&gt; above, and runs through all of that code again with the new values. If it continues to find non-matching buckets, this code will keep running until it finds a match, an empty bucket, or hits the beginning of the table.&lt;/p&gt;

&lt;pre&gt;    0x0040 add    x12, x12, w11, uxtw #4
&lt;/pre&gt;

&lt;p&gt;This is the target for when the search wraps. &lt;code&gt;x12&lt;/code&gt; contains a pointer to the current bucket, which in this case is also the first bucket. &lt;code&gt;w11&lt;/code&gt; contains the table mask, which is the size of the table. This adds the two together, while also shifting &lt;code&gt;w11&lt;/code&gt; left by 4 bits, multiplying it by &lt;code&gt;16&lt;/code&gt;. The result is that &lt;code&gt;x12&lt;/code&gt; now points to the end of the table, and the search can resume from there.&lt;/p&gt;

&lt;pre&gt;    0x0044 ldp    x9, x17, [x12]
&lt;/pre&gt;

&lt;p&gt;The now-familiar &lt;code&gt;ldp&lt;/code&gt; loads the new bucket into &lt;code&gt;x9&lt;/code&gt; and &lt;code&gt;x17&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;    0x0048 cmp    x9, x1
    0x004c b.ne   0x54
    0x0050 br     x17
&lt;/pre&gt;

&lt;p&gt;This code checks to see if the bucket matches and jumps to the bucket's &lt;code&gt;IMP&lt;/code&gt;. It's a duplicate of the code at &lt;code&gt;0x0020&lt;/code&gt; above.&lt;/p&gt;

&lt;pre&gt;    0x0054 cbz    x9, __objc_msgSend_uncached
&lt;/pre&gt;

&lt;p&gt;Just like before, if the bucket is empty then it's a cache miss and execution proceeds into the comprehensive lookup code implemented in C.&lt;/p&gt;

&lt;pre&gt;    0x0058 cmp    x12, x10
    0x005c b.eq   0x68
&lt;/pre&gt;

&lt;p&gt;This checks for wraparound &lt;em&gt;again&lt;/em&gt;, and jumps to &lt;code&gt;0x68&lt;/code&gt; if we've hit the beginning of the table a second time. In this case, it jumps into the comprehensive lookup code implemented in C:&lt;/p&gt;

&lt;pre&gt;    0x0068 b      __objc_msgSend_uncached
&lt;/pre&gt;

&lt;p&gt;This is something that should never actually happen. The table grows as entries are added to it, and it's never 100% full. Hash tables become inefficient when they're too full because collisions become too common.&lt;/p&gt;

&lt;p&gt;Why is this here? A comment in the source code explains:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Clone scanning loop to miss instead of hang when cache is corrupt. The slow path may detect any corruption and halt later.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I doubt that this is common, but evidently the folks at Apple have seen memory corruption which caused the cache to be filled with bad entries, and jumping into the C code improves the diagnostics.&lt;/p&gt;

&lt;p&gt;The existence of this check should have minimal impact on code that doesn't suffer from this corruption. Without it, the original loop could be reused, which would save a bit of instruction cache space, but the effect is minimal. This wraparound handler is not the common case anyway. It will only be invoked for selectors that get sorted near the beginning of the hash table, and then only if there's a collision and all the prior entries are occupied.&lt;/p&gt;

&lt;pre&gt;    0x0060 ldp    x9, x17, [x12, #-0x10]!
    0x0064 b      0x48
&lt;/pre&gt;

&lt;p&gt;The remainder of this loop is the same as before. Load the next bucket into &lt;code&gt;x9&lt;/code&gt; and &lt;code&gt;x17&lt;/code&gt;, update the bucket pointer in &lt;code&gt;x12&lt;/code&gt;, and go back to the top of the loop.&lt;/p&gt;

&lt;p&gt;That's the end of the main body of &lt;code&gt;objc_msgSend&lt;/code&gt;. What remains are special cases for &lt;code&gt;nil&lt;/code&gt; and tagged pointers.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Tagged Pointer Handler&lt;/b&gt;&lt;br&gt;You'll recall that the very first instructions checked for those and jumped to offset &lt;code&gt;0x6c&lt;/code&gt; to handle them. Let's continue from there:&lt;/p&gt;

&lt;pre&gt;    0x006c b.eq    0xa4
&lt;/pre&gt;

&lt;p&gt;We've arrived here because &lt;code&gt;self&lt;/code&gt; is less than or equal to zero. Less than zero indicates a tagged pointer, and zero is &lt;code&gt;nil&lt;/code&gt;. The two cases are handled completely differently, so the first thing the code does here is check to see whether &lt;code&gt;self&lt;/code&gt; is &lt;code&gt;nil&lt;/code&gt; or not. If &lt;code&gt;self&lt;/code&gt; is equal to zero then this instruction branches to &lt;code&gt;0xa4&lt;/code&gt;, which is where the &lt;code&gt;nil&lt;/code&gt; handler lives. Otherwise, it's a tagged pointer, and execution continues with the next instruction.&lt;/p&gt;

&lt;p&gt;Before we move on, let's briefly discuss how tagged pointers work. Tagged pointers support multiple classes. The top four bits of the tagged pointer (on ARM64) indicate which class the "object" is. They are essentially the tagged pointer's isa. Of course, four bits isn't nearly enough to hold a class pointer. Instead, there's a special table which stores the available tagged pointer classes. The class of a tagged pointer "object" is found by looking up the index in that table which corresponds to the top four bits.&lt;/p&gt;

&lt;p&gt;This isn't the whole story. Tagged pointers (at least on ARM64) also support extended classes. When the top four bits are all set to &lt;code&gt;1&lt;/code&gt; the next eight bits are used to index into an extended tagged pointer class table. This allows the runtime to support more tagged pointer classes, at the cost of having less storage for them.&lt;/p&gt;

&lt;p&gt;Let's continue.&lt;/p&gt;

&lt;pre&gt;    0x0070 mov    x10, #-0x1000000000000000
&lt;/pre&gt;

&lt;p&gt;This sets &lt;code&gt;x10&lt;/code&gt; to an integer value with the top four bits set and all other bits set to zero. This will serve as a mask to extract the tag bits from &lt;code&gt;self&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;    0x0074 cmp    x0, x10
    0x0078 b.hs   0x90
&lt;/pre&gt;

&lt;p&gt;This checks for an extended tagged pointer. If &lt;code&gt;self&lt;/code&gt; is greater than or equal to the value in &lt;code&gt;x10&lt;/code&gt;, then that means the top four bits are all set. In that case, branch to &lt;code&gt;0x90&lt;/code&gt; which will handle extended classes. Otherwise, use the primary tagged pointer table.&lt;/p&gt;

&lt;pre&gt;    0x007c adrp   x10, _objc_debug_taggedpointer_classes@PAGE
    0x0080 add    x10, x10, _objc_debug_taggedpointer_classes@PAGEOFF
&lt;/pre&gt;

&lt;p&gt;This little song and dance loads the address of &lt;code&gt;_objc_debug_taggedpointer_classes&lt;/code&gt;, which is the primary tagged pointer table. ARM64 requires two instructions to load the address of a symbol. This is a standard technique on RISC-like architectures. Pointers on ARM64 are 64 bits wide, and instructions are only 32 bits wide. It's not possible to fit an entire pointer into one instruction.&lt;/p&gt;

&lt;p&gt;x86 doesn't suffer from this problem, since it has variable-length instructions. It can just use a 10-byte instruction, where two bytes identify the instruction itself and the target register, and eight bytes hold the pointer value.&lt;/p&gt;

&lt;p&gt;On a machine with fixed-length instructions, you load the value in pieces. In this case, only two pieces are needed. The &lt;code&gt;adrp&lt;/code&gt; instruction loads the top part of the value, and the &lt;code&gt;add&lt;/code&gt; then adds in the bottom part.&lt;/p&gt;

&lt;pre&gt;    0x0084 lsr    x11, x0, #60
&lt;/pre&gt;

&lt;p&gt;The tagged class index is in the top four bits of &lt;code&gt;x0&lt;/code&gt;. To use it as an index, it has to be shifted right by &lt;code&gt;60&lt;/code&gt; bits so it becomes an integer in the range &lt;code&gt;0-15&lt;/code&gt;. This instruction performs that shift and places the index into &lt;code&gt;x11&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;    0x0088 ldr    x16, [x10, x11, lsl #3]
&lt;/pre&gt;

&lt;p&gt;This uses the index in &lt;code&gt;x11&lt;/code&gt; to load the entry from the table that &lt;code&gt;x10&lt;/code&gt; points to. The &lt;code&gt;x16&lt;/code&gt; register now contains the class of this tagged pointer.&lt;/p&gt;

&lt;pre&gt;    0x008c b      0x10
&lt;/pre&gt;

&lt;p&gt;With the class in &lt;code&gt;x16&lt;/code&gt;, we can now branch back to the main code. The code starting with offset &lt;code&gt;0x10&lt;/code&gt; assumes that the class pointer is loaded into &lt;code&gt;x16&lt;/code&gt; and performs dispatch from there. The tagged pointer handler can therefore just branch back to that code rather than duplicating logic here.&lt;/p&gt;

&lt;pre&gt;    0x0090 adrp   x10, _objc_debug_taggedpointer_ext_classes@PAGE
    0x0094 add    x10, x10, _objc_debug_taggedpointer_ext_classes@PAGEOFF
&lt;/pre&gt;

&lt;p&gt;The extended tagged class handler looks similar. These two instructions load the pointer to the extended table.&lt;/p&gt;

&lt;pre&gt;    0x0098 ubfx   x11, x0, #52, #8
&lt;/pre&gt;

&lt;p&gt;This instruction loads the extended class index. It extracts &lt;code&gt;8&lt;/code&gt; bits starting from bit &lt;code&gt;52&lt;/code&gt; in &lt;code&gt;self&lt;/code&gt; into &lt;code&gt;x11&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;    0x009c ldr    x16, [x10, x11, lsl #3]
&lt;/pre&gt;

&lt;p&gt;Just like before, that index is used to look up the class in the table and load it into &lt;code&gt;x16&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;    0x00a0 b      0x10
&lt;/pre&gt;

&lt;p&gt;With the class in &lt;code&gt;x16&lt;/code&gt;, it can branch back into the main code.&lt;/p&gt;

&lt;p&gt;That's nearly everything. All that remains is the &lt;code&gt;nil&lt;/code&gt; handler.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;code&gt;nil&lt;/code&gt; Handler&lt;/b&gt;&lt;br&gt;Finally we get to the &lt;code&gt;nil&lt;/code&gt; handler. Here it is, in its entirety.&lt;/p&gt;

&lt;pre&gt;    0x00a4 mov    x1, #0x0
    0x00a8 movi   d0, #0000000000000000
    0x00ac movi   d1, #0000000000000000
    0x00b0 movi   d2, #0000000000000000
    0x00b4 movi   d3, #0000000000000000
    0x00b8 ret
&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;nil&lt;/code&gt; handler is completely different from the rest of the code. There's no class lookup or method dispatch. All it does for &lt;code&gt;nil&lt;/code&gt; is return &lt;code&gt;0&lt;/code&gt; to the caller.&lt;/p&gt;

&lt;p&gt;This task is a bit complicated by the fact that &lt;code&gt;objc_msgSend&lt;/code&gt; doesn't know what kind of return value the caller expects. Is this method returning one integer, or two, or a floating-point value, or nothing at all?&lt;/p&gt;

&lt;p&gt;Fortunately, all of the registers used for return values can be safely overwritten even if they're not being used for this particular call's return value. Integer return values are stored in &lt;code&gt;x0&lt;/code&gt; and &lt;code&gt;x1&lt;/code&gt; and floating point return values are stored in vector registers &lt;code&gt;v0&lt;/code&gt; through &lt;code&gt;v3&lt;/code&gt;. Multiple registers are used for returning smaller &lt;code&gt;struct&lt;/code&gt;s.&lt;/p&gt;

&lt;p&gt;This code clears &lt;code&gt;x1&lt;/code&gt; and &lt;code&gt;v0&lt;/code&gt; through &lt;code&gt;v3&lt;/code&gt;. The &lt;code&gt;d0&lt;/code&gt; through &lt;code&gt;d3&lt;/code&gt; registers refer to the bottom half of the corresponding &lt;code&gt;v&lt;/code&gt; registers, and storing into them clears the top half, so the effect of the four &lt;code&gt;movi&lt;/code&gt; instructions is to clear those four registers. After doing this, it returns control to the caller.&lt;/p&gt;

&lt;p&gt;You might wonder why this code doesn't clear &lt;code&gt;x0&lt;/code&gt;. The answer to that is simple: &lt;code&gt;x0&lt;/code&gt; holds &lt;code&gt;self&lt;/code&gt; which in this case is &lt;code&gt;nil&lt;/code&gt;, so it's already zero! You can save an instruction by not clearing &lt;code&gt;x0&lt;/code&gt; since it already holds the value we want.&lt;/p&gt;

&lt;p&gt;What about larger &lt;code&gt;struct&lt;/code&gt; returns that don't fit into registers? This requires a little cooperation from the caller. Large &lt;code&gt;struct&lt;/code&gt; returns are performed by having the caller allocate enough memory for the return value, and then passing the address of that memory in &lt;code&gt;x8&lt;/code&gt;. The function then writes to that memory to return a value. &lt;code&gt;objc_msgSend&lt;/code&gt; can't clear this memory, because it doesn't know how big the return value is. To solve this, the compiler generates code which fills the memory with zeroes before calling &lt;code&gt;objc_msgSend&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That's the end of the &lt;code&gt;nil&lt;/code&gt; handler, and of &lt;code&gt;objc_msgSend&lt;/code&gt; as a whole.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;br&gt;It's always interesting to dive into framework internals. &lt;code&gt;objc_msgSend&lt;/code&gt; in particular is a work of art, and delightful to read through.&lt;/p&gt;

&lt;p&gt;That's it for today. Come back next time for more squishy goodness. Friday Q&amp;amp;A is driven by reader input, so if you have something you'd like to see discussed here, &lt;a href="mailto:mike@mikeash.com"&gt;send it in&lt;/a&gt;!&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;
</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/friday-qa-2017-06-30-dissecting-objc_msgsend-on-arm64.html</guid><pubDate>Sat, 01 Jul 2017 04:23:00 GMT</pubDate></item><item><title>More Advanced Swift Workshop, and Blog and Book Updates
</title><link>http://www.mikeash.com/pyblog/more-advanced-swift-workshop-and-blog-and-book-updates.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;More Advanced Swift Workshop, and Blog and Book Updates
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2017 06 13  16 11"
                  tags="advertisement"
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;More Advanced Swift Workshop, and Blog and Book Updates
&lt;/div&gt;
              &lt;p&gt;I'm hoping to resume a regular posting schedule soon, and I wanted to give everybody some updates.&lt;/p&gt;

&lt;p&gt;First, I'm holding two more Advanced Swift Workshops next month, one in DC on July 13th and one in New York City on July 24th. &lt;a href="https://www.eventbrite.com/e/advanced-swift-with-mike-ash-full-day-workshop-tickets-35095447428?aff=es2"&gt;Click here for the one in DC&lt;/a&gt;, and &lt;a href="https://www.eventbrite.com/e/advanced-swift-with-mike-ash-full-day-workshop-tickets-35095522653?aff=es2"&gt;here for the one in New York City&lt;/a&gt;. As with the previous ones, we'll be covering various advanced topics on Swift programming, with yours truly presenting and a small group with lots of opportunity for discussion and experimentation.&lt;/p&gt;

&lt;p&gt;Second, The Complete Friday Q&amp;amp;A Volume II is nearly ready. Not quite there yet, but the text and layout are done and it's down to some final tweaking and doing the work of actually getting it out there. Stay tuned.&lt;/p&gt;

&lt;p&gt;Last, I hope to resume regular posts in the next couple of weeks. There were some interesting things from WWDC that I want to write about, plus my usual routine of crazy topics. In particular, I did a thorough analysis of the latest implementation of &lt;code&gt;objc_msgSend&lt;/code&gt; for ARM64 for a talk that I'd like to write up, and I want to write something about Swift's new &lt;code&gt;Codable&lt;/code&gt; stuff. As always, I'm driven by reader suggestions, so if you have something from WWDC you'd like to see, or something unrelated you think would be cool, &lt;a href="mailto:mike@mikeash.com"&gt;send it in&lt;/a&gt;!&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/more-advanced-swift-workshop-and-blog-and-book-updates.html</guid><pubDate>Tue, 13 Jun 2017 16:11:00 GMT</pubDate></item><item><title>Advanced Swift Workshop in New York City
</title><link>http://www.mikeash.com/pyblog/advanced-swift-workshop-in-new-york-city.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;Advanced Swift Workshop in New York City
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2017 03 27  15 29"
                  tags="advertisement"
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;Advanced Swift Workshop in New York City
&lt;/div&gt;
              &lt;p&gt;I will be holding another one-day workshop on advanced Swift programming in New York City on May 4th. This will be much the same as my previous one in Washington in December, in a new location and with various tweaks and improvements. If you enjoy my articles and want to sharpen your Swift skills, &lt;a href="https://www.eventbrite.com/e/advanced-swift-with-mike-ash-full-day-workshop-tickets-29051962201"&gt;check it out&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I'll discuss the ins and outs of ARC and memory management, reference cycles, enums, generics, designing code to take advantage of enums and generics, pointer APIs, and interfacing with C APIs. Attendees will receive a bunch of Xcode playgrounds that illustrate everything we discuss, as well as the presentation slides.&lt;/p&gt;

&lt;p&gt;The format will be part lecture, part exercises using the playgrounds, with plenty of opportunity for discussions and personalized help. &lt;/p&gt;

&lt;p&gt;For more information, or to buy tickets, &lt;a href="https://www.eventbrite.com/e/advanced-swift-with-mike-ash-full-day-workshop-tickets-32966081437"&gt;visit the event page&lt;/a&gt;. If you know anyone who might like to come, please pass the word!&lt;/p&gt;

&lt;p&gt;Another book update, since my last workshop post included one: I've completed my final read-through and am in the middle of fixing the problems that uncovered. Then it'll be ready to go! I don't have a date for it, but it's getting close.&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/advanced-swift-workshop-in-new-york-city.html</guid><pubDate>Mon, 27 Mar 2017 15:29:00 GMT</pubDate></item><item><title>Advanced Swift Workshop in Washington, DC
</title><link>http://www.mikeash.com/pyblog/advanced-swift-workshop-in-washington-dc.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;Advanced Swift Workshop in Washington, DC
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2016 11 12  21 27"
                  tags="advertisement"
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;Advanced Swift Workshop in Washington, DC
&lt;/div&gt;
              &lt;p&gt;I will be holding a one-day workshop on advanced Swift programming in the Washington, DC area on December 12th. If you enjoy my articles and want to sharpen your Swift skills, &lt;a href="https://www.eventbrite.com/e/advanced-swift-with-mike-ash-full-day-workshop-tickets-29051962201"&gt;check it out&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I'm going to be discussing the ins and outs of ARC and memory management, reference cycles, enums, generics, designing code to take advantage of enums and generics, pointer APIs, and interfacing with C APIs. I have been building out a set of nifty Xcode playgrounds to illustrate everything, and attendees will receive a copy of them, as well as the presentation slides.&lt;/p&gt;

&lt;p&gt;The format will be part lecture, part exercises using the playgrounds, with plenty of opportunity for discussions and personalized help.&lt;/p&gt;

&lt;p&gt;If you think you might like to come, &lt;a href="https://www.eventbrite.com/e/advanced-swift-with-mike-ash-full-day-workshop-tickets-29051962201"&gt;take a look at the event on Eventbrite&lt;/a&gt;. And if you know others who might like to come, please tell them about it!&lt;/p&gt;

&lt;p&gt;In unrelated news, since I'm sure some of you are wondering, Volume II of my book is coming along slowly but surely, and I hope to get it out the door and get back to writing articles before too much longer. Stay tuned!&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/advanced-swift-workshop-in-washington-dc.html</guid><pubDate>Sat, 12 Nov 2016 21:27:00 GMT</pubDate></item><item><title>Good News, Bad News, and Ugly News
</title><link>http://www.mikeash.com/pyblog/good-news-bad-news-and-ugly-news.html</link><description>&lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;Good News, Bad News, and Ugly News
&lt;/title&gt;
              &lt;blogattrs
                  postdate="2016 06 01  11 48"
                  tags="book"
                  /&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;!-- enable-comments --&gt;
              &lt;div class="blogtitle"&gt;Good News, Bad News, and Ugly News
&lt;/div&gt;
              &lt;p&gt;The good news is that I'm officially restarting work on The Complete Friday Q&amp;amp;A: Volume II. I got partway into it a while ago and ran out of steam. The restarted edition includes all posts made since then, making it pretty massive. I can't commit to a specific timeframe, but I hope that it will be a few months at most before I have it out. There may be opportunities for reader involvement in checking and polishing it, so watch this space.&lt;/p&gt;

&lt;p&gt;The bad news is that I can't keep up with new blog posts at the same time. It just hasn't worked out. That's part of why I've been quiet lately, and so I'm suspending regular posts for the duration. I may make occasional irregular posts in the meantime (I'm sure WWDC will have something worth discussing) and I'll resume a more regular schedule once the book is done.&lt;/p&gt;

&lt;p&gt;The ugly news is... there is no ugly news, that was just a pointless movie reference. Don't panic!&lt;/p&gt;

              &lt;/body&gt;
              &lt;/html&gt;
</description><author>Mike Ash</author><guid isPermaLink="true">http://www.mikeash.com/?page=pyblog/good-news-bad-news-and-ugly-news.html</guid><pubDate>Wed, 01 Jun 2016 11:48:00 GMT</pubDate></item></channel></rss>
