|
1 | 1 | # swift-extras-uuid |
2 | 2 |
|
3 | | -A reimplementation of UUID in Swift without the use of Foundation. |
| 3 | +A reimplementation of UUID in Swift without the use of Foundation or any other dependency. |
| 4 | + |
| 5 | +[](https://swift.org/download/) |
| 6 | +[](https://github.com/fabianfett/swift-extras-uuid/actions) |
| 7 | +[](https://codecov.io/gh/fabianfett/swift-extras-uuid) |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +### Motivation |
| 12 | + |
| 13 | +Foundation on non Darwin platforms (Linux, Windows) has often worse performance and/or missing features compared to its Darwin counterpart. That's why I like to limit the use of Foundation classes and structs to the absolute minimum when writing server side or cross platform Swift code. This package offers the same UUID functionality that Foundation provides, without actually using Foundation. The performance is slightly better on Linux compared to the Foundation implementation and comparable on macOS. |
| 14 | + |
| 15 | +### How to use it? |
| 16 | + |
| 17 | +Add dependency to your `Package.swift`. |
| 18 | + |
| 19 | +```swift |
| 20 | + dependencies: [ |
| 21 | + .package(url: "https://github.com/fabianfett/swift-extras-uuid.git", .upToNextMajor(from: "0.1.0")), |
| 22 | + ], |
| 23 | +``` |
| 24 | + |
| 25 | +Add `ExtrasUUID` to the target you want to use it in. |
| 26 | + |
| 27 | +```swift |
| 28 | + targets: [ |
| 29 | + .target(name: "MyFancyTarget", dependencies: [ |
| 30 | + .product(name: "ExtrasUUID", package: "swift-extras-uuid"), |
| 31 | + ]) |
| 32 | + ] |
| 33 | +``` |
| 34 | + |
| 35 | +Import the library and use it. This package's UUID is prefixed with an X to avoid naming conflicts in cases where you import Foundation in the same file. |
| 36 | + |
| 37 | +```swift |
| 38 | +import ExtrasUUID |
| 39 | + |
| 40 | +let uuid = XUUID() |
| 41 | +``` |
| 42 | + |
| 43 | +### Interop with Foundation |
| 44 | + |
| 45 | +You can easily convert between a Foundation UUID and an swift-extras XUUID. |
| 46 | + |
| 47 | +From Foundation: |
| 48 | + |
| 49 | +```swift |
| 50 | +let fduuid = UUID() |
| 51 | +let xuuid = XUUID(uuid: fduuid.uuid) |
| 52 | +``` |
| 53 | + |
| 54 | +To Foundation: |
| 55 | +```swift |
| 56 | +let xuuid = XUUID() |
| 57 | +let fduuid = UUID(uuid: xuuid.uuid) |
| 58 | +``` |
| 59 | + |
| 60 | +### Performance |
| 61 | + |
| 62 | +To run simple performance tests, check out this repository and run: |
| 63 | + |
| 64 | +``` |
| 65 | +swift run -c release --enable-test-discovery |
| 66 | +``` |
| 67 | + |
| 68 | +The performance tests will |
| 69 | + |
| 70 | +- create 1m uuids |
| 71 | +- parse 1m uuids |
| 72 | +- create 1m uuid strings |
| 73 | +- compare 10k uuids |
| 74 | + |
| 75 | +#### Linux - Swift 5.3 |
| 76 | + |
| 77 | +| | Creating Random | Parsing | Stringify | Comparing | |
| 78 | +|:--|:--|:--|:--|:--| |
| 79 | +| Foundation | 3.94s | 1.01s | 0.8s | 0.16s | |
| 80 | +| ExtrasUUID | 2.06s | 0.34s | 0.16s | 0.13s | |
| 81 | +| Speedup | ~2x | ~3x | ~5x | ~1x | |
| 82 | + |
0 commit comments