# Best way to initialize a constant UUID?

**URL:** <https://forums.swift.org/t/best-way-to-initialize-a-constant-uuid/75253>\
**Category:** Using Swift\
**Tags:** foundation, uuid\
**Created:** [October 9, 2024, 1:55pm UTC](https://forums.swift.org/t/best-way-to-initialize-a-constant-uuid/75253 "2024-10-09T13:55:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbrk24](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bbrk24/32/27370_2.png) [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Post date:** [October 9, 2024, 1:55pm UTC](https://forums.swift.org/t/best-way-to-initialize-a-constant-uuid/75253/1 "2024-10-09T13:55:34Z")

</div>

I'm trying to strike a balance between legibility and efficiency here.

I was looking at the UUID initializers in Godbolt, and there's basically only two: `init(uuid:)` takes a tuple of 16 ints, and `init(uuidString:)` takes a string. (There's also the decodable `init(from:)` but I can't imagine that'd be better in any conceivable way here.)

`init(uuidString:)` is definitely the more developer-friendly option, as you can just copy-paste the UUID from (and to) wherever. However, even when the string is a string literal and the call is immediately force-unwrapped, this still creates a runtime call to a UUID-string parsing function (`$s20FoundationEssentials4UUIDV10uuidStringACSgSSh_tcfC`), and then moves the result into the UUID object one byte at a time (using different registers for better ILP).

`init(uuid:)` is far worse for developer productivity, but it seemed like it could be a more efficient alternative. However, even that doesn't put the numbers directly in the UUID object. Instead, it compiles to something like this:

```cpp
const char tuple[] = { 0x01, 0x02, ..., 0x10 };
char uuid[16] = {0};

int main() {
  memcpy(uuid, tuple, 16);
  // ...
}

```

I suppose I could write a `#UUIDLiteral` macro that takes a `StaticString` argument and compiles to a call to `UUID.init(uuid:)`, but that seems like unnecessary effort.

Is the overhead from `init(uuidString:)` generally minimal? Or is there a better way to initialize a constant UUID?

---

<div class="post-metadata">

**Author:** ![benrimmington](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benrimmington/32/695_2.png) [@benrimmington](https://forums.swift.org/u/benrimmington)\
**Post date:** [October 9, 2024, 5:02pm UTC](https://forums.swift.org/t/best-way-to-initialize-a-constant-uuid/75253/2 "2024-10-09T17:02:33Z")

</div>

The [initializers](https://github.com/swiftlang/swift-foundation/blob/swift-6.0.1-RELEASE/Sources/FoundationEssentials/UUID.swift#L42-L59) and [parse](https://github.com/swiftlang/swift-foundation/blob/swift-6.0.1-RELEASE/Sources/_FoundationCShims/uuid.c#L224-L241) [functions](https://github.com/apple-oss-distributions/xnu/blob/xnu-11215.1.10/libkern/uuid/uuid.c#L141-L159) are open source.

> [@bbrk24](#):
>
> `init(uuid:)` is far worse for developer productivity, but it seemed like it could be a more efficient alternative. However, even that doesn't put the numbers directly in the UUID object.

The implementation is `self.uuid = uuid`, but it doesn't use `@frozen` or `@inlinable`, and apparently Swift's calling convention will destructure the tuple into 16 arguments. (Possibly the stored property can use a fixed-size array type in the future.)

> [@bbrk24](#):
>
> Is the overhead from `init(uuidString:)` generally minimal?

I don't know — it uses `sscanf` to initialize the tuple elements.

---

<div class="post-metadata">

**Author:** ![nikolai.ruhe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nikolai.ruhe/32/23230_2.png) [@nikolai.ruhe](https://forums.swift.org/u/nikolai.ruhe)\
**Post date:** [October 9, 2024, 9:18pm UTC](https://forums.swift.org/t/best-way-to-initialize-a-constant-uuid/75253/3 "2024-10-09T21:18:28Z")

</div>

The best of both worlds would probably be a macro that parses the string at compile time and emit code using the tuple initializer.

As a nice side effect you would get compile time errors for malformed UUID strings.

On the other hand, I’m not sure I can imagine a project that uses so many hardcoded UUIDs that the effort is worth it.
