Re: Mixing Obj-C and C methods

2013-08-07 Thread Maxthon Chan
My common way of handling this would be NSNotificationCenter. It is a singleton 
so I am always sure that it is there, and I can wrap all parameters into the 
userInfo dictionary.

Sent from my iPhone

 On 2013年7月30日, at 21:19, KappA rejek...@gmail.com wrote:
 
 I sometimes just access my objc-objects from a C thread-proc via the
 AppDelegate (providing there's a trail to the object I need, which there
 usually is)... If the callback void pointer parameter isn't being used for
 something else, you can simply cast the object in there... or if you need
 multiple parameters you can create a struct that stores what you need and
 pass that. Not sure if this helps but figured I'd mention it.
 
 AppDelegate *d = [[UIApplication sharedApplication] delegate];
 
 
 
 On Tue, Jul 30, 2013 at 8:53 AM, lowell lowe...@me.com wrote:
 
 The first two parameters to the function have to be an id and a SEL ...
 
typedef id (*IMP)(id, SEL, ...);
 
 ... (this is where we get self and _cmd, by the way) followed by the rest
 of the method params, if any.
 
 
 lowell
 
 
 On Jul 30, 2013, at 12:59 AM, Vincent Habchi vi...@macports.org wrote:
 
 Hi everybody,
 
 I have a very simple question: if I embed a C-function (more precisely,
 a callback from an external C-library) in an Obj-C object, can I expect
 this function to behave like a regular method? I.e. can it freely access
 ‘self’ and other attributes?
 
 Thanks a lot!
 Vincent
 
 
 
 
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 https://lists.apple.com/mailman/options/cocoa-dev/lowellv%40me.com
 
 This email sent to lowe...@me.com
 
 
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 https://lists.apple.com/mailman/options/cocoa-dev/rejekted%40gmail.com
 
 This email sent to rejek...@gmail.com
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 https://lists.apple.com/mailman/options/cocoa-dev/xcvista%40me.com
 
 This email sent to xcvi...@me.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Synchronous Serial Port Protocol messaging With NSOperationQueue/GCD

2013-08-07 Thread Vanni Parronchi
hi everyone,

I'm writing a mac osx app that needs to talk to a serial device.

There's a well known protocol to adhere to (implemented in the device
firmware) and is a kind of a half-duplex synchronous protocol where if i
send a message to the device from my app i have to wait till i get a
response from the device, inspect it and then i'm allowed to send another
message. This has to go for several messages i need to send to the device.

Given this synchronous nature, i'm trying to keep the UI free so i'm doing
all the messaging stuff on background threads. My choice is to use
NSOperation and NSOperationQueue, where the queue is configured with
maxConcurrentOperations set to 1 in order to have a strict serial behavior
(this is due to the constraints given by the implemented protocol). How can
i make an NSOperation subclass waiting for a signal given by the serial
port delegate to make it fire isFinished KVO to the queue and make that
queue consequently execute the next NSOperation?

Does this approach make sense to you?

I'm open to any suggestions of course.

Thank you!

Vanni
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

How to convert an NSString to

2013-08-07 Thread Ronald Hofmann
Hi all,
I need to convert a normal: 
NSString * string = @Ich möchte am Wettbewerb teilnehmen 

to something like this:
NSString * string = @Ich%20möchte%20am%20Wettbewerb%20teilnehmen

I don´t want to do this manually.

Any clues how to do it with Objective-C ?

Greetings from Switzerland, Ronald Hofmann
---  



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Mixing Obj-C and C methods

2013-08-07 Thread Tom Davie

On 30 Jul 2013, at 15:44, Maxthon Chan xcvi...@me.com wrote:

 My common way of handling this would be NSNotificationCenter. It is a 
 singleton so I am always sure that it is there, and I can wrap all parameters 
 into the userInfo dictionary.

NSNotificationCenter is not a singleton:
 $ cat test.m
 #import Foundation/Foundation.h
 
 int main (int argc, char **argv)
 {
   @autoreleasepool
   {
   NSNotificationCenter *a = [[[NSNotificationCenter alloc] init] 
 autorelease];
   NSNotificationCenter *b = [[[NSNotificationCenter alloc] init] 
 autorelease];
   NSLog(@%p, %p, a, b);
   }
 $ clang -framework Foundation test.m
 $ ./a.out 
 2013-08-07 09:13:25.508 a.out[6155:507] 0x7fcb1040a090, 0x7fcb1040a190


It just happens to have one instance (defaultNotificationCenter) which is 
common to want to use in lots of locations in your program.

You can not “be sure that [a singleton] is there” any more or less than any 
other class in your program.

For reference though, both designs for effectively making all objects in your 
program global objects are pretty horrible for exactly that reason – making 
everything effectively a global is just terrible design.

Tom Davie

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: How to convert an NSString to

2013-08-07 Thread Igor Elland
Hi, 

Have you tried:

[@Ich möchte am Wettbewerb teilnehmen 
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Granted, this will also replace the ö with its percent escaped variant, which 
you probably want anyway.

Best,
Igor Elland

On Aug 6, 2013, at 1:45 PM, Ronald Hofmann pro...@jumbosoft.de wrote:

 Hi all,
 I need to convert a normal: 
 NSString * string = @Ich möchte am Wettbewerb teilnehmen 
 
 to something like this:
 NSString * string = @Ich%20möchte%20am%20Wettbewerb%20teilnehmen
 
 I don´t want to do this manually.
 
 Any clues how to do it with Objective-C ?
 
 Greetings from Switzerland, Ronald Hofmann
 ---  
 
 
 
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 https://lists.apple.com/mailman/options/cocoa-dev/igor.elland%40me.com
 
 This email sent to igor.ell...@me.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Synchronous Serial Port Protocol messaging With NSOperationQueue/GCD

2013-08-07 Thread Tom Davie

On 1 Aug 2013, at 12:03, Vanni Parronchi vanniparron...@gmail.com wrote:

 hi everyone,
 
 I'm writing a mac osx app that needs to talk to a serial device.
 
 There's a well known protocol to adhere to (implemented in the device
 firmware) and is a kind of a half-duplex synchronous protocol where if i
 send a message to the device from my app i have to wait till i get a
 response from the device, inspect it and then i'm allowed to send another
 message. This has to go for several messages i need to send to the device.
 
 Given this synchronous nature, i'm trying to keep the UI free so i'm doing
 all the messaging stuff on background threads. My choice is to use
 NSOperation and NSOperationQueue, where the queue is configured with
 maxConcurrentOperations set to 1 in order to have a strict serial behavior
 (this is due to the constraints given by the implemented protocol). How can
 i make an NSOperation subclass waiting for a signal given by the serial
 port delegate to make it fire isFinished KVO to the queue and make that
 queue consequently execute the next NSOperation?
 
 Does this approach make sense to you?

My suggestion would instead be to make an NSOperation subclass for sending a 
message to the device.

That subclass should require you to specify to which device you are talking.  
Each device object should carry around a dispatch_semaphore.  When a message is 
sent, the semaphore should be checked and set to stop any other operations 
proceeding until the semaphore is unset.  When the device responds, the 
semaphore should be unset.

You can then make your operation queue a concurrent queue, rather than a serial 
one, so that you can send messages to more than one device at the same time, 
while still maintaining your one-thing-at-a-time per device rule.

Tom Davie

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: How to convert an NSString to

2013-08-07 Thread Tamas Nagy
Or if you just want to convert the spaces: string =  [@Ich möchte am 
Wettbewerb teilnehmen  stringByReplacingOccurrencesOfString:@  
withString:@%20];


On Aug 7, 2013, at 9:19 AM, Igor Elland igor.ell...@me.com wrote:

 Hi, 
 
 Have you tried:
 
 [@Ich möchte am Wettbewerb teilnehmen 
 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 
 Granted, this will also replace the ö with its percent escaped variant, which 
 you probably want anyway.
 
 Best,
 Igor Elland
 
 On Aug 6, 2013, at 1:45 PM, Ronald Hofmann pro...@jumbosoft.de wrote:
 
 Hi all,
 I need to convert a normal: 
 NSString * string = @Ich möchte am Wettbewerb teilnehmen 
 
 to something like this:
 NSString * string = @Ich%20möchte%20am%20Wettbewerb%20teilnehmen
 
 I don´t want to do this manually.
 
 Any clues how to do it with Objective-C ?
 
 Greetings from Switzerland, Ronald Hofmann
 ---  
 
 
 
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 https://lists.apple.com/mailman/options/cocoa-dev/igor.elland%40me.com
 
 This email sent to igor.ell...@me.com
 
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 https://lists.apple.com/mailman/options/cocoa-dev/tamas.lov.nagy%40gmail.com
 
 This email sent to tamas.lov.n...@gmail.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: How to convert an NSString to

2013-08-07 Thread Igor Elland

 Or if you just want to convert the spaces: string =  [@Ich möchte am 
 Wettbewerb teilnehmen  stringByReplacingOccurrencesOfString:@  
 withString:@%20”];

Please avoid using that, if you really need **only** white space to be 
replaced, you want to use [@Ich möchte am Wettbewerb teilnehmen 
stringByReplacingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] 
withString:@%20”] instead, as there are different UTF-8 characters for 
different kinds of whitespace.
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: How to convert an NSString to

2013-08-07 Thread Tom Davie

On 7 Aug 2013, at 09:24, Igor Elland igor.ell...@me.com wrote:

 
 Or if you just want to convert the spaces: string =  [@Ich möchte am 
 Wettbewerb teilnehmen  stringByReplacingOccurrencesOfString:@  
 withString:@%20”];
 
 Please avoid using that, if you really need **only** white space to be 
 replaced, you want to use [@Ich möchte am Wettbewerb teilnehmen 
 stringByReplacingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] 
 withString:@%20”] instead, as there are different UTF-8 characters for 
 different kinds of whitespace.

This is incorrect.  %20 is specifically for representing the 0x20th Unicode 
character – that is “space”.  It is not for representing other whitespace 
characters like tab (%09) etc.

Tom Davie

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Mixing Obj-C and C methods

2013-08-07 Thread Jean-Daniel Dupas
While all theses methods may look valid, what not simply use a static variable 
declared in your file ?

Instead of trying to use complex approach to hide the fact you need a global, 
just use one, and don't try to reuse the existing one for things there are not 
designed to do.


static id myCallbackHandler;

void someCallBack() {
[myCallbackHandler handleCallBack];
}

- (void)foo {
myCallbackHandler = self;
callCFunctionWithCallBack(someCallBack);
myCallbackHandler = nil;
}


Le 30 juil. 2013 à 15:44, Maxthon Chan xcvi...@me.com a écrit :

 My common way of handling this would be NSNotificationCenter. It is a 
 singleton so I am always sure that it is there, and I can wrap all parameters 
 into the userInfo dictionary.
 
 Sent from my iPhone
 
 On 2013年7月30日, at 21:19, KappA rejek...@gmail.com wrote:
 
 I sometimes just access my objc-objects from a C thread-proc via the
 AppDelegate (providing there's a trail to the object I need, which there
 usually is)... If the callback void pointer parameter isn't being used for
 something else, you can simply cast the object in there... or if you need
 multiple parameters you can create a struct that stores what you need and
 pass that. Not sure if this helps but figured I'd mention it.
 
 AppDelegate *d = [[UIApplication sharedApplication] delegate];
 
 
 
 On Tue, Jul 30, 2013 at 8:53 AM, lowell lowe...@me.com wrote:
 
 The first two parameters to the function have to be an id and a SEL ...
 
   typedef id (*IMP)(id, SEL, ...);
 
 ... (this is where we get self and _cmd, by the way) followed by the rest
 of the method params, if any.
 
 
 lowell
 
 
 On Jul 30, 2013, at 12:59 AM, Vincent Habchi vi...@macports.org wrote:
 
 Hi everybody,
 
 I have a very simple question: if I embed a C-function (more precisely,
 a callback from an external C-library) in an Obj-C object, can I expect
 this function to behave like a regular method? I.e. can it freely access
 ‘self’ and other attributes?
 
 Thanks a lot!
 Vincent
 
 
 
 
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 https://lists.apple.com/mailman/options/cocoa-dev/lowellv%40me.com
 
 This email sent to lowe...@me.com
 
 
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 https://lists.apple.com/mailman/options/cocoa-dev/rejekted%40gmail.com
 
 This email sent to rejek...@gmail.com
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 https://lists.apple.com/mailman/options/cocoa-dev/xcvista%40me.com
 
 This email sent to xcvi...@me.com
 
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 https://lists.apple.com/mailman/options/cocoa-dev/devlists%40shadowlab.org
 
 This email sent to devli...@shadowlab.org

-- Jean-Daniel





___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Mixing Obj-C and C methods

2013-08-07 Thread Maxthon Chan
Sorry, clicked a wrong button.

On Aug 7, 2013, at 16:00, Maxthon Chan xcvi...@me.com wrote:

 Well here is a reason I think that is valid enough to implement a callback 
 using notifications: that is what Objective-C use for what callbacks used to 
 do, besides target-actions and delegations. In Cocoa, quite a lot of 
 delegated methods are actually mapped to messages.
 
 Here is a snippet, with CGI as the class prefix:
 
 void _CGI_SocketCallback(void *ctxt, int param)
 {
   id objectIdentifier = @((NSUInteger)ctxt); // Treat object pointers as 
 NSUIntegers - that prevents crashes and does not leak memory.
   [[NSNotificationCenter defaultCenter] 
 postNotificationName:CGISocketNotification object:objectIdentifier 
 userData:@{@“param”: @(param)}];
 }
 
 - (void)setUpCallback
 {
   [[NSNotificationCenter defaultCenter] addObserver:self 
 action:@selector(callbackCalled:) name:CGISocketNotification 
 object:@((__bridge NSUInteger)self)];
 }
 
 If needed:
 
 - (void)callbackCalled:(NSNotification *)aNotification
 {
   if ([self.delegate 
 respondsToSelector:@selector(connection:callbackDidCallWithParam:)])
   [self.delegate connection:self 
 callbackDidCallWithParam:[aNotification userInfo][@“param”]];
 }
 
 On Aug 7, 2013, at 15:47, Jean-Daniel Dupas devli...@shadowlab.org wrote:
 
 While all theses methods may look valid, what not simply use a static 
 variable declared in your file ?
 
 Instead of trying to use complex approach to hide the fact you need a 
 global, just use one, and don't try to reuse the existing one for things 
 there are not designed to do.
 
 
 static id myCallbackHandler;
 
 void someCallBack() {
  [myCallbackHandler handleCallBack];
 }
 
 - (void)foo {
  myCallbackHandler = self;
  callCFunctionWithCallBack(someCallBack);
  myCallbackHandler = nil;
 }
 
 
 Le 30 juil. 2013 à 15:44, Maxthon Chan xcvi...@me.com a écrit :
 
 My common way of handling this would be NSNotificationCenter. It is a 
 singleton so I am always sure that it is there, and I can wrap all 
 parameters into the userInfo dictionary.
 
 Sent from my iPhone
 
 On 2013年7月30日, at 21:19, KappA rejek...@gmail.com wrote:
 
 I sometimes just access my objc-objects from a C thread-proc via the
 AppDelegate (providing there's a trail to the object I need, which there
 usually is)... If the callback void pointer parameter isn't being used for
 something else, you can simply cast the object in there... or if you need
 multiple parameters you can create a struct that stores what you need and
 pass that. Not sure if this helps but figured I'd mention it.
 
 AppDelegate *d = [[UIApplication sharedApplication] delegate];
 
 
 
 On Tue, Jul 30, 2013 at 8:53 AM, lowell lowe...@me.com wrote:
 
 The first two parameters to the function have to be an id and a SEL ...
 
  typedef id (*IMP)(id, SEL, ...);
 
 ... (this is where we get self and _cmd, by the way) followed by the rest
 of the method params, if any.
 
 
 lowell
 
 
 On Jul 30, 2013, at 12:59 AM, Vincent Habchi vi...@macports.org wrote:
 
 Hi everybody,
 
 I have a very simple question: if I embed a C-function (more precisely,
 a callback from an external C-library) in an Obj-C object, can I expect
 this function to behave like a regular method? I.e. can it freely access
 ‘self’ and other attributes?
 
 Thanks a lot!
 Vincent
 
 
 
 
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 https://lists.apple.com/mailman/options/cocoa-dev/lowellv%40me.com
 
 This email sent to lowe...@me.com
 
 
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 https://lists.apple.com/mailman/options/cocoa-dev/rejekted%40gmail.com
 
 This email sent to rejek...@gmail.com
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 https://lists.apple.com/mailman/options/cocoa-dev/xcvista%40me.com
 
 This email sent to xcvi...@me.com
 
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 https://lists.apple.com/mailman/options/cocoa-dev/devlists%40shadowlab.org
 
 This email sent to devli...@shadowlab.org
 
 -- Jean-Daniel


Re: Mixing Obj-C and C methods

2013-08-07 Thread Scott Ribe
On Aug 7, 2013, at 1:47 AM, Jean-Daniel Dupas devli...@shadowlab.org wrote:

 While all theses methods may look valid, what not simply use a static 
 variable declared in your file ?

DING DING DING DING! WE HAVE A WINNER!

Actually, much of the history behind other methods for singletons revolves 
around big enterprisey nasty inter-tangled systems where developers lose track 
of (in many cases, never knew in the first place) the dependencies between 
globals.

Well, in my opinion, if you have so many dependencies between globals that you 
can't figure out in which order to initialize them, you have a far bigger 
problem.

-- 
Scott Ribe
scott_r...@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice





___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Synchronous Serial Port Protocol messaging With NSOperationQueue/GCD

2013-08-07 Thread Sean McBride
On Thu, 1 Aug 2013 12:03:01 +0200, Vanni Parronchi said:

hi everyone,

I'm writing a mac osx app that needs to talk to a serial device.

Have you considered using the open source AMSerialPort class?  It doesn't use 
GCD, but does have an NSThread option.  It could probably be retrofitted to use 
GCD also... which I have been thinking I might one day have time to do...

Cheers,

-- 

Sean McBride, B. Eng s...@rogue-research.com
Rogue Researchwww.rogue-research.com 
Mac Software Developer  Montréal, Québec, Canada



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Mixing Obj-C and C methods

2013-08-07 Thread Andy Lee
On Aug 7, 2013, at 3:47 AM, Jean-Daniel Dupas devli...@shadowlab.org wrote:
 Instead of trying to use complex approach to hide the fact you need a global, 
 just use one, and don't try to reuse the existing one for things there are 
 not designed to do.
 
 
 static id myCallbackHandler;
 
 void someCallBack() {
   [myCallbackHandler handleCallBack];
 }
 
 - (void)foo {
   myCallbackHandler = self;
   callCFunctionWithCallBack(someCallBack);
   myCallbackHandler = nil;
 }

What if instance x does [x foo], and before someCallBack() gets called, some 
other instance y does [y foo]?  There will be two future calls to 
someCallBack(), and [y handleCallBack] will be called both times, which is not 
the desired outcome.  This is a problem with any approach where the callback 
looks in some global place, whether it's a static variable, a key path from the 
app delegate, or whatever.

Even if you are sure you won't run into the problem of the global variable 
being overwritten, I think routing self through a global like myCallbackHandler 
is more complex than:

void someCallBack(void *contextPtr) {
[[(MyClass *)contextPtr autorelease] handleCallBack];
}

- (void)foo {
callCFunctionWithCallBack(someCallBack, (void *)[self retain]);
}

or with ARC:

void someCallBack(void *contextPtr) {
[(__bridge_retained MyClass *)contextPtr handleCallBack];
}

- (void)foo {
callCFunctionWithCallBack(someCallBack, (__bridge_transfer void *)self);
}

This assumes that the API includes a context pointer, but realistically, how 
often won't that be the case?  (I don't actually know.)

--Andy

 
 
 Le 30 juil. 2013 à 15:44, Maxthon Chan xcvi...@me.com a écrit :
 
 My common way of handling this would be NSNotificationCenter. It is a 
 singleton so I am always sure that it is there, and I can wrap all 
 parameters into the userInfo dictionary.
 
 Sent from my iPhone
 
 On 2013年7月30日, at 21:19, KappA rejek...@gmail.com wrote:
 
 I sometimes just access my objc-objects from a C thread-proc via the
 AppDelegate (providing there's a trail to the object I need, which there
 usually is)... If the callback void pointer parameter isn't being used for
 something else, you can simply cast the object in there... or if you need
 multiple parameters you can create a struct that stores what you need and
 pass that. Not sure if this helps but figured I'd mention it.
 
 AppDelegate *d = [[UIApplication sharedApplication] delegate];
 
 
 
 On Tue, Jul 30, 2013 at 8:53 AM, lowell lowe...@me.com wrote:
 
 The first two parameters to the function have to be an id and a SEL ...
 
  typedef id (*IMP)(id, SEL, ...);
 
 ... (this is where we get self and _cmd, by the way) followed by the rest
 of the method params, if any.
 
 
 lowell
 
 
 On Jul 30, 2013, at 12:59 AM, Vincent Habchi vi...@macports.org wrote:
 
 Hi everybody,
 
 I have a very simple question: if I embed a C-function (more precisely,
 a callback from an external C-library) in an Obj-C object, can I expect
 this function to behave like a regular method? I.e. can it freely access
 ‘self’ and other attributes?
 
 Thanks a lot!
 Vincent
 


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Synchronous Serial Port Protocol messaging With NSOperationQueue/GCD

2013-08-07 Thread Ken Thomases
On Aug 7, 2013, at 2:20 AM, Tom Davie wrote:

 My suggestion would instead be to make an NSOperation subclass for sending a 
 message to the device.
 
 That subclass should require you to specify to which device you are talking.  
 Each device object should carry around a dispatch_semaphore.  When a message 
 is sent, the semaphore should be checked and set to stop any other operations 
 proceeding until the semaphore is unset.  When the device responds, the 
 semaphore should be unset.
 
 You can then make your operation queue a concurrent queue, rather than a 
 serial one, so that you can send messages to more than one device at the same 
 time, while still maintaining your one-thing-at-a-time per device rule.

Why would you do that?  Operation queues are cheap.  Have one serial queue per 
device.  There's little reason to add another synchronization method on top of 
operations to cause them to serialize with respect to one another.

Regards,
Ken


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Mixing Obj-C and C methods

2013-08-07 Thread Jean-Daniel Dupas

Le 7 août 2013 à 17:32, Andy Lee ag...@mac.com a écrit :

 On Aug 7, 2013, at 3:47 AM, Jean-Daniel Dupas devli...@shadowlab.org wrote:
 Instead of trying to use complex approach to hide the fact you need a 
 global, just use one, and don't try to reuse the existing one for things 
 there are not designed to do.
 
 
 static id myCallbackHandler;
 
 void someCallBack() {
  [myCallbackHandler handleCallBack];
 }
 
 - (void)foo {
  myCallbackHandler = self;
  callCFunctionWithCallBack(someCallBack);
  myCallbackHandler = nil;
 }
 
 What if instance x does [x foo], and before someCallBack() gets called, some 
 other instance y does [y foo]?  There will be two future calls to 
 someCallBack(), and [y handleCallBack] will be called both times, which is 
 not the desired outcome.  This is a problem with any approach where the 
 callback looks in some global place, whether it's a static variable, a key 
 path from the app delegate, or whatever.

 Even if you are sure you won't run into the problem of the global variable 
 being overwritten, I think routing self through a global like 
 myCallbackHandler is more complex than:

If you intend to use it from multiple threads, so use a tls.

__thread id myCallbackHandler;

I was talking about the case where you have to deal with a poorly design API 
with no context pointer argument.
The case with a context argument should off course be handle the way you 
describe.

 void someCallBack(void *contextPtr) {
   [[(MyClass *)contextPtr autorelease] handleCallBack];
 }
 
 - (void)foo {
   callCFunctionWithCallBack(someCallBack, (void *)[self retain]);
 }
 
 or with ARC:
 
 void someCallBack(void *contextPtr) {
   [(__bridge_retained MyClass *)contextPtr handleCallBack];
 }
 
 - (void)foo {
   callCFunctionWithCallBack(someCallBack, (__bridge_transfer void *)self);
 }
 
 This assumes that the API includes a context pointer, but realistically, how 
 often won't that be the case?  (I don't actually know.)
 --Andy
 
 
 
 Le 30 juil. 2013 à 15:44, Maxthon Chan xcvi...@me.com a écrit :
 
 My common way of handling this would be NSNotificationCenter. It is a 
 singleton so I am always sure that it is there, and I can wrap all 
 parameters into the userInfo dictionary.
 
 Sent from my iPhone
 
 On 2013年7月30日, at 21:19, KappA rejek...@gmail.com wrote:
 
 I sometimes just access my objc-objects from a C thread-proc via the
 AppDelegate (providing there's a trail to the object I need, which there
 usually is)... If the callback void pointer parameter isn't being used for
 something else, you can simply cast the object in there... or if you need
 multiple parameters you can create a struct that stores what you need and
 pass that. Not sure if this helps but figured I'd mention it.
 
 AppDelegate *d = [[UIApplication sharedApplication] delegate];
 
 
 
 On Tue, Jul 30, 2013 at 8:53 AM, lowell lowe...@me.com wrote:
 
 The first two parameters to the function have to be an id and a SEL ...
 
 typedef id (*IMP)(id, SEL, ...);
 
 ... (this is where we get self and _cmd, by the way) followed by the rest
 of the method params, if any.
 
 
 lowell
 
 
 On Jul 30, 2013, at 12:59 AM, Vincent Habchi vi...@macports.org wrote:
 
 Hi everybody,
 
 I have a very simple question: if I embed a C-function (more precisely,
 a callback from an external C-library) in an Obj-C object, can I expect
 this function to behave like a regular method? I.e. can it freely access
 ‘self’ and other attributes?
 
 Thanks a lot!
 Vincent
 
 

-- Jean-Daniel





___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Mixing Obj-C and C methods

2013-08-07 Thread Andy Lee
On Aug 7, 2013, at 12:04 PM, Jean-Daniel Dupas devli...@shadowlab.org wrote:
 If you intend to use it from multiple threads, so use a tls.
 
 __thread id myCallbackHandler;

I did not know about __thread, thanks for this.  By using tls you're 
effectively having each thread store the info that would have been stored by 
the implementation of the callback's context pointer, if it had one.

If [x foo] and [y foo] are called in the same thread, then that thread's 
myCallbackHandler can still get clobbered if the callback is asynchronous.  So 
I guess you'd have to be careful to always call -foo in a separate thread, or 
implement it to always spawn a thread.

 I was talking about the case where you have to deal with a poorly design API 
 with no context pointer argument.
 The case with a context argument should off course be handle the way you 
 describe.

I see, thanks.

--Andy


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Mixing Obj-C and C methods

2013-08-07 Thread Jean-Daniel Dupas

Le 7 août 2013 à 18:34, Andy Lee ag...@mac.com a écrit :

 On Aug 7, 2013, at 12:04 PM, Jean-Daniel Dupas devli...@shadowlab.org wrote:
 If you intend to use it from multiple threads, so use a tls.
 
 __thread id myCallbackHandler;
 
 I did not know about __thread, thanks for this.  By using tls you're 
 effectively having each thread store the info that would have been stored by 
 the implementation of the callback's context pointer, if it had one.
 
 If [x foo] and [y foo] are called in the same thread, then that thread's 
 myCallbackHandler can still get clobbered if the callback is asynchronous.  
 So I guess you'd have to be careful to always call -foo in a separate thread, 
 or implement it to always spawn a thread.

And even with a synchronous API, the TLS approach is fragile as the API as to 
guarantee that the callback is performed on the same thread.

But honestly, if I encounter such API and it does not let me pass a context 
pointer for the callback, I will just avoid it and use something else.

 I was talking about the case where you have to deal with a poorly design API 
 with no context pointer argument.
 The case with a context argument should off course be handle the way you 
 describe.
 
 I see, thanks.
 
 --Andy
 

-- Jean-Daniel





___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Synchronous Serial Port Protocol messaging With NSOperationQueue/GCD

2013-08-07 Thread Vanni Parronchi
Hi all,

Thank you all for you contribution and responses. It was my first message
to the list and was a bit intimidated :).

the app is meant to be modular, 10.7 based and it has to manage not only
serial devices (rs485) but also USB ones (through a virtual com port using
an FTDI chip on the device and an external C library some contractor made
quite some time ago for the company i'm working for, that abstracts
communication with the FTDI underlying driver). I'm focusing on the serial
ones for now.

given the modular nature the app uses loadable bundles as plugins so that
every plugin can be updated independently (i'm thinking about using the
famous sparkle http://sparkle.andymatuschak.org/%E2%80%8E by Andy
Matuschak, anyone?).

I've managed to implement a kind of IOC/service
locatorhttp://www.martinfowler.com/articles/injection.html#UsingAServiceLocatorin
a framework that every bundle can have access to, in order to request
instances belonging to different bundles through protocols. And for
messaging to/from bundles i made use of NSNotificationCenter a lot,
probably abusing it, hoping that this won't bring issues in the future.

Those serial devices can be daisy chained together, every device has a
unique integer ID that can be set on the device

Every command sent to the device is an instance of a high level message
class that encapsulates all the information a message has to have
(target device id, command and data)

The connection layer plugins (rs485, usb) are abstracted through proper
class (loadable bundle) and every class *has* a message manager that
receives high level messages and converts to/from low level messages in
form of pure NSData objects. Through a category on NSData i can factorize a
high level message in the proper set of bytes, then send the NSData to
serial port and wait for the response (if no response every message has a
timeout)

I can't talk to different devices at the same time because collisions on
the bus can happen thus corrupting the messages. It has to be strictly half
duplex

I use ORSSerialPort as the serial port library. I found its API to be more
comfortable than the AMSerialPort i tried when i started.

NSOperation and NSOperationQueues enables you to better encapsulate the
logic of sending a message, but at the end all the messages are not so
different in term if implementation at a lower level and the balance
between encapsulating but passing lots of arguments and handling everything
in a dispatch call preserving scope and context led me to go with pure GCD.

I use dispatch_groups to handle high level commands that are in fact the
product of several low level messages to the device, dispatch_async and
dispatch_semaphore to wait for the response, and i also fire a
dispatch_source with a timer to handle timeouts, i.e. discovering devices
from the software implies firing messages to integer IDs in a for loop. So
far, seems that the synchronization is achieved but i'll see when i'll go
further.

It's my first app for Mac OS X, i did entirely iOS before, and i've run
through so much trials and errors that i almost burned out. Hope i'll get
it to be shipped.

For those who managed to read all this, does it seem a reasonable approach
to the problem?

Thank you and Best Regards!

Vanni



2013/8/7 Ken Thomases k...@codeweavers.com

 On Aug 7, 2013, at 2:20 AM, Tom Davie wrote:

  My suggestion would instead be to make an NSOperation subclass for
 sending a message to the device.
 
  That subclass should require you to specify to which device you are
 talking.  Each device object should carry around a dispatch_semaphore.
  When a message is sent, the semaphore should be checked and set to stop
 any other operations proceeding until the semaphore is unset.  When the
 device responds, the semaphore should be unset.
 
  You can then make your operation queue a concurrent queue, rather than a
 serial one, so that you can send messages to more than one device at the
 same time, while still maintaining your one-thing-at-a-time per device rule.

 Why would you do that?  Operation queues are cheap.  Have one serial queue
 per device.  There's little reason to add another synchronization method on
 top of operations to cause them to serialize with respect to one another.

 Regards,
 Ken


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

IOS floating point performance

2013-08-07 Thread Trygve Inda
I have an app that is running slow. I have narrowed it down to several
functions which are trig-intensive (used to calculate the position of the
moon at a given moment and more specifically to calculate rise/set times).

To calculate the position and rise/set times for a month on average,
requires:

300 trig functions for position
 24 points in time to find the correct set of three points
 30 days
  2 events

Total of 432,000 trig functions (mostly sin and cos)
 
On my iPad 3, this takes about 3 seconds, and on my desktop (2.3GHz
Quad-core i7) it is nearly instantaneous.

I am using doubles (not floats).

What can I do to speed this up?



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: IOS floating point performance

2013-08-07 Thread Rick Mann
A few things:

- The little ARM in the iPad 3 is nothing compared to your desktop. Not only is 
it not as fast, it doesn't have the memory bandwidth. Also, an iPad 4 will be 
twice as fast.
- make sure you're running optimized code. You can set optimization flags on 
individual files (that's what I do in our compute-heavy code, so I can run 
debug releases for the UI but leave the computation optimized).
- Find a way to compute less. Algorithmic optimizations are key here. Maybe a 
larger step size?
- Can you break up the work into multiple tasks? That will allow you to 
parallelize the work. At the very least, make sure you're doing it on a 
separate dispatch queue/NSOperationQueue so it doesn't block your main UI.

On Aug 7, 2013, at 13:50 , Trygve Inda cocoa...@xericdesign.com wrote:

 I have an app that is running slow. I have narrowed it down to several
 functions which are trig-intensive (used to calculate the position of the
 moon at a given moment and more specifically to calculate rise/set times).
 
 To calculate the position and rise/set times for a month on average,
 requires:
 
 300 trig functions for position
 24 points in time to find the correct set of three points
 30 days
  2 events
 
 Total of 432,000 trig functions (mostly sin and cos)
 
 On my iPad 3, this takes about 3 seconds, and on my desktop (2.3GHz
 Quad-core i7) it is nearly instantaneous.
 
 I am using doubles (not floats).
 
 What can I do to speed this up?
 
 
 
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 https://lists.apple.com/mailman/options/cocoa-dev/rmann%40latencyzero.com
 
 This email sent to rm...@latencyzero.com


-- 
Rick




___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: IOS floating point performance

2013-08-07 Thread Fritz Anderson
On 7 Aug 2013, at 3:50 PM, Trygve Inda cocoa...@xericdesign.com wrote:

 I have an app that is running slow. I have narrowed it down to several
 functions which are trig-intensive (used to calculate the position of the
 moon at a given moment and more specifically to calculate rise/set times).
 
 To calculate the position and rise/set times for a month on average,
 requires:
 
 300 trig functions for position
 24 points in time to find the correct set of three points
 30 days
  2 events
 
 Total of 432,000 trig functions (mostly sin and cos)
 
 On my iPad 3, this takes about 3 seconds, and on my desktop (2.3GHz
 Quad-core i7) it is nearly instantaneous.
 
 I am using doubles (not floats).
 
 What can I do to speed this up?

You don't have to be told that desktop performance with the same algorithm on a 
processor that is 1-10% as fast isn't going to happen. 

If you can figure out how to vectorize your calculations, look at veclib in the 
Accelerate framework. vvsin() and vvsinf() have been available since iOS 5.

I'm a little surprised to see that veclib supports doubles. My instinct (based 
on imagining that you'll be striding through an array with vector registers 
that can hold two doubles or four floats)* is that floats could be much faster, 
and you should really think about whether you need doubles: You're depicting 
the moon, not flying there.

* (Readers should consider whether correcting my rule-of-thumb notion is a good 
use of their time.)

— F



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: IOS floating point performance

2013-08-07 Thread Conrad Shultz

On Aug 7, 2013, at 1:50 PM, Trygve Inda cocoa...@xericdesign.com wrote:

 I have an app that is running slow. I have narrowed it down to several
 functions which are trig-intensive (used to calculate the position of the
 moon at a given moment and more specifically to calculate rise/set times).
 
 To calculate the position and rise/set times for a month on average,
 requires:
 
 300 trig functions for position
 24 points in time to find the correct set of three points
 30 days
  2 events
 
 Total of 432,000 trig functions (mostly sin and cos)
 
 On my iPad 3, this takes about 3 seconds, and on my desktop (2.3GHz
 Quad-core i7) it is nearly instantaneous.
 
 I am using doubles (not floats).
 
 What can I do to speed this up?

This is somewhat outside my area of expertise, but if your calculations are at 
least partially independent, and can thus be expressed in terms of vector 
calculations, then you might take a look at vecLib (part of 
Accelerate.framework), 

https://developer.apple.com/library/ios/#documentation/Performance/Conceptual/vecLib/Reference/reference.html

There’s also a WWDC session from 2011 or 2012 that provides an introduction to 
Accelerate.framework.

-Conrad
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: IOS floating point performance

2013-08-07 Thread Trygve Inda
 A few things:
 
 - The little ARM in the iPad 3 is nothing compared to your desktop. Not only
 is it not as fast, it doesn't have the memory bandwidth. Also, an iPad 4 will
 be twice as fast.
 - make sure you're running optimized code. You can set optimization flags on
 individual files (that's what I do in our compute-heavy code, so I can run
 debug releases for the UI but leave the computation optimized).
 - Find a way to compute less. Algorithmic optimizations are key here. Maybe a
 larger step size?
 - Can you break up the work into multiple tasks? That will allow you to
 parallelize the work. At the very least, make sure you're doing it on a
 separate dispatch queue/NSOperationQueue so it doesn't block your main UI.

I am currently doing it on an NSThread. I may try replacing all the doubles
with floats in the algorithm and see how that goes (on a backup of course!).

How do I set optimization on a per-file basis?

Trygve



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: IOS floating point performance

2013-08-07 Thread Rick Mann

On Aug 7, 2013, at 14:34 , Trygve Inda cocoa...@xericdesign.com wrote:

 I am currently doing it on an NSThread. I may try replacing all the doubles
 with floats in the algorithm and see how that goes (on a backup of course!).

Yes, floats should help (unless precision errors make it worse). You'll be 
moving a lot less memory around, and they're computed in hardware (I think 
double requires software intervention, but I could be wrong about that).

 How do I set optimization on a per-file basis?

In the Target settings, Build Phases tab, Compile Sources phase, you can set 
individual compiler flags. I put -O3 on each of the files I want optimized. -O2 
or -Os should be tried, too, to see which gives you the best results.


-- 
Rick




___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: IOS floating point performance

2013-08-07 Thread Howard Moon

On Aug 7, 2013, at 2:27 PM, Fritz Anderson wrote:

 On 7 Aug 2013, at 3:50 PM, Trygve Inda cocoa...@xericdesign.com wrote:
 
 I have an app that is running slow. I have narrowed it down to several
 functions which are trig-intensive (used to calculate the position of the
 moon at a given moment and more specifically to calculate rise/set times).
 
 To calculate the position and rise/set times for a month on average,
 requires:
 
 300 trig functions for position
 24 points in time to find the correct set of three points
 30 days
 2 events
 
 Total of 432,000 trig functions (mostly sin and cos)
 
 On my iPad 3, this takes about 3 seconds, and on my desktop (2.3GHz
 Quad-core i7) it is nearly instantaneous.
 
 I am using doubles (not floats).
 
 What can I do to speed this up?
 
 You don't have to be told that desktop performance with the same algorithm on 
 a processor that is 1-10% as fast isn't going to happen. 
 
 If you can figure out how to vectorize your calculations, look at veclib in 
 the Accelerate framework. vvsin() and vvsinf() have been available since iOS 
 5.
 
 I'm a little surprised to see that veclib supports doubles. My instinct 
 (based on imagining that you'll be striding through an array with vector 
 registers that can hold two doubles or four floats)* is that floats could be 
 much faster, and you should really think about whether you need doubles: 
 You're depicting the moon, not flying there.
 
 * (Readers should consider whether correcting my rule-of-thumb notion is a 
 good use of their time.)
 
   — F

You can also use lookup tables for your trig functions, especially if you can 
live with less precision.

And you can look and see if there are calculations that are going to be 
repeated, and do those only once (or once per loop, as appropriate).

Also, you might be able to combine some of the calculations into simpler ones, 
using the rules of trig (think sin2x, etc.).

Another approach is to represent sines and cosines as infinite series, and 
combine those series in order to compute one (or two, or a few) series that you 
can then either compute directly (using finite approximations to the infinite 
series) or turn them back into trig functions.  (That's difficult work, though, 
far beyond just using trig functions.)

-Howard


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: IOS floating point performance

2013-08-07 Thread Trygve Inda
 I'm a little surprised to see that veclib supports doubles. My instinct (based
 on imagining that you'll be striding through an array with vector registers
 that can hold two doubles or four floats)* is that floats could be much
 faster, and you should really think about whether you need doubles: You're
 depicting the moon, not flying there.

Floats don't work - I get completely wrong results.

Trygve



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: IOS floating point performance

2013-08-07 Thread Trygve Inda
 
 On Aug 7, 2013, at 14:34 , Trygve Inda cocoa...@xericdesign.com wrote:
 
 I am currently doing it on an NSThread. I may try replacing all the doubles
 with floats in the algorithm and see how that goes (on a backup of course!).
 
 Yes, floats should help (unless precision errors make it worse). You'll be
 moving a lot less memory around, and they're computed in hardware (I think
 double requires software intervention, but I could be wrong about that).
 
 How do I set optimization on a per-file basis?
 
 In the Target settings, Build Phases tab, Compile Sources phase, you can set
 individual compiler flags. I put -O3 on each of the files I want optimized.
 -O2 or -Os should be tried, too, to see which gives you the best results.
 

I tried using floats and the results were wrong, presumably due to lower
precision and compounding of errors (but it took roughly the same amount of
time as doubles).



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: IOS floating point performance

2013-08-07 Thread David Rowland
I have written an app that does astronomical calculations like that , Sun and 
Moon rise and set and location and….. I never saw a problem with speed. I was 
very impressed with how much it can do. However, are you using Objective C 
methods for the calculations? The run time dispatch in Objective C could slow 
things considerably. I wrote all my math in C++.

David


On Aug 7, 2013, at 1:50 PM, Trygve Inda cocoa...@xericdesign.com wrote:

 I have an app that is running slow. I have narrowed it down to several
 functions which are trig-intensive (used to calculate the position of the
 moon at a given moment and more specifically to calculate rise/set times).
 
 To calculate the position and rise/set times for a month on average,
 requires:
 
 300 trig functions for position
 24 points in time to find the correct set of three points
 30 days
  2 events
 
 Total of 432,000 trig functions (mostly sin and cos)
 
 On my iPad 3, this takes about 3 seconds, and on my desktop (2.3GHz
 Quad-core i7) it is nearly instantaneous.
 
 I am using doubles (not floats).
 
 What can I do to speed this up?
 
 
 
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 https://lists.apple.com/mailman/options/cocoa-dev/rowlandd%40sbcglobal.net
 
 This email sent to rowla...@sbcglobal.net


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: IOS floating point performance

2013-08-07 Thread Trygve Inda
 I have written an app that does astronomical calculations like that , Sun and
 Moon rise and set and location and….. I never saw a problem with speed. I was
 very impressed with how much it can do. However, are you using Objective C
 methods for the calculations? The run time dispatch in Objective C could slow
 things considerably. I wrote all my math in C++.
 
 David

I am using C for the math calcs. The issue is I am trying to calc a month at
a time... Sun rise/set and moon rise/set for 30 days. For one day it is
plenty fast, but the slow speed shows up when doing a month at a time.

Did you use floats or doubles?

Trygve




___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: IOS floating point performance

2013-08-07 Thread Jens Alfke

On Aug 7, 2013, at 1:50 PM, Trygve Inda cocoa...@xericdesign.com wrote:

 What can I do to speed this up?

Did you use Instruments to CPU-profile the app? It would be a good idea, at 
least to confirm that the slowness comes directly from the math functions and 
not something else like memory allocation or method-dispatching.

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com