implications of a large partial class

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?

11

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

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật