# \[Discussion\] Bag of Bytes Types

**URL:** <https://forums.swift.org/t/discussion-bag-of-bytes-types/86983>\
**Category:** Discussion\
**Tags:** data, pointers, bytes\
**Created:** [May 29, 2026, 4:34pm UTC](https://forums.swift.org/t/discussion-bag-of-bytes-types/86983 "2026-05-29T16:34:25Z")\
**Posts on this page:** 1\
**Showing post:** 34

<div class="post-metadata">

**Author:** ![al45tair](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/al45tair/32/17985_2.png) [@al45tair](https://forums.swift.org/u/al45tair)\
**Post date:** [July 3, 2026, 11:21am UTC](https://forums.swift.org/t/discussion-bag-of-bytes-types/86983/34 "2026-07-03T11:21:49Z")

</div>

> [@jmschonfeld](#):
>
> - **What bag of bytes types do you use in your code? Do you use different types for different purposes?**

`Array<UInt8>` is very useful when you need a byte container that can grow (we don't have `reallocate` on `Unsafe(Mutable)(Raw)BufferPointer`). Span types are very useful for _interfaces_ but need to be backed by some actual container type; very often that is either `Array<UInt8>` or `Unsafe(Mutable)(Raw)BufferPointer`.

(It's worth saying that while reallocation-in-place is often frowned upon by more theoretical types, in practice it's often a big performance win being able to do that.)

> [@jmschonfeld](#):
>
> - **Why did you choose to use these types / what aspects led to these decisions?**

`Data` requires importing `Foundation`, which isn't true of the other types (this means that there are contexts where `Data` cannot be used, like in the Swift runtime code itself).

There's no equivalent of `withUnsafeTemporaryAllocation` for `Data`.

> [@jmschonfeld](#):
>
> - **In general, what are important characteristics of bag of bytes types for your use cases?**

Being able to append bytes to it (and resize it) is useful in some situations. Being able to easily convert to `Span` types is also handy. The ability to read arbitrary data out of it, likewise — sometimes you have a bag of bytes and want to read a word or even a struct defined in C out of the raw memory.

Creating temporary storage of this kind is often handy when interacting with C code, hence `withUnsafeTemporaryAllocation`.

> [@jmschonfeld](#):
>
> - **What does the flow of your bytes look like through your application or API?**

Varies depending on what you're talking about. The Swift Runtime module includes code for parsing ELF, PE-COFF and DWARF that you could look at. There are also applications where it makes sense to use a bag-of-bytes as a ring buffer while doing I/O, or as the backing for another data type (like a `String`).

---

_[View the full topic](https://forums.swift.org/t/discussion-bag-of-bytes-types/86983)._
