maximum number of methods supported in C# class
-
Seeing Assembly metatokens takes the following form: 00 000000, where the former is the type, and the latter is the code, I would say the maximum number of methoddef's in an assembly would be limited to 24-bits, iow 16.7 million. Dunno if there is a limitation on a classes though.
**
xacc.ide-0.2.0.75 - now with C# 3.5 support and Navigation Bar!
**
-
Thanks for Info, But I read in one article that E-Bay once hits the compiler limit in max number of methods in single class. Thanks, Vythees
IIRC ebay is running its servers using c++ and ISAPI, not .net.
-- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
-
when in trouble, switch to Win64. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
IIRC ebay is running its servers using c++ and ISAPI, not .net.
-- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
-
-
Seeing Assembly metatokens takes the following form: 00 000000, where the former is the type, and the latter is the code, I would say the maximum number of methoddef's in an assembly would be limited to 24-bits, iow 16.7 million. Dunno if there is a limitation on a classes though.
**
xacc.ide-0.2.0.75 - now with C# 3.5 support and Navigation Bar!
**
-
Not research, I know it from working with the spec :) And those values can be accessed from .NET 2 (nowadays).
**
xacc.ide-0.2.0.75 - now with C# 3.5 support and Navigation Bar!
**
-
There is a limit, I remember someone a while ago (I've got a feeling in this forum) hit a limit on the number of fields in anycase that they could include in a class. Buggered if I can remember where it was or what it was :sigh:
Maybe you are thinking about the parameter limit, that is 16383/4.
**
xacc.ide-0.2.0.75 - now with C# 3.5 support and Navigation Bar!
**
-
Maybe you are thinking about the parameter limit, that is 16383/4.
**
xacc.ide-0.2.0.75 - now with C# 3.5 support and Navigation Bar!
**
-
Just curiosity ;) , Did anyone have idea of how many methods a single C# class can allow ? I heard, it is compiler dependent If true, what is the maximum no allowed by the standard compiler ? Thanks, Vythees
I let this run for an hour to get to 5000 before I gave up. Someone with more CPU and physical RAM than I have should run it and see where it ends...
using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom.Compiler;
namespace MethodCountLimitFinder {
class Program {
static void Main(string[] args) {
Int32 methodCount = 1;
Microsoft.CSharp.CSharpCodeProvider cscp = new Microsoft.CSharp.CSharpCodeProvider();
ICodeCompiler icc = cscp.CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
CompilerResults cr = null;
string pre = "using System;" + Environment.NewLine +
Environment.NewLine +
"namespace Tester {" + Environment.NewLine +
" class Test {" + Environment.NewLine;
string post = " }" + Environment.NewLine +
"}";
string inner = string.Empty;
while (true) {
inner += " public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine +
" return 42;" + Environment.NewLine +
" }" + Envi -
I let this run for an hour to get to 5000 before I gave up. Someone with more CPU and physical RAM than I have should run it and see where it ends...
using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom.Compiler;
namespace MethodCountLimitFinder {
class Program {
static void Main(string[] args) {
Int32 methodCount = 1;
Microsoft.CSharp.CSharpCodeProvider cscp = new Microsoft.CSharp.CSharpCodeProvider();
ICodeCompiler icc = cscp.CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
CompilerResults cr = null;
string pre = "using System;" + Environment.NewLine +
Environment.NewLine +
"namespace Tester {" + Environment.NewLine +
" class Test {" + Environment.NewLine;
string post = " }" + Environment.NewLine +
"}";
string inner = string.Empty;
while (true) {
inner += " public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine +
" return 42;" + Environment.NewLine +
" }" + EnviSean Michael Murphy wrote:
while (true) { inner += " public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine + " return 42;" + Environment.NewLine + " }" + Environment.NewLine; cr = icc.CompileAssemblyFromSource(cp, pre + inner + post); if (cr.Errors.Count > 0) break; methodCount++; if (methodCount % 10 == 0) System.Console.WriteLine(methodCount.ToString()); }
Sean Michael Murphy wrote:
Someone with more CPU and physical RAM than I have should run it and see where it ends...
No wonder, always use StringBuilder for string concatenation in a loop.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
-
I agree, I think then there should be the same constraint exist in .NET class also. Thanks, Vythees
Even assuming there is a constraint, you're talking about 2 totally different languages so why should they be the same?
-- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
-
Sean Michael Murphy wrote:
while (true) { inner += " public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine + " return 42;" + Environment.NewLine + " }" + Environment.NewLine; cr = icc.CompileAssemblyFromSource(cp, pre + inner + post); if (cr.Errors.Count > 0) break; methodCount++; if (methodCount % 10 == 0) System.Console.WriteLine(methodCount.ToString()); }
Sean Michael Murphy wrote:
Someone with more CPU and physical RAM than I have should run it and see where it ends...
No wonder, always use StringBuilder for string concatenation in a loop.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
-
Well it can be said too often, but it's appropriate here.
-
Sean Michael Murphy wrote:
while (true) { inner += " public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine + " return 42;" + Environment.NewLine + " }" + Environment.NewLine; cr = icc.CompileAssemblyFromSource(cp, pre + inner + post); if (cr.Errors.Count > 0) break; methodCount++; if (methodCount % 10 == 0) System.Console.WriteLine(methodCount.ToString()); }
Sean Michael Murphy wrote:
Someone with more CPU and physical RAM than I have should run it and see where it ends...
No wonder, always use StringBuilder for string concatenation in a loop.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
dnh wrote:
No wonder, always use StringBuilder for string concatenation in a loop.
Hmmm. Interesting. When I originally undertook to code this snippet to try to figure an answer to this guys question, optimization was pretty far from my mind. I mean, I cranked the original bit of code out in 15 minutes (or so) and had originally coded it so the methods would be recreated every time. I took another 5 minutes and optimized it so that only 1 method (the new one) would have to be concatenated to the "guts", which was then stuck in between the fixed "header" and "footer" of the class. It ran slowly, but I assumed that most of the overhead was in the actual code compilation (compiling classes of 15000 lines), and not a little bit of string concatenation. So I've re-written it using
StringBuilder
and timed both versions for 500 iterations. The original code did 500 iterations on my PC in 161.5222 seconds. This version:StringBuilder inner = new StringBuilder();
DateTime startTime = DateTime.Now;
for (Int32 i = 0; i < 500; i++) {
inner.Append(" public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine +
" return 42;" + Environment.NewLine +
" }" + Environment.NewLine);
StringBuilder code = new StringBuilder(pre);
code.Append(inner);
code.Append(post);
cr = icc.CompileAssemblyFromSource(cp, code.ToString());
if (cr.Errors.Count > 0)
break;
methodCount++;
if (methodCount % 10 == 0)
System.Console.WriteLine(methodCount.ToString());
}
TimeSpan ts = DateTime.Now - startTime;
System.Console.WriteLine(ts.TotalSeconds);did it in 160.111. Much less that 1% slower. Not a string concatenation to be found, except for the line joins. Anything to add? Thanks. Sean
-
dnh wrote:
No wonder, always use StringBuilder for string concatenation in a loop.
Hmmm. Interesting. When I originally undertook to code this snippet to try to figure an answer to this guys question, optimization was pretty far from my mind. I mean, I cranked the original bit of code out in 15 minutes (or so) and had originally coded it so the methods would be recreated every time. I took another 5 minutes and optimized it so that only 1 method (the new one) would have to be concatenated to the "guts", which was then stuck in between the fixed "header" and "footer" of the class. It ran slowly, but I assumed that most of the overhead was in the actual code compilation (compiling classes of 15000 lines), and not a little bit of string concatenation. So I've re-written it using
StringBuilder
and timed both versions for 500 iterations. The original code did 500 iterations on my PC in 161.5222 seconds. This version:StringBuilder inner = new StringBuilder();
DateTime startTime = DateTime.Now;
for (Int32 i = 0; i < 500; i++) {
inner.Append(" public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine +
" return 42;" + Environment.NewLine +
" }" + Environment.NewLine);
StringBuilder code = new StringBuilder(pre);
code.Append(inner);
code.Append(post);
cr = icc.CompileAssemblyFromSource(cp, code.ToString());
if (cr.Errors.Count > 0)
break;
methodCount++;
if (methodCount % 10 == 0)
System.Console.WriteLine(methodCount.ToString());
}
TimeSpan ts = DateTime.Now - startTime;
System.Console.WriteLine(ts.TotalSeconds);did it in 160.111. Much less that 1% slower. Not a string concatenation to be found, except for the line joins. Anything to add? Thanks. Sean
Sean Michael Murphy wrote:
Anything to add?
I'd agree that most time takes compilation, but the thing about string concatenation with + is that it's -unlike compilation - completely unnecessary. And I don't think that using StringBuilder for concatenating strings in big loops is optimalization - I think it's something you should do without thinking. btw you're still allocating 7 or so strings in
inner.Append(" public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine + " return 42;" + Environment.NewLine + " }" + Environment.NewLine);
every cycle, that's 3500 unnecessary allocations :) Anyway, cool way to check for number of methods limit indeed.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
-
dnh wrote:
No wonder, always use StringBuilder for string concatenation in a loop.
Hmmm. Interesting. When I originally undertook to code this snippet to try to figure an answer to this guys question, optimization was pretty far from my mind. I mean, I cranked the original bit of code out in 15 minutes (or so) and had originally coded it so the methods would be recreated every time. I took another 5 minutes and optimized it so that only 1 method (the new one) would have to be concatenated to the "guts", which was then stuck in between the fixed "header" and "footer" of the class. It ran slowly, but I assumed that most of the overhead was in the actual code compilation (compiling classes of 15000 lines), and not a little bit of string concatenation. So I've re-written it using
StringBuilder
and timed both versions for 500 iterations. The original code did 500 iterations on my PC in 161.5222 seconds. This version:StringBuilder inner = new StringBuilder();
DateTime startTime = DateTime.Now;
for (Int32 i = 0; i < 500; i++) {
inner.Append(" public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine +
" return 42;" + Environment.NewLine +
" }" + Environment.NewLine);
StringBuilder code = new StringBuilder(pre);
code.Append(inner);
code.Append(post);
cr = icc.CompileAssemblyFromSource(cp, code.ToString());
if (cr.Errors.Count > 0)
break;
methodCount++;
if (methodCount % 10 == 0)
System.Console.WriteLine(methodCount.ToString());
}
TimeSpan ts = DateTime.Now - startTime;
System.Console.WriteLine(ts.TotalSeconds);did it in 160.111. Much less that 1% slower. Not a string concatenation to be found, except for the line joins. Anything to add? Thanks. Sean
Hi,
while( true) { do { inner.Append(" public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine + " return 42;" + Environment.NewLine + " }" + Environment.NewLine); methodCount++; } while ((methodCount % 1000) != 0); cr = icc.CompileAssemblyFromSource(cp, pre + inner.ToString() + post); if (cr.Errors.Count > 0) break; System.Console.WriteLine(methodCount.ToString() + " Compiled successfuly ==> so not succed"); } System.Console.WriteLine(methodCount + " may be approximately to -1000 of method count");
I modified slightly your code as the above and executed, Its going on till 100000 ( above 1 lakh ), My machine got down, So I planned to run today night. Now I feeling, I shouldn't ask this question first of all ;) Thanks, Vythees -- modified at 5:24 Tuesday 3rd July, 2007 Thanks, Vythees -
Hi,
while( true) { do { inner.Append(" public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine + " return 42;" + Environment.NewLine + " }" + Environment.NewLine); methodCount++; } while ((methodCount % 1000) != 0); cr = icc.CompileAssemblyFromSource(cp, pre + inner.ToString() + post); if (cr.Errors.Count > 0) break; System.Console.WriteLine(methodCount.ToString() + " Compiled successfuly ==> so not succed"); } System.Console.WriteLine(methodCount + " may be approximately to -1000 of method count");
I modified slightly your code as the above and executed, Its going on till 100000 ( above 1 lakh ), My machine got down, So I planned to run today night. Now I feeling, I shouldn't ask this question first of all ;) Thanks, Vythees -- modified at 5:24 Tuesday 3rd July, 2007 Thanks, Vytheesvytheeswaran wrote:
Now I feeling, I shouldn't ask this question first of all
Don't be crazy. I enjoyed thinking about it. Sean
-
Sean Michael Murphy wrote:
Anything to add?
I'd agree that most time takes compilation, but the thing about string concatenation with + is that it's -unlike compilation - completely unnecessary. And I don't think that using StringBuilder for concatenating strings in big loops is optimalization - I think it's something you should do without thinking. btw you're still allocating 7 or so strings in
inner.Append(" public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine + " return 42;" + Environment.NewLine + " }" + Environment.NewLine);
every cycle, that's 3500 unnecessary allocations :) Anyway, cool way to check for number of methods limit indeed.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
dnh wrote:
I think it's something you should do without thinking.
Never do anything without thinking.
-
I let this run for an hour to get to 5000 before I gave up. Someone with more CPU and physical RAM than I have should run it and see where it ends...
using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom.Compiler;
namespace MethodCountLimitFinder {
class Program {
static void Main(string[] args) {
Int32 methodCount = 1;
Microsoft.CSharp.CSharpCodeProvider cscp = new Microsoft.CSharp.CSharpCodeProvider();
ICodeCompiler icc = cscp.CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
CompilerResults cr = null;
string pre = "using System;" + Environment.NewLine +
Environment.NewLine +
"namespace Tester {" + Environment.NewLine +
" class Test {" + Environment.NewLine;
string post = " }" + Environment.NewLine +
"}";
string inner = string.Empty;
while (true) {
inner += " public Int32 Method" + methodCount.ToString() + "() {" + Environment.NewLine +
" return 42;" + Environment.NewLine +
" }" + EnviA) I don't think there's any need for including the
NewLine
s. B) Why step by one? Why not doublemethodCount
after each successful compile?