0

How can I force NSMutableData to reserve contiguous memory, in the same section? I have a fair amout of data arriving piece by piece, and the performance is not satisfactory. The total size of my data will be constant.

jscs
  • 63,694
  • 13
  • 151
  • 195
Jose Alberto Diaz
  • 155
  • 1
  • 3
  • 12

1 Answers1

2

NSMutableData should store all data contiguously. Internally it is using a "stretchy" buffer resized with realloc. You can create with [NSMutableData dataWithLength:];

Why not use an NSData if the size is constant?

Also why are you worrying about NSMutableData performance. Did instruments profile show a performance problem?

Justin Meiners
  • 10,754
  • 6
  • 50
  • 92
  • Hi @Justin, Ok I´ll explain a little more, the buffer size is constant but I write to this by parts, the data comes from an external accessory so each time I receive data I need to copy it into the rigth position. I´m worry about performance since I receive 64KB of information from the accessory and it is taking 3+ seconds (for me it is a lot) so I´m trying to optimize the code. – Jose Alberto Diaz Jul 11 '13 at 16:53
  • @JoseAlbertoDiaz write into the data using memcpy([data mutableBytes] + offset, source, size); or maybe the more cocoa friendly replaceBytesInRange:withBytes: – Justin Meiners Jul 11 '13 at 17:05