When does `Data.init?(capacity:)` fail?

Hello there, I’m trying to optimize my code and reduce copying from different buffers into a new one.

I thought I just create a Data value with enough capacity and write directly into it. My problem is that Data.init?(capacity:) can fail, but why and when?

Can someone explain this behavior to me?

I’m sending data via TCP sockets and when recn function write directly into a Data struct.

···

--
Adrian Zubarev
Sent with Airmail

This <http://article.gmane.org/gmane.comp.lang.swift.user/1702&gt; might be
relavant. Basically, Data’s init will fail if memory can’t be allocated for
it.

···

On Fri, Jun 17, 2016 at 11:38 AM Adrian Zubarev via swift-users < swift-users@swift.org> wrote:

Hello there, I’m trying to optimize my code and reduce copying from
different buffers into a new one.

I thought I just create a Data value with enough capacity and write
directly into it. My problem is that Data.init?(capacity:) can fail, but
why and when?

Can someone explain this behavior to me?

I’m sending data via TCP sockets and when recn function write directly
into a Data struct.

--
Adrian Zubarev
Sent with Airmail

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
-Saagar Jha

As I understand it, that’s not an error in the ‘try’ sense of the word. If that failure happens, it’s a catastrophic issue which should bring down the application.

So the initialiser shouldn’t be failable; you’re right. File a bug at bugs.swift.org.

Karl

···

On 18 Jun 2016, at 06:06, Saagar Jha via swift-users <swift-users@swift.org> wrote:

This <http://article.gmane.org/gmane.comp.lang.swift.user/1702&gt; might be relavant. Basically, Data’s init will fail if memory can’t be allocated for it.

On Fri, Jun 17, 2016 at 11:38 AM Adrian Zubarev via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
Hello there, I’m trying to optimize my code and reduce copying from different buffers into a new one.

I thought I just create a Data value with enough capacity and write directly into it. My problem is that Data.init?(capacity:) can fail, but why and when?

Can someone explain this behavior to me?

I’m sending data via TCP sockets and when recn function write directly into a Data struct.

--
Adrian Zubarev
Sent with Airmail

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users
--
-Saagar Jha
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Not quite:

Swift’s policy on memory allocation failure is that fixed-size object
allocation is considered to be a runtime failure if it cannot be handled.
OTOH, APIs that can take a variable and arbitrarily large amount to
allocate should be failable. NSData falls into the later category.

Source <http://article.gmane.org/gmane.comp.lang.swift.user/1709&gt;

···

On Sun, Jun 19, 2016 at 10:00 AM Karl <razielim@gmail.com> wrote:

As I understand it, that’s not an error in the ‘try’ sense of the word. If
that failure happens, it’s a catastrophic issue which should bring down the
application.

So the initialiser shouldn’t be failable; you’re right. File a bug at
bugs.swift.org.

Karl

On 18 Jun 2016, at 06:06, Saagar Jha via swift-users < > swift-users@swift.org> wrote:

This <http://article.gmane.org/gmane.comp.lang.swift.user/1702&gt; might be
relavant. Basically, Data’s init will fail if memory can’t be allocated
for it.

On Fri, Jun 17, 2016 at 11:38 AM Adrian Zubarev via swift-users < > swift-users@swift.org> wrote:

Hello there, I’m trying to optimize my code and reduce copying from
different buffers into a new one.

I thought I just create a Data value with enough capacity and write
directly into it. My problem is that Data.init?(capacity:) can fail, but
why and when?

Can someone explain this behavior to me?

I’m sending data via TCP sockets and when recn function write directly
into a Data struct.

--
Adrian Zubarev
Sent with Airmail

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
-Saagar Jha
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--

-Saagar Jha

I’ve got an answer on Twitter for that behavior: https://twitter.com/phausler/status/743927492096851969

I’ve anyone like me needs Data to have a specific capacity and still be non-optional, here is how I build a workaround (Data and Data.Deallocator aren’t fully implemented yet, thats why I’m using a custom deallocator rather then .free):

let capacity = 42 // or get the value somehow

let dataBufferPointer = UnsafeMutablePointer<UInt8>(allocatingCapacity: capacity)
        dataBufferPointer.initialize(with: 0, count: capacity)
         
// do some work and mutation to the data here

let deallocator = Data.Deallocator.custom {
             
    (buffer, _) in
             
    free(buffer)
}
         
// this initializer says you should not mutate that data after constructing it
let data = Data(bytesNoCopy: dataBufferPointer, count: capacity, deallocator: deallocator)

···

--
Adrian Zubarev
Sent with Airmail

Am 19. Juni 2016 um 19:27:21, Saagar Jha (saagarjha28@gmail.com) schrieb:

Not quite:

Swift’s policy on memory allocation failure is that fixed-size object allocation is considered to be a runtime failure if it cannot be handled. OTOH, APIs that can take a variable and arbitrarily large amount to allocate should be failable. NSData falls into the later category.
Source

On Sun, Jun 19, 2016 at 10:00 AM Karl <razielim@gmail.com> wrote:
As I understand it, that’s not an error in the ‘try’ sense of the word. If that failure happens, it’s a catastrophic issue which should bring down the application.

So the initialiser shouldn’t be failable; you’re right. File a bug at bugs.swift.org.

Karl

On 18 Jun 2016, at 06:06, Saagar Jha via swift-users <swift-users@swift.org> wrote:

This might be relavant. Basically, Data’s init will fail if memory can’t be allocated for it.

On Fri, Jun 17, 2016 at 11:38 AM Adrian Zubarev via swift-users <swift-users@swift.org> wrote:
Hello there, I’m trying to optimize my code and reduce copying from different buffers into a new one.

I thought I just create a Data value with enough capacity and write directly into it. My problem is that Data.init?(capacity:) can fail, but why and when?

Can someone explain this behavior to me?

I’m sending data via TCP sockets and when recn function write directly into a Data struct.

--
Adrian Zubarev
Sent with Airmail

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users
--
-Saagar Jha
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
-Saagar Jha