views:

181

answers:

4

I have a custom class I use frequently on my projects. This class has several methods but not all of them are used in every project.

My question is: including this class on a Project will bloat the project with useless code or will the compiler just include the methods used?

I mean, if my class has 30 methods but just 4 are being used in a given project will the compiler include also the other unused 26 or just the 4 used in the final product?

In case it includes all, is there a way to force it to disregard the unused methods and trim the binary to the minimum?

+4  A: 

If the other 26 methods have code in @implementation, then yes, they will be used in the final product.

The reason is because of the runtime system. Even if you didn't use that 26 methods in compile time, there's no guarantee they won't be referred in runtime (remember NSSelectorFromString and -performSelector:).

I don't know if there's a way to force remove these code. (-dead_strip doesn't work.)

KennyTM
That's what I thought originally too. jessecurry made me unsure of it.
Johannes Rudolph
@jesse: No no no, it's the other way round. Those ObjC code is never removed, even if you add `__attribute__((unused))` (which is ignored.)
KennyTM
+4  A: 

The linker supports dead-stripping, if you turn it on unused code should not cause any bloat.

From the Apple docs:

The static linker (ld) supports the removal of unused code and data blocks from executable files. This process (known as dead-code stripping) helps reduce the overall size of executables, which in turn improves performance by reducing the memory footprint of the executable. It also allows programs to link successfully when unused code refers to an undefined symbol (instead of resulting in a link error).

Dead-code stripping is not limited to removing only unused functions and executable code from a binary. The linker also removes any unused symbols and data that reside in data blocks. Such symbols might include global variables, static variables, and string data, among others.

When dead-code stripping is enabled, the static linker searches for code that is unreachable from an initial set of live symbols and blocks.

jessecurry
thanks!!!!!!!!!!
Digital Robot
To be hones, I'm not sure if this is correct with Objective-C. Since it is a dynamic language, methods can be called by name. So the linker would not know what to strip and what to keep.
St3fan
Works wonders for me! http://is.gd/bP8Do (developer.apple.com)
Joe D'Andrea
I'm fairly sure that St3fan and KennyTM are correct. Dead code stripping is only useful for C and C++. See also http://seriot.ch/blog.php?article=20080728.
Steve Madsen
+1  A: 

My question is: including this class on a Project will bloat the project with useless code or will the compiler just include the methods used?

I think you are talking about including the header and implementation of your helper class. This will increase the binary size. As pointed out by jessecurry the linker supports dead-end stripping. This is bad as there's always the possibility someone wants to link with the public api of your binary (fortunately this is not the case as dynamic linking is not allowed on iphone but consider other platforms). But I bet the difference in size is way too marginal to be significant.

The most impact in terms of size is usually the resources you include with your application (images, strings etc.).

Johannes Rudolph
+2  A: 

Sounds like you need to refactor and rename the big fat mamma class.

Flinkman
Kudos for an answer that really makes sense. There is no reason for a big-fat-mamma class in Objective-C. Libraries are too easy to slim down and include in the .pch file when needed to use big-fat-mamma classes anymore.
Jann