Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.)

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

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.).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top