• 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: Efficiency in SmallSockets
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Efficiency in SmallSockets


  • Subject: Re: Efficiency in SmallSockets
  • From: Paul Ferguson <email@hidden>
  • Date: Mon, 16 Sep 2002 23:10:56 -0700

On Monday, September 16, 2002, at 03:05 PM, Anthony Duer wrote:

Hi, I'm having a trouble with efficiency in SmallSockets. My problem occurs with a latency in when text arrives and the speed in which I can process it. Currently I'm developing a text-based game client which connects to a game server via a simple TCP/IP connection. My problem is that when I go to read from the socket, I first have to put the data from the socket into a NSData, read from the NSData into a NSString, then insert the NSString into a NSTextView. Under heavy load (10-20 lines/second) this seems to become very bogged down, taking a few seconds to 'catch up' to where the current status is. Here's my current working code, the function is run on a seperate Thread:

while([mySocket isConnected])
{
if([mySocket isReadable])
{
[mySocket readData: myData];
while([myData containsString: @"\n"])
{
line = [[NSString alloc] initWithData: [myData getBytesUpToString: @"\n"] encoding: NSUTF8StringEncoding];
[mainText setEditable: YES];
[mainText insertText: line];
[mainText setEditable: NO];
[line release];
[mainText moveToEndOfDocument:mainText];
[mainText scrollRangeToVisible:[mainText selectedRange]];
}
}
else
{
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.05]];
}
}

A couple observations:

(1) Don't sleep after the read, just use blocking mode (which is the default) to wait for data to arrive rather than calling isReadable. Use an NS_DURING/NS_HANDLER block to catch disconnection and other exceptions. If you're running your network code in a separate thread as you indicate (and as you should), this is much more efficient; the sleepUntilDate call is probably contributing substantially to your performance problems.

(2) Make sure to reserve enough space in myData (using dataWithCapacity: or initWithCapacity:) to avoid it having to resize during readData.

(3) You may have problems modifying the NSTextView in a thread other than the main UI thread. You should consider using something like Toby Paterson's InterThreadMessaging or other code to pass the NSData to the main thread.

(4) You should insert text into the view by manipulating the NSTextView's textStorage object directly, rather than using insertText. See NSTextStorage for more info (and the key point to remember is that NSTextStorage is a subclass of NSMutableString).

(5) If your data is already text, you can probably just stuff the whole thing into the text storage at once, unless there's some reason you need to first split it into separate lines as you show in your while() loop--but I'm not sure what you may be doing to the incoming text.

Hope this helps,

<fergy/>
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Efficiency in SmallSockets (From: Anthony Duer <email@hidden>)

  • Prev by Date: Re: [newbie] 10.2 Build problems
  • Next by Date: NSButton Colour?
  • Previous by thread: Re: Efficiency in SmallSockets
  • Next by thread: Dynamic Menu Items
  • Index(es):
    • Date
    • Thread