Archive for August 14, 2017

Monday, August 14, 2017

Swift.Unmanaged

Mike Ash:

In order to work with C APIs, we sometimes need to convert Swift object references to and from raw pointers. […] You can do this with unsafeBitCast, although I strongly recommend against it.

[…]

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

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

[…]

To retrieve a reference from C into Swift code, you create an Unmanaged 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.

Previously: Swift 3.0 Unsafe World.

PLRelational

Chris Campbell:

While working on the next major version of VoodooPad, an observation was made: user interfaces are basically just bidirectional transformation functions, taking data from some source and presenting it to the user, and vice versa. It sounds so simple when boiled down like that, but the reality of app development is a different story, often filled with twisty, error-prone UI code, even with the help of functional programming aids (e.g. Rx and its ilk). A question then emerged: can we build a library that allows us to express those transformations in a concise way, with an eye towards simplifying app development?

PLRelational is our (exploratory) answer to that question. At its core, PLRelational is a Swift framework that allows you to:

  • declaratively express relationships using relational algebra
  • asynchronously query and update those relations
  • observe fine-grained deltas when a relation’s data has changed

Update (2017-08-28): Mike Ash:

This article will give an overview of the basics of relational algebra in a way that programmers can easily understand, aimed at explaining the foundations that PLRelational is built on. Terminology will match what PLRelational uses.

Update (2017-08-31): Chris Campbell:

Before looking into all the goodies that PLRelational and PLRelationalBinding have to offer, it helps to first understand how the core Relation classes compute and deliver changes.

Update (2017-09-11): Mike Ash:

We’ve been talking a lot lately about PLRelational and all the fancy stuff it does. However, we’ve been glossing over a fundamental part of it: how it actually stores data. After all, PLRelational is a data persistence framework at its core.

A Brief History of the UUID

Rick Branson (via Hacker News):

[Apollo’s] NCS introduced the concept of the UID (Universal IDentifier), which served as the unique primary identity for entities. UIDs are 64-bit numbers that combine a monotonic clock with a unique host ID permanently embedded in the hardware of all of their workstations. Under this scheme, identifiers could be generated thousands of times per second at each host and remain globally unique for all time with no scaling bottleneck. The only point of coordination was at Apollo’s factories — where the machines were permanently branded with their respective identifiers.

[…]

NCA introduced UUIDs, which built on the UID design, but accommodated a broader range of vendors by extending the number space to 128-bits. Thus the UUID was born. This concept was so useful that even after NCA became a distant memory and RPC fell out of fashion, the UUID remained popular, eventually being standardized by ISO, IETF, and ITU.

[…]

As the untrustworthy Internet became the dominant networking platform, UUID generation which depended on trust became obsolete. All of these concerns have lead most to abandon leveraging hardware identifiers in UUIDs.

tytso:

Later on Paul went on to Microsoft, and I’m fairly certain that it was due to Paul that Microsoft adopted the OSF DCE RPC layer for its internal use, and UUID’s started being used extensively inside Microsoft. UUID’s also got used in Intel’s EFI specification for the GPT partition table, although somewhere along the way they got renamed “Globally Unique ID’s” --- it’s the same spec, though.

[…]

As far as uuidd is concerned, the reason why it exists is because a certain very large Enterprise Resource Planning system was using libuuid to generate uuid’s for its objects, and it needed to create them very, very quickly so they can initalize the customer’s ERP database in finite time. They were also using the time-based UUID’s, with the UUID stored in the database with the bytes cleverly rearranged so the time bits would be stored in the LSB, and the Ethernet MAC address would be in the MSB, so that a database using a B-tree (plus prefix key compression) for its indexing would be able to very efficiently index the UUID’s. This is similar to k-ordering trick that Flake was using, but this very large enterprise planning company was doing in 2007, five years before team at Boundary came up with Flake, and they were doing it using standard UUID’s, but simply storing the Time-based UUID bytes in a different order. (I believe they were also simply storing the ID in binary form, instead of base-62 encoding, since if you’re going to have jillions of objects in your ERP database, you want them to be as efficient as possible.)

Anyway, a certain Linux distribution company contacted me on behalf of this very large Enterprise Resource Planning company, and we came up with a scheme where the uuidd daemon could issue blocks of time-based UUID’s to clients, so we could amortize the UUID generation over blocks of 50 or 100 UUID’s at a time. (This ERP was generating a huge number of UUID’s.) I did it as a freebie, because I was tickled pick that libuuid was such a critical part of a large ERP system, and it wasn’t that hard to implement the uuidd extension to libuuid.

Classic Game Postmortem: Oregon Trail

Game Developers Conference (via Avi Drissman):

In this GDC 2017 postmortem, Oregon Trail creator Don Rawitsch sets off on a journey to explore the development of this classic educational game that took the world by storm.