How can I create an inline array inside a struct?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How can I create an inline array inside a struct?

Jens Alfke
I want to define a struct that contains a fixed-size blob of data — for example, a SHA-1 digest, which is 20 bytes long. Is there any way to define this in Swift such that the data lives in the struct, instead of being a separate heap block?

In other words, I want the Swift equivalent of
typedef struct {
char bytes[20];
} SHA1Digest;

I couldn’t find anything in either official Swift book about working with C arrays. The closest thing is the section “Pointers” in Using Swift With Cocoa, but it only seems to talk about calling other Swift code.

As far as I can tell, to get a block of n bytes (where n is known at compile time) I have to use an Array. Which implies creating a separate internal object to store the array contents. This isn’t very inefficient if I only have a couple of these, but if I’ve got millions of them, or need to process them at a very high rate (i.e. in some numeric or crypto code) it can be a big bottleneck — this is the sort of reason why people fall back to C or C++ when doing computation-intensive code.

Go can handle this just fine (a fixed-size array, as opposed to a slice, needn’t be heap-allocated, and is copied by value) and I was imagining Swift could too, until I tried to create such a thing today and got stuck.

—Jens

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/FE6014F6-BFEA-49A7-AC39-4F5101E3F369%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How can I create an inline array inside a struct?

Brent Royal-Gordon-2
A tuple is fixed-size and usually inline. (And, in fact, that's how that struct would be translated into Swift!) You can't index into a tuple with an integer, of course, but if you're willing to write a giant switch statement or something to do that, that might do the trick.

On Sun, Jun 7, 2015 at 4:16 PM Jens Alfke <[hidden email]> wrote:
I want to define a struct that contains a fixed-size blob of data — for example, a SHA-1 digest, which is 20 bytes long. Is there any way to define this in Swift such that the data lives in the struct, instead of being a separate heap block?

In other words, I want the Swift equivalent of
typedef struct {
char bytes[20];
} SHA1Digest;

I couldn’t find anything in either official Swift book about working with C arrays. The closest thing is the section “Pointers” in Using Swift With Cocoa, but it only seems to talk about calling other Swift code.

As far as I can tell, to get a block of n bytes (where n is known at compile time) I have to use an Array. Which implies creating a separate internal object to store the array contents. This isn’t very inefficient if I only have a couple of these, but if I’ve got millions of them, or need to process them at a very high rate (i.e. in some numeric or crypto code) it can be a big bottleneck — this is the sort of reason why people fall back to C or C++ when doing computation-intensive code.

Go can handle this just fine (a fixed-size array, as opposed to a slice, needn’t be heap-allocated, and is copied by value) and I was imagining Swift could too, until I tried to create such a thing today and got stuck.

—Jens

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/FE6014F6-BFEA-49A7-AC39-4F5101E3F369%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/CAEeRk-beJqCw9HNuMgz46rDAFfriEoymsCQoy8ato3VyDAYtug%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How can I create an inline array inside a struct?

Jens Alfke

On Jun 15, 2015, at 4:13 PM, Brent Royal-Gordon <[hidden email]> wrote:

A tuple is fixed-size and usually inline. (And, in fact, that's how that struct would be translated into Swift!) You can't index into a tuple with an integer, of course, but if you're willing to write a giant switch statement or something to do that, that might do the trick.

Indexing doesn’t matter; these values are basically opaque blobs, they just need to be handed to/from the underlying APIs like CC_SHA1( ). So the question is, can I in Swift get the address of such a tuple as an UnsafeMutablePointer?

—Jens

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/042EF13A-36D0-4110-94DA-E66822DAFDA7%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How can I create an inline array inside a struct?

Brent Royal-Gordon-2
Playgrounds are acting up for me right now, but you can pass a tuple to withUnsafeMutablePointer...

On Mon, Jun 15, 2015 at 4:38 PM Jens Alfke <[hidden email]> wrote:

On Jun 15, 2015, at 4:13 PM, Brent Royal-Gordon <[hidden email]> wrote:

A tuple is fixed-size and usually inline. (And, in fact, that's how that struct would be translated into Swift!) You can't index into a tuple with an integer, of course, but if you're willing to write a giant switch statement or something to do that, that might do the trick.

Indexing doesn’t matter; these values are basically opaque blobs, they just need to be handed to/from the underlying APIs like CC_SHA1( ). So the question is, can I in Swift get the address of such a tuple as an UnsafeMutablePointer?

—Jens

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/CAEeRk-bfrskPLpgj6Q%3D5c1KVkxagfX4cWh24t7UZPNdO00ukng%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.