New Codable now lives in swift-serialization

Hello again Swift Community! I’ve got some news about the New Codable work I’ve been posting about in “New Codable” prototype available for feedback: it now has a new name, and its own official repository, independent from swift-foundation, called swiftlang/swift-serialization (GitHub - swiftlang/swift-serialization · GitHub).

For those not familiar, this project exists to satisfy the demonstrable need for an alternative to Codable that has the following qualities:

  • More efficient in CPU and memory usage than Codable
  • Compatible with Embedded Swift
  • Provides code generation via macros with improved customization options and less need to write manual implementations
  • Enables format-specific interfaces instead of forcing a limited set of primitive data types

For the initial version, 0.0.1, the code from Foundation’s experimental/new-codable branch has been imported as-is, complete with commit history. Some notes:

  • The library has no dependency on Foundation. It needs only the standard library, swift-collections, and swift-syntax for the macros.
  • It requires Swift 6.4 or later, and OS 27 on Apple platforms.
  • We intend to support a wide range of platforms and environments, including Darwin, Linux, Windows, Android, WebAssembly, and Embedded Swift.
  • There is now a separate Benchmarks package, built on package-benchmark, which currently compares the new JSON implementation against Foundation’s JSONEncoder and JSONDecoder.

To try it out, add it as a package dependency:

.package(
	url: "https://github.com/swiftlang/swift-serialization.git",
	.upToNextMinor(from: "0.0.1")
)

Then add the dependency to your target:

.target(
    name: "MyTarget",
    dependencies: [
        .product(name: "NewCodable", package: "swift-serialization")
    ]
)

For the moment, the module is still called NewCodable (and NewCodableMacros). I plan to rename these in 0.1.0, so please expect that.

Since this package doesn’t depend on Foundation in any way, the Date and Data conformances and other Foundation-specific features that were available on the experimental/new-codable branch are not included. These will return some time in the future, either by making swift-foundation depend on swift-serialization, or via some other package.

Please excuse our dust. This is very much a work in progress. We welcome your pull requests to get the project into shape. I've already begun the process of proposing some major naming changes to align with the "swift-serialization" nomenclature, as well as some big API improvements.

Your time, consideration, and feedback are greatly appreciated, and are in fact critical to this project’s success. We hope the new dedicated repository will facilitate adoption, experimentation, discussion, and evolution of these APIs.

Thank you!

29 Likes

ヾ( ˃ᴗ˂ )◞ • *✰ hooray, an early Christmas present!

I have been waiting for this day, may the new package live long and prosper.

4 Likes

This is great to see! Having a separate repo will allow us to meaningfully adopt and provide feedback.

This part seemed interesting to me. Since we are in a package now we should be able to just add a package trait that adds these conformances conditionally right? That will allow use-cases that don't want Foundation to work and allow others to opt-in. We have done the same approach in a few packages around the ecosystem already.

2 Likes

This implies swift-collections will be lowered into the toolchain, no?

I did ask for it here but it was not on my Christmas bingo card!
Thank you @kperryua, will give it a try later in penny-bot and what not.

2 Likes

We can look into that approach! TBH, I wasn't sure if traits allowed conditional package dependencies in a way that would fully satisfy those that don't need Foundation in their project. It looks like they do though, so that could work well.

However, if Foundation did want to depend on swift-serialization in the future for whatever reason, I'm unclear if this using approach would block that.

1 Like

I don't mean to imply that at all. The project's dependency on swift-collections is quite minimal right now and could easily be replaced. If it isn't already, it should be using the stdlib's UniqueArray. The OrderedDictionary usage might actually be more correct as a simple array of tuples.

2 Likes

Hi,

Exciting, I’ve been waiting for this - really appreciate the embedded support! I had a brief look, some minor questions:

I was wondering whyfunc visitBytes(_ array: [UInt8]) and similar methods aren’t given a _ bytes: RawSpan parameter instead?

Also, could the array coding strategies perhaps allow any type conforming to Iterable or at least just Sequence? I guess, the encoding strategy just needs some type that yields sequential access to its elements, so I’m not seeing atm why that strategy needs to be constrained to solely Arrays.

Furthermore, would it perhaps make sense to use a UniqueArray in the primitive enum? I don’t think we really need the COW stuff Array has - or is it that there’s no way to convert a UniqueArray into an Array without copying its contents (given that it’s likely people will want an Array to be decoded, so I’d understand Array being the default).

1 Like

Some rough benchmarks put adopting it in Vapor with a 20-40% improvement in performance for decoding JSON/url encoded forms with various iterations. And that appears to scale, decoded a JSON array of 100 objects take instructions from 4 million to 1.7 and allocations from 1557to 336. Multipart also seems to do well with encoding instructions going from 500k to 93k and allocations, which were already very good going from 72 to 24. Some nice improvements! Just a plain switch, no real performance investigations yet

4 Likes

Best thing in 2016