• 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: Copy and release
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Copy and release


  • Subject: Re: Copy and release
  • From: Ricky Sharp <email@hidden>
  • Date: Thu, 21 Oct 2004 15:49:56 -0500

On Oct 21, 2004, at 3:05 PM, Evan Schoenberg wrote:

On Oct 21, 2004, at 10:35 AM, Nicko van Someren wrote:

It is quite common when writing programs for Cocoa that a data structure needs to be mutable while it is being filled but after that it would be useful for it become immutable in order to take advantage of the efficiencies that affords once the object has been set up.

This has been a wonder of mine for a long time in Cocoa.. do you have information on how precisely the immutable objects are more efficient? I've heard the claim before but never seen hard data either confirming or denying it.

The two things I've read about regarding the advantages are:

(1) Sizes are well-known, so things like memory allocation and copying data can be more efficient.

Compare this to general-purpose containers that often resize by large blocks of memory and thus waste space. For example, a string implementation may store data in blocks of say 256 bytes each. Very short strings then waste quite a bit of memory.

I would also imagine methods like length would simply return some constant value rather than having to "figure things out" (e.g. executing a strlen).

(2) Because data is immutable, there's a chance to share references. I'll mention strings again as an example. Two immutable strings that contain the same exact data could be stored such that only one copy of the data exists. You'd then have two references to that data.

I believe in the Java world, such an optimization is called "string interning".


When I worked at Motorola in the late 90's, these two concepts were extremely important for the embedded device we were building. How such optimizations impact the desktop with huge storage and tons more CPU power, I don't know. I would assume that doing things in loops (esp nested) could show some tangible differences; your profiler would know for sure.

___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com

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

This email sent to email@hidden
  • Follow-Ups:
    • Re: Copy and release
      • From: Scott Stevenson <email@hidden>
References: 
 >Copy and release (From: Nicko van Someren <email@hidden>)
 >Re: Copy and release (From: Evan Schoenberg <email@hidden>)

  • Prev by Date: Cocoa & Excel
  • Next by Date: Re: Toolbar Item Validation in a Panel
  • Previous by thread: Re: Copy and release
  • Next by thread: Re: Copy and release
  • Index(es):
    • Date
    • Thread