|
|
Cocoa/CoreFoundation Collection Capacity Conflict
Don’t you love alliteration? Coconspirator Len Case recently killed a bug. Fortunately I saw his commit message fly by and learned something. Witness:
#if CRASH_CITY Set Set it to The crash illustrates the difference in handling collection capacity between Cocoa and Core Foundation. Even when they’re ostensibly the same. (This example uses When creating a collection using Cocoa, capacity is advisory. If you stuff more objects into a collection than its capacity — as this code snippet does — you may suffer a performance hit, but your program will continue to work. This stands in contrast to collection creation using Core Foundation: its capacity is strict. Try to put more objects into a Core Foundation-created collection, and you won’t even get an exception — you’ll get but an ol’timey crash. Unfortunately, while Core Foundation’s capacity handling is strict, it’s also sloppy. By all rights, with That’s because Core Foundation silently internally increases the capacity you’ve handed it, to match some number Apple has decided results in better performance. (Here I would have accused Apple of Sharking lots o’ CF code to determine the optimum number, but they seem way too human-rounded to me. Core Foundation’s capacity slop makes it depressingly easy to introduce an off-by-one (or even off-by-a-dozen) when specifying a capacity. Technically, your program is incorrect, but you’ll never notice since CF has covered up your mistake. Because Core Foundation collections have a nasty habit of making their way up to Cocoa-land as seemingly innocent mutable collections (that won’t blow up if you happen to put one more object into them), it’s probably best to avoid specifying a nonzero Wednesday, December 27, 2006
|
Contact Me Topics RSS Feed Linkblog
Bill Bumgarner Brent Simmons Daniel Jalkut Dave Dribin Eric Albert Eric Rescorla Eric Sink Greg Miller Gus Mueller Jeremy Zawodny John Gruber Mark Dalrymple Michael Tsai Peter Ammon Raymond Chen Ryan Wilcox Scott Stevenson Steven Frank The Daily WTF we hates software Wil Shipley |
Copyright © 1997-2009 Jonathan 'Wolf' Rentzsch. All rights reserved.
Questions? Comments? Contact Me.