• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Need advice about [NSInputStream read:maxLength:] performance.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Need advice about [NSInputStream read:maxLength:] performance.


  • Subject: Re: Need advice about [NSInputStream read:maxLength:] performance.
  • From: Jens Alfke <email@hidden>
  • Date: Wed, 09 Sep 2015 15:30:51 -0700


On Sep 9, 2015, at 2:07 PM, Motti Shneor <email@hidden> wrote:

I need reading messages off the stream, each prefixed by 4 bytes containing its size. I was wondering if it was advisable to first read just the size (4 bytes) then read the rest of the message, or maybe it was better to have a big buffer into which to “drain” the stream, then parse my messages off the buffer? 

The first approach won’t work, because the amount of data received is unpredictable. For instance you might receive only three bytes, which isn’t enough to parse the data size. Or you might be able to read the data size, but then try to read a 100000-byte message and get only 1500 bytes of it.

The usual approach when parsing data asynchronously from a network stream is to keep a variable-size buffer of unparsed data. When data arrives, you read all of it and append it to the buffer; then you try to parse the data in the buffer. If there’s not enough to parse yet, you give up till there’s more; or if you parse some data, you remove it from the start of the buffer.

So in your case you’d first check whether the buffer has at least four bytes; if so, you look at the length, and check whether that many more bytes are available. If they are, you read the whole message and remove it from the buffer.

An NSMutableData object works well as a buffer, if you’re not too concerned about high performance. The fastest implementation is probably some sort of custom ring buffer.

—Jens
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Need advice about [NSInputStream read:maxLength:] performance.
      • From: Motti Shneor <email@hidden>
    • Re: Need advice about [NSInputStream read:maxLength:] performance.
      • From: Adam Kaplan <email@hidden>
References: 
 >Need advice about [NSInputStream read:maxLength:] performance. (From: Motti Shneor <email@hidden>)

  • Prev by Date: Re: Need advice about [NSInputStream read:maxLength:] performance.
  • Next by Date: backgrounding a udp socket
  • Previous by thread: Re: Need advice about [NSInputStream read:maxLength:] performance.
  • Next by thread: Re: Need advice about [NSInputStream read:maxLength:] performance.
  • Index(es):
    • Date
    • Thread