473,383 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Mixed Mode Slow?

I've been trying a mixed mode project but I'm having speed problems.

I was using a DLL and linking into it from C#. I thought I'd try and stick
the C# functionality and the raw unmanaged code into a mixed mode project.

It works but it's incredibly slow. All the optimization settings are the
same. All the code which was in the DLL is compiled with the /clr off. Only
the bits which were in C# have been re-written in managed C++ (and I've
checked them - they're not the cause).

However it's now five times slower than it was.

Any ideas or should I just go back to the original design?

Jos
Nov 16 '05 #1
10 3170
Jos,
I've been trying a mixed mode project but I'm having speed problems.

I was using a DLL and linking into it from C#. I thought I'd try and stick
the C# functionality and the raw unmanaged code into a mixed mode project.

It works but it's incredibly slow. All the optimization settings are the
same. All the code which was in the DLL is compiled with the /clr off. Only the bits which were in C# have been re-written in managed C++ (and I've
checked them - they're not the cause).

However it's now five times slower than it was.

Any ideas or should I just go back to the original design?


It might very well depend a lot on what you're doing. For example: is the
interface between your MC++ wrapper and the unmanaged C++ library very
chatty? If so, that will cause too many managed->unmanaged->managed
transitions, which can be costly. You can avoid this by making your
interface more granular, and sometimes even by compiling some of your
unmanaged code with /clr.

--
Tomas Restrepo
to****@mvps.org
Nov 16 '05 #2
Thomas
It might very well depend a lot on what you're doing. For example: is the
interface between your MC++ wrapper and the unmanaged C++ library very
chatty? If so, that will cause too many managed->unmanaged->managed
transitions, which can be costly. You can avoid this by making your
interface more granular, and sometimes even by compiling some of your
unmanaged code with /clr.


Thanks. However I don't think this is the issue. It's a very chunky
interface and the number of transitions is very limited. I did also do some
experiments along these lines and these indicated that the problem was not
in the transitions.

The only thing I can think of is - either - that turning off the clr is
being ignored sometimes - or - that the optimizations I'm applying are being
ignored. It feels like I'm running a debug version. Very odd.

Out of interest I compiled the whole thing as managed. Very impressive that
it compiles without even a murmur. But... it was ten times slower - ouch!

I'm convinced this must be possible so I think my next step is to compile
the native code as a library and import that into my mixed mode project. If
that doesn't work then I just don't know.

All suggestions gratefully received.

Jos
Nov 16 '05 #3
No - exactly the same as before. Still five times slower.

I had to turn off some optimizations because they were incompatible with the
CLR flag. Can they really make that much difference?

This is a complex real world app not a demo so I would have thought that
things like whold program optimization might make a difference of 10% not
500%.

Anyone from MS care to comment.

Jos
Nov 16 '05 #4
Hi Jos,
No - exactly the same as before. Still five times slower.

I had to turn off some optimizations because they were incompatible with the CLR flag. Can they really make that much difference?


Can you post the set of flags you're compiling with?
Also, can you give us some idea of the kind of MC++ code you're writing
here? That should give us a clue as to what's going on....
--
Tomas Restrepo
to****@mvps.org
Nov 16 '05 #5
> Can you post the set of flags you're compiling with?
Also, can you give us some idea of the kind of MC++ code you're writing
here? That should give us a clue as to what's going on....


Well my latest incarnation is written as an x86 library which is then linked
with a MC++ shell.

The x86 lib is compiled with - /02 (Maximise Speed), /Ob2 (Any Suitable
Inline Function Expansion), No global optimization (coz it's incompatible
with CLR). Otherwise it's a standard release build,

The .NET shell has the same settings but has global optimizations turned on
(I think this may be being ignored because it's incompatible with .CLR).
Also has Favor Fast Code turned on. The linker optimization is standard.

The shell is a shell - not much there. Typically things like this
(STARTFUNCTION and ENDFUNCTION are a simple set of try/catch handlers for
SEH)...

[Category("Settings"), Description("Add a page at a specified location -
return the page ID")]
int AddPage(int page)
{
STARTFUNCTION()
return mPDF->AddPage(page);
ENDFUNCTION()
}

All suggestions gratefully received.

Does anyone know if MS use MC++ for any of the .NET Framework? Or do they
package all their x86 code in standard x86 DLLs and call it that way?

I can't believe it was used for .NET 1.0 because the AppDomain bug meant
that anything written using it wouldn't work under ASP.NET. Has anything
changed with 1.1?

Jos
Nov 16 '05 #6
Number five needs more data.......

We have a 3D engine for massive financial visualization and we use C#
to implement the interface. While we wouldn't recommend C# for vector
processing, it's pretty snappy at what it does. I suspect somethings
at cross purposes.

First principles
< begin a set of opinionated statements>
C# is based on a intermediate virtual system, so it's going to run
slower than C++ native code, and I'm really getting sick of the 'but
no' from the msft folks on this. First because they keep arguing with
how the facts came to be and secondly because they can't claim their
virtualization has zero overhead, and with no overhead the cost is
still higher because they're not doing straight stack pushes and pops
to get parameters like a native app can. C# probably costs an order
of magnitude or more in potential performance. In most cases this
difference is so small as to be pointless, but that doesn't mean its
not there.

So, are you doing any computationally horribly complex or long
operations ? We have operations where we need to normalize several
100K values. That kind of thing goes in C++. Anything that talks to
the user or the net is fine, C# is not shabby, I'm just saying it's
not perfect.

Data transport between dlls by straight function call is cheap. Real
cheap. It's also unmanaged. So's the Windows kernel, but people from
MSFT.NET seem to keep forgetting that when casting elderberry wine and
hampsters at unmanaged code. Transition between the .net managed
architecture and the unmanaged architecture is something I'm pretty
sure I don't want to know anything about. Probably _worse_ than
making sausage. In any case it's very expensive.

..Net makes it real easy to use COM based communications. That's a
wonderful thing. If you haven't dealt with COM before, it's very very
expensive compared to traditional function calls. Really outrageously
expensive if the component you're talking to is in a different
process. And if it's off the machine, hey you're using the DAL du
jour.

< begin a set of highly opinionated statements>

The rules of thumb I use, and the answers to them might help here

< this goes in C++>
for(i = 0;i < 100K or so;i++)
gruesomely complex numerical operation or data diddling
Stay managed as long as possible on the C# side. Technically, we
never do _anything_ unmanaged on the C# side except when we actually
call our DLL functions.

COM is nice but learning to use arrays is twice as nice. Never ever
make 10 com calls when 1 call with an array will do.

Remember that C# garbage collects. If your C++ and C# are too tightly
bound, you'll see a regular 'anti-heartbeat'.

It's nice that C++ can generate IL code. Pointless, but nice. C# is a
better tool for managed code, C++ for unmanaged. For that matter,
it's possible to write C#, generate IL, and then decompile Java, but
thats also pointless. Funny, but pointless. The bad news - managed
C++ code is not C++ with a wee few nits, it's slower. If it wasn't,
it wouldn't be secure. Don't write managed C++, write managed C#.

Unmanaged debugging is real damn slow, compared to either managed c#
debugging or native C++ debugging. Forget about unmanaged debugging if
you turn it on, start it off in C#, and the system immediately starts
running at 1Khz. I haven't worked this all out yet, but I do know
that using a mixture of DLL function and COM calls when one object of
my affection was Excel caused me to form this rule. It was
interesting for the first few minutes as it tried to deal with Excel,
but I guess futility isn't a concept the debugger understands.

Anyway, hope this helps some. Check out the profiler you can get of
the msft site, make sure you only use unmanaged debugging when you
absolutely have to (it is way cool, that is true), make your com calls
efficient if you have to make them at all, and dont make the system
keep jumping between managed and unmanaged code.

regards
mmm
"Jos Vernon" <jo********@websupergoo.com> wrote in message news:<eO**************@TK2MSFTNGP12.phx.gbl>...
Can you post the set of flags you're compiling with?
Also, can you give us some idea of the kind of MC++ code you're writing
here? That should give us a clue as to what's going on....


Well my latest incarnation is written as an x86 library which is then linked
with a MC++ shell.

The x86 lib is compiled with - /02 (Maximise Speed), /Ob2 (Any Suitable
Inline Function Expansion), No global optimization (coz it's incompatible
with CLR). Otherwise it's a standard release build,

The .NET shell has the same settings but has global optimizations turned on
(I think this may be being ignored because it's incompatible with .CLR).
Also has Favor Fast Code turned on. The linker optimization is standard.

The shell is a shell - not much there. Typically things like this
(STARTFUNCTION and ENDFUNCTION are a simple set of try/catch handlers for
SEH)...

[Category("Settings"), Description("Add a page at a specified location -
return the page ID")]
int AddPage(int page)
{
STARTFUNCTION()
return mPDF->AddPage(page);
ENDFUNCTION()
}

All suggestions gratefully received.

Does anyone know if MS use MC++ for any of the .NET Framework? Or do they
package all their x86 code in standard x86 DLLs and call it that way?

I can't believe it was used for .NET 1.0 because the AppDomain bug meant
that anything written using it wouldn't work under ASP.NET. Has anything
changed with 1.1?

Jos

Nov 16 '05 #7
Jos Vernon wrote:
Does anyone know if MS use MC++ for any of the .NET Framework? Or do
they package all their x86 code in standard x86 DLLs and call it that
way?
I believe that the native parts of the BCL predate MC++. If you look in the
Rotor (aka SSCLI) source code, you'll see that the native functions use a
special attribute ([internal]) in the C# code to cause the compiler to call
a native function. The native functions are implemented in normal C++,
using a lot of macros to create definitions for the functions. I don't
think this kind of binding is documented anywhere - it's meant to be
internal to the BCL implementation, afterall.

I can't believe it was used for .NET 1.0 because the AppDomain bug
meant that anything written using it wouldn't work under ASP.NET. Has
anything changed with 1.1?


Not in the way the native portions of the BCL are implemented, no.

-cd
Nov 16 '05 #8
Hi Jos,
Well my latest incarnation is written as an x86 library which is then linked with a MC++ shell.

The x86 lib is compiled with - /02 (Maximise Speed), /Ob2 (Any Suitable
Inline Function Expansion), No global optimization (coz it's incompatible
with CLR). Otherwise it's a standard release build,
I'm assuming you mean /GL and not /Og here, right?

The .NET shell has the same settings but has global optimizations turned on (I think this may be being ignored because it's incompatible with .CLR).
Also has Favor Fast Code turned on. The linker optimization is standard.
Just out of curiosity, are you specifying /O2 or /O1 as well? (I hope you
have at least /Og in there besides /Ot, otherwise, you're not doing much,
really).
The shell is a shell - not much there. Typically things like this
(STARTFUNCTION and ENDFUNCTION are a simple set of try/catch handlers for
SEH)...

[Category("Settings"), Description("Add a page at a specified location -
return the page ID")]
int AddPage(int page)
{
STARTFUNCTION()
return mPDF->AddPage(page);
ENDFUNCTION()
}
OK, I can see that....
All suggestions gratefully received.

Does anyone know if MS use MC++ for any of the .NET Framework? Or do they
package all their x86 code in standard x86 DLLs and call it that way?
AFAIK, not much in the framework itself is written in MC++ (actually, the
only thing I can think of that *might* be is
System.EnterpriseServices.Thunk.dll)

Most of the rest of the framework libraries are straight C#.

I can't believe it was used for .NET 1.0 because the AppDomain bug meant
that anything written using it wouldn't work under ASP.NET. Has anything
changed with 1.1?


Some of the problems with that asp.net you mention have been corrected in
v1.1 (for assemblies compiled with VC++ 7.1)... see
http://support.microsoft.com/default...;en-us;Q309694

You should also be aware of
http://support.microsoft.com/default...b;en-us;814472

--
Tomas Restrepo
to****@mvps.org
Nov 16 '05 #9
I wouldn't disagree.

It would be nice if C# read .h files.

Jos
Nov 16 '05 #10
Hi Jos,
I'm assuming you mean /GL and not /Og here, right?
I don't really think it matters. I've tried a set of variations on a

theme.

HUmm... well it *should* matter. /GL (enable whole program optimizations) is
incompatible with /clr. /Og (enable global optimizations) is *not*. :-)
Ultimately these are the same optimizations I was using in a straight DLL
with C# front end and it ran 5x faster.


That does seem weird, and certainly shouldn't be that way unless something
weird is going on.... I'm out of ideas, though, without seeing the actual
code :(

--
Tomas Restrepo
to****@mvps.org

Nov 16 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mike Kamzyuk | last post by:
Hello all. Basically, I need to call a mixed-mode dll's function (which uses managed code) from a native or mixed-mode dll function (which does not use managed code). I'm wondering if this could...
0
by: Edward Diener | last post by:
I have some questions about the instructions for creating a mixed mode DLL in the MSDN topic "Converting Managed Extensions for C++ Projects from Pure Intermediate Language to Mixed Mode" in the...
9
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and...
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
8
by: Nadav | last post by:
Hi, I am writing a performence critical application, this require me to stick to unmanaged C++ as performance is much better using unmanaged C++ ( about 33% better ), Still, I am trying to avoid...
2
by: Doug Belkofer | last post by:
We have created a fairly complex mixed-mode DLL that we want to use from VB.NET. The mixed-mode DLL is written in C++, and does use the standard C runtime libraries. An unusual thing is happening...
8
by: Edward Diener | last post by:
By reuse, I mean a function in an assembly which is called in another assembly. By a mixed-mode function I mean a function whose signature has one or more CLR types and one or more non-CLR...
10
by: ajtaylor | last post by:
Hello, I have a load of native C++ code that I want to use in a CLR class library. My "logic" being that I create a C++/CLI managed class that acts as an interface to the unmanged code. I...
0
by: emu | last post by:
Hi All, I have an unmanaged C++ application that references a mixed mode image DLL (mixed managed and unmanaged). Under .NET 1.1 we could trust the dll (the mixed mode dll) by running the...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.