Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm writing a middle layer between vendor code and company business logic. Part of the exercise is to insulate our business logic from vendor's types to make swapping out vendor code easier.

As such, I'm writing a lot of extension methods that do translation to and from. I'm placing these extensions in public static partial classes. For organizational purposes and simply because, for static extension methods, the class it self does not matter(is this a correct assumption btw?)

EDIT: To be clear I'm creating a lot of different appropriately named source files, that all implemented one partial class

My question is, how much code is too much for a partial class. I could conceive having 100's of methods in a single class(at some point). When does this become a burden for the compiler and CIL layer? Is there really a limit if these partials classes hold no state/memory?

share|improve this question
4  
It's more likely to be a burden on the developer long before the compiler/CIL have a problem with it. –  Dan Pichelman Mar 28 '13 at 21:17
3  
I suspect (but cannot prove) that the compiler can handle many 100's of methods in a single class without issue. Typing that many methods and keeping them straight may be difficult. Good luck! –  Dan Pichelman Mar 28 '13 at 21:29
1  
@svick: You seem to have a strange idea of "proof". –  Peter K. Mar 28 '13 at 21:52
1  
@Peter K.: "Proof" is a word with multiple meanings, only one of which is "mathematical proof". –  Michael Shaw Mar 28 '13 at 22:15
2  
@PeterK. The question is “can the compiler handle 100s of methods in a single class?” You can answer that by trying to do that and succeeding or failing. The question isn't “are there any specific situations where having 100s of methods in a single class can be a problem for the compiler?” –  svick Mar 29 '13 at 0:08
show 6 more comments

1 Answer 1

up vote 7 down vote accepted

TLDR: A single class with hundreds of methods is just fine, you won't get into problems unless you have thousands of methods.

To test this properly, I have created the following T4 template in Visual Studio:

class LotsOfMethods
{
    <# for (int i = 0; i < 100; i++) { #>
    public static void M<#= i #>()
    {
    }
    <# } #>
}

By varying the constant in the loop, I can find out how do various numbers of methods behave. As it turns out, 100, 1 000, 10 000 or even 100 000 methods compile just fine (though in the latter cases, the compilation takes longer than a single project with one or two classes normally would and VS also sometimes freezes for few seconds).

When I tried to actually run the code, the cases with 100, 1 000 or 10 000 methods were all fine. The interesting part is that when you try to use the class with 100 000 methods, you'll get an exception:

System.TypeLoadException: Type 'LotsOfMethods' from assembly 'ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' contains more methods than the current implementation allows.

Specifically, this exception happens when the class has 65 521 (216-15) methods or more. (An interesting question is why exactly this number. What does the 15 mean?)

Another observation is that more methods in a class make JIT compilation slower. (Or is it more methods in an assembly? I haven't tested that.) When executing 500 of the methods I get the following results:

For a class with 65 520 methods:

For the first time: 2,2079481 s
For the second time: 0,0003259 s

With 1 000 methods:

For the first time: 0,0545898 s
For the second time: 0,0003138 s

And 500 methods:

For the first time: 0,0361426 s
For the second time: 0,0003226 s

The results were measured using the following code:

var sw = Stopwatch.StartNew();

int n = 500; // this is constant no matter how many methods does the class have

for (int i = 0; i < n; i++)
{
    typeof(LotsOfMethods).GetMethod(string.Format("M{0}", i)).Invoke(null, null);
}

var first = sw.Elapsed.TotalSeconds;
sw.Restart();

for (int i = 0; i < n; i++)
{
    typeof(LotsOfMethods).GetMethod(string.Format("M{0}", i)).Invoke(null, null);
}

var second = sw.Elapsed.TotalSeconds;

Console.WriteLine("For the first time: {0} s", first);
Console.WriteLine("For the second time: {0} s", second);
share|improve this answer
    
thank you for the very thorough analysis –  Dmitry Mar 29 '13 at 2:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.