Re: Initializing a NSMutableString an odd way

2013-08-03 Thread Andreas Grosam

On 31.07.2013, at 22:01, Greg Parker gpar...@apple.com wrote:

 On Jul 31, 2013, at 12:28 PM, Vincent Habchi vi...@macports.org wrote:
 David Duncan wrote:
 Why would there be? Your just asking for a mutable copy of an empty string. 
 It should be equivalent to [[NSMutableString alloc] initWithString:@«  »]
 
 But much slower I expect, since it creates a NSString, takes a mutable copy, 
 then implicitly releases the constant empty NSString.
 
 For giggles I tried some NSMutableString allocation patterns into my 
 microbenchmark test harness. 
 
 Simple alloc/init is the fastest:
 
 100  [[[NSMutableString alloc] init] release]
 102  [[NSMutableString new] release]
 109  [NSMutableString string]  // ARC enabled
 117  [[@ mutableCopy] release]
 119  @autoreleasepool { [NSMutableString string]; }  // ARC disabled
 129  [[[NSMutableString alloc] initWithString:@] release]
 
 (Smaller numbers are better. Numbers are getrusage(RUSAGE_SELF) time for 
 1000 iterations, normalized to fastest=100. Your mileage may vary.)


How much time contributes the allocation of the raw storage? I would guess that 
initializing an empty string is quite fast (while generally NSString is 
disappointing slow in almost all other functions).

What's also interesting is the time it takes to *deallocate* an object. Since 
ARC and weak pointers, this time increased dramatically (due to the requirement 
to remove the object from the sparse array).


In those micro-benchmarks its also interesting how this benchmark affects the 
behavior of the heap. It's likely we see a particular edge case which is either 
fast or slow. I would expect that allocating and deallocating the same object 
in a loop leads to faster allocation times. As opposed to the case where 
differrent objects are allocated and deallocated in random order.


Andreas


 
 ARC and non-ARC scores are the same within measurement noise, except for 
 [NSMutableString string] where ARC can optimize the autoreleased return value 
 so the test doesn't need to spin the autorelease pool. Note th
 
 
 BTW, what’s the difference between [[NSMutableString alloc] init] and 
 [[NSMutableString alloc] initWithString:@“”]?
 
 Semantically there's no difference: you get the same string with the same 
 retain count.
 
 
 -- 
 Greg Parker gpar...@apple.com Runtime Wrangler

___

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

Initializing a NSMutableString an odd way

2013-07-31 Thread Vincent Habchi
Folks,

I apologize if this question looks stupid or contrived. Here it is: is it 
permissible to use [@“” mutableCopy] to initialize (or reset) a NSMutableString 
instead of the more classical [[NSMutableString alloc] init]?

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: Initializing a NSMutableString an odd way

2013-07-31 Thread Mike Abdullah

On 31 Jul 2013, at 19:09, Vincent Habchi vi...@macports.org wrote:

 Folks,
 
 I apologize if this question looks stupid or contrived. Here it is: is it 
 permissible to use [@“” mutableCopy] to initialize (or reset) a 
 NSMutableString instead of the more classical [[NSMutableString alloc] init]?

Yes.


___

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: Initializing a NSMutableString an odd way

2013-07-31 Thread Vincent Habchi

Le 31 juil. 2013 à 20:15, Mike Abdullah mabdul...@karelia.com a écrit :

 I apologize if this question looks stupid or contrived. Here it is: is it 
 permissible to use [@“” mutableCopy] to initialize (or reset) a 
 NSMutableString instead of the more classical [[NSMutableString alloc] init]?
 
 Yes.

Thanks! (No side effect?)

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: Initializing a NSMutableString an odd way

2013-07-31 Thread David Duncan
On Jul 31, 2013, at 11:25 AM, Vincent Habchi vi...@macports.org wrote:

 
 Le 31 juil. 2013 à 20:15, Mike Abdullah mabdul...@karelia.com a écrit :
 
 I apologize if this question looks stupid or contrived. Here it is: is it 
 permissible to use [@“” mutableCopy] to initialize (or reset) a 
 NSMutableString instead of the more classical [[NSMutableString alloc] 
 init]?
 
 Yes.
 
 Thanks! (No side effect?)


Why would there be? Your just asking for a mutable copy of an empty string. It 
should be equivalent to [[NSMutableString alloc] initWithString:@]
--
David Duncan


___

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: Initializing a NSMutableString an odd way

2013-07-31 Thread Fritz Anderson
On 31 Jul 2013, at 1:28 PM, David Duncan david.dun...@apple.com wrote:

 On Jul 31, 2013, at 11:25 AM, Vincent Habchi vi...@macports.org wrote:
 
 
 Le 31 juil. 2013 à 20:15, Mike Abdullah mabdul...@karelia.com a écrit :
 
 I apologize if this question looks stupid or contrived. Here it is: is it 
 permissible to use [@“” mutableCopy] to initialize (or reset) a 
 NSMutableString instead of the more classical [[NSMutableString alloc] 
 init]?
 
 Yes.
 
 Thanks! (No side effect?)
 
 
 Why would there be? Your just asking for a mutable copy of an empty string. 
 It should be equivalent to [[NSMutableString alloc] initWithString:@]

Or [NSMutableString string].

— 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: Initializing a NSMutableString an odd way

2013-07-31 Thread Ken Thomases
On Jul 31, 2013, at 1:45 PM, Fritz Anderson wrote:

 On 31 Jul 2013, at 1:28 PM, David Duncan david.dun...@apple.com wrote:
 
 On Jul 31, 2013, at 11:25 AM, Vincent Habchi vi...@macports.org wrote:
 
 
 Le 31 juil. 2013 à 20:15, Mike Abdullah mabdul...@karelia.com a écrit :
 
 I apologize if this question looks stupid or contrived. Here it is: is it 
 permissible to use [@“” mutableCopy] to initialize (or reset) a 
 NSMutableString instead of the more classical [[NSMutableString alloc] 
 init]?
 
 Yes.
 
 Thanks! (No side effect?)
 
 
 Why would there be? Your just asking for a mutable copy of an empty string. 
 It should be equivalent to [[NSMutableString alloc] initWithString:@]
 
 Or [NSMutableString string].

With ARC, yes.  With MRR, they're different in terms of ownership.

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: Initializing a NSMutableString an odd way

2013-07-31 Thread Vincent Habchi
Thanks to all for answering,

 Why would there be? Your just asking for a mutable copy of an empty string. 
 It should be equivalent to [[NSMutableString alloc] initWithString:@«  »]

But much slower I expect, since it creates a NSString, takes a mutable copy, 
then implicitly releases the constant empty NSString.

BTW, what’s the difference between [[NSMutableString alloc] init] and 
[[NSMutableString alloc] initWithString:@“”]?

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: Initializing a NSMutableString an odd way

2013-07-31 Thread Sandor Szatmari
I think there are some overlooked subtleties as @ is a string literal.  
Retain and release are pretty much meaningless to it.

Sandor Szatmari

On Jul 31, 2013, at 15:28, Vincent Habchi vi...@macports.org wrote:

 Thanks to all for answering,
 
 Why would there be? Your just asking for a mutable copy of an empty string. 
 It should be equivalent to [[NSMutableString alloc] initWithString:@«  »]
 
 But much slower I expect, since it creates a NSString, takes a mutable copy, 
 then implicitly releases the constant empty NSString.
 
 BTW, what’s the difference between [[NSMutableString alloc] init] and 
 [[NSMutableString alloc] initWithString:@“”]?
 
 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/admin.szatmari.net%40gmail.com
 
 This email sent to admin.szatmari@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: Initializing a NSMutableString an odd way

2013-07-31 Thread Fritz Anderson
On 31 Jul 2013, at 2:28 PM, Vincent Habchi vi...@macports.org wrote:

 Thanks to all for answering,
 
 Why would there be? Your just asking for a mutable copy of an empty string. 
 It should be equivalent to [[NSMutableString alloc] initWithString:@«  »]
 
 But much slower I expect, since it creates a NSString, takes a mutable copy, 
 then implicitly releases the constant empty NSString.

NSString literals are baked into the application binary, and couldn't be 
deallocated if you tried. They are interned, so the code sample at the end of 
this message prints the same addresses for foo and empty no matter how you use 
them (even the product of -copy).

 BTW, what’s the difference between [[NSMutableString alloc] init] and 
 [[NSMutableString alloc] initWithString:@“”]?

The latter is redundant.

— 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: Initializing a NSMutableString an odd way

2013-07-31 Thread Greg Parker
On Jul 31, 2013, at 12:28 PM, Vincent Habchi vi...@macports.org wrote:
 David Duncan wrote:
 Why would there be? Your just asking for a mutable copy of an empty string. 
 It should be equivalent to [[NSMutableString alloc] initWithString:@«  »]
 
 But much slower I expect, since it creates a NSString, takes a mutable copy, 
 then implicitly releases the constant empty NSString.

For giggles I tried some NSMutableString allocation patterns into my 
microbenchmark test harness. 

Simple alloc/init is the fastest:

100  [[[NSMutableString alloc] init] release]
102  [[NSMutableString new] release]
109  [NSMutableString string]  // ARC enabled
117  [[@ mutableCopy] release]
119  @autoreleasepool { [NSMutableString string]; }  // ARC disabled
129  [[[NSMutableString alloc] initWithString:@] release]

(Smaller numbers are better. Numbers are getrusage(RUSAGE_SELF) time for 
1000 iterations, normalized to fastest=100. Your mileage may vary.)

ARC and non-ARC scores are the same within measurement noise, except for 
[NSMutableString string] where ARC can optimize the autoreleased return value 
so the test doesn't need to spin the autorelease pool. Note th


 BTW, what’s the difference between [[NSMutableString alloc] init] and 
 [[NSMutableString alloc] initWithString:@“”]?

Semantically there's no difference: you get the same string with the same 
retain count.


-- 
Greg Parker gpar...@apple.com Runtime Wrangler



___

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: Initializing a NSMutableString an odd way

2013-07-31 Thread Gary L. Wade
On 7/31/2013 1:01 PM, Greg Parker gpar...@apple.com wrote:


Simple alloc/init is the fastest:

100  [[[NSMutableString alloc] init] release]
102  [[NSMutableString new] release]
109  [NSMutableString string]  // ARC enabled
117  [[@ mutableCopy] release]
119  @autoreleasepool { [NSMutableString string]; }  // ARC disabled
129  [[[NSMutableString alloc] initWithString:@] release]

(Smaller numbers are better. Numbers are getrusage(RUSAGE_SELF) time for
1000 iterations, normalized to fastest=100. Your mileage may vary.)


Yeah, those numbers make sense from a theoretical standpoint (i.e.
without looking at the source code).  The +new call theoretically calls
+alloc/-init so would be slightly slower due to three method calls
(+new/+alloc/-init) and the -init call theoretically can do very little
until actual data is supplied while the others with actual data might add
the overhead of defining the actual data; theoretically, an empty string
is more data than no data yet to be filled.
--
Gary L. Wade
http://www.garywade.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: Initializing a NSMutableString an odd way

2013-07-31 Thread Vincent Habchi
Greg,

thanks for diverting some of your time testing this. As someone already 
commented, the results are somehow consistent with “common sense”, whatever 
that means (cf. below).

 ARC and non-ARC scores are the same within measurement noise, except for 
 [NSMutableString string] where ARC can optimize the autoreleased return value 
 so the test doesn't need to spin the autorelease pool. Note th

The end of your message somehow got lost in the cyberspace.

I thus suppose that to “clean” a NSMutableString, it is faster to recreate 
another one rather than use -deleteCharactersInRange: with a range covering the 
whole string.

Thanks again so much to everybody for your enlightening advice.

Vincent

—
“Common sense is the collection of prejudices acquired by age eighteen.” A. 
Einstein.



___

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: Initializing a NSMutableString an odd way

2013-07-31 Thread Greg Parker
On Jul 31, 2013, at 1:32 PM, Vincent Habchi vi...@macports.org wrote:
 Greg Parker wrote:
 ARC and non-ARC scores are the same within measurement noise, except for 
 [NSMutableString string] where ARC can optimize the autoreleased return 
 value so the test doesn't need to spin the autorelease pool. Note th
 
 The end of your message somehow got lost in the cyberspace.

The spurious text was an editing error. No content was harmed.


 I thus suppose that to “clean” a NSMutableString, it is faster to recreate 
 another one rather than use -deleteCharactersInRange: with a range covering 
 the whole string.

Not necessarily. If you have long string and you want to clear it and re-fill 
it with another long string, then it may be faster to use 
-deleteCharactersInRange: in order to avoid memory re-allocation overhead. But 
that possibility depends on precise implementation details of NSMutableString.


-- 
Greg Parker gpar...@apple.com Runtime Wrangler



___

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: Initializing a NSMutableString an odd way

2013-07-31 Thread Sandor Szatmari
Why not [aMutableString setString:@];?

Sandor Szatmari

On Jul 31, 2013, at 16:32, Vincent Habchi vi...@macports.org wrote:

 Greg,
 
 thanks for diverting some of your time testing this. As someone already 
 commented, the results are somehow consistent with “common sense”, whatever 
 that means (cf. below).
 
 ARC and non-ARC scores are the same within measurement noise, except for 
 [NSMutableString string] where ARC can optimize the autoreleased return 
 value so the test doesn't need to spin the autorelease pool. Note th
 
 The end of your message somehow got lost in the cyberspace.
 
 I thus suppose that to “clean” a NSMutableString, it is faster to recreate 
 another one rather than use -deleteCharactersInRange: with a range covering 
 the whole string.
 
 Thanks again so much to everybody for your enlightening advice.
 
 Vincent
 
 —
 “Common sense is the collection of prejudices acquired by age eighteen.” A. 
 Einstein.
 
 
 
 ___
 
 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/admin.szatmari.net%40gmail.com
 
 This email sent to admin.szatmari@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: Initializing a NSMutableString an odd way

2013-07-31 Thread Vincent Habchi
Le 31 juil. 2013 à 22:38, Greg Parker gpar...@apple.com a écrit :

 Not necessarily. If you have long string and you want to clear it and re-fill 
 it with another long string, then it may be faster to use 
 -deleteCharactersInRange: in order to avoid memory re-allocation overhead. 
 But that possibility depends on precise implementation details of 
 NSMutableString.

So I guess profiling is once more everybody’s friend.
Thanks anyhow once more for taking the time to answer my very minor inquiry.

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: Initializing a NSMutableString an odd way

2013-07-31 Thread Alex Zavatone

On Jul 31, 2013, at 4:01 PM, Greg Parker wrote:

 On Jul 31, 2013, at 12:28 PM, Vincent Habchi vi...@macports.org wrote:
 David Duncan wrote:
 Why would there be? Your just asking for a mutable copy of an empty string. 
 It should be equivalent to [[NSMutableString alloc] initWithString:@«  »]
 
 But much slower I expect, since it creates a NSString, takes a mutable copy, 
 then implicitly releases the constant empty NSString.
 
 For giggles I tried some NSMutableString allocation patterns into my 
 microbenchmark test harness. 

I'm rather fascinated with these and wrote the one that profiled the speed of 
all variable classes and functions of Lingo back in the Director days.

Once we knew just how fast each of the variable allocations and functions took 
when compared to each other, this made it really easy to determine what 
approaches not to use in the interests of performance, or where we could change 
code for the sake of readability over performance.

By any chance is this harness open or is it a private project?  Of course, we 
have instruments, but it would be rather great to simply be able to plug in 
some operations and know the speed of them and their speed relative to each 
other.

 Simple alloc/init is the fastest:
 
 100  [[[NSMutableString alloc] init] release]
 102  [[NSMutableString new] release]
 109  [NSMutableString string]  // ARC enabled
 117  [[@ mutableCopy] release]
 119  @autoreleasepool { [NSMutableString string]; }  // ARC disabled
 129  [[[NSMutableString alloc] initWithString:@] release]
 
 (Smaller numbers are better. Numbers are getrusage(RUSAGE_SELF) time for 
 1000 iterations, normalized to fastest=100. Your mileage may vary.)
 
 ARC and non-ARC scores are the same within measurement noise, except for 
 [NSMutableString string] where ARC can optimize the autoreleased return value 
 so the test doesn't need to spin the autorelease pool. Note th
 

Your note got cut off near the en







___

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