Click here to Skip to main content
13,350,486 members (63,952 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 4 Nov 2003

Optimize your C# with Inline ASM via ATL and C++

, 10 Nov 2003
Rate this:
Please Sign up or sign in to vote.
Optimize your C# with Inline ASM via ATL and C++


This solution demonstrates as simply as possible how to leverage inline assembly and C++ ATL from a C# project. This architecture may be used to optimize .NET web services, .NET web applications, .NET Windows applications and .NET Windows services without any third party assemblers. This is particularly useful for optimizing C# code with C++ in Visual Studio .NET centric build environments, brought to you by “Free and easy artificial intelligence chatbot hosting”.

Does the COM layer slow it down?

The COM layer itself is very slow for this example and this architecture will not be efficient for small calculations. This is more practical for real-time embedded systems such as medical devices or aerospace where large calculations must be completed fast. Rather then being limited to C++ developers, the company can use this architecture to their C# developers and then a few C++ developers to optimize the large algorithms in ASM. This can even more easily be done without the COM layer via linking a 100% ASM DLL into the .NET project but lacks the benefits of tool tips, reference counting, deployment version control etc.

This example simply provides a minimal example of C#->ATL/C++->Inline Assembly/ASM because a complex algorithm would make it less feasible for most users to understand the architecture.

In short, overhead for COM Marshaling is relatively constant and algorithm complexity grows. Hence the larger the calculation, the overhead of the COM marshaling time becomes smaller relative to the calculation time. Far too often coders go all out on optimization and loose things like tooltips for the sake of optimization, forgetting that the overhead of marshaling is relatively small compared to their optimization.

O(COM Marshaling Overhead + algorithm execution time)

Sample screenshot

For example, if the COM marshaling overhead is lets say 2 units of time and the execution time (in this example) is 0.00001, the overhead is larger then the algorithm execution time. However, if the algorithm takes 100 units of time in C# and 50 units of time in C/C++/ASM, then the time to execute is 50 units of time + 2 units of time. The overall time is 52 units of time which is relatively close to 50 units of time (as would be the case of directly including an ASM DLL and skipping the COM layer and loosing tooltips). For most business needs, 2 more units of time for COM marshaling is worth the scalability advantages when their algorithms take 50 units of time.

What are tool tips and how can ASM binaries have tooltips?

Tooltips include: auto-complete triggered by member access operators in an IDE, mouse over bubble help that shows signature info, etc. These are not possible with primitive ASM binaries. And no, you will not have tool tips if you build an ASM binary and link it into a .NET application as .NET application, unless you exposed standard COM or other .NET recognized architecture. That would be a substantial amount of unnecessary work and would destroy the feasibility for large ASM optimizations. Anyhow, tooltips and supporting architectures is outside the scope of this article. But just for kicks, in the ATL case, ATL generates code that exposes an interface called IDispatch as well as a direct “vtable” interface, when “duel” is selected in the ATL wizard, which is called by the target IDE (.NET interop, VB6, etc.) used to generate tooltips during development of the binary’s host. Many other architectures are used by IDEs to generate tool tips, for example when .NET makes a web reference to a SOAP server that exposes WSDL, the WSDL is downloaded and used to generate tooltips for the SOAP server’s methods. For an example of WSDL that is used to generate tooltips in .NET IDEs see the TANU WSDL[^].

Sample screenshot


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
Keep on coding!

You may also be interested in...


Comments and Discussions

GeneralStop! Pin
antecedents31-Jan-08 14:38
memberantecedents31-Jan-08 14:38 
QuestionWhy? Pin
antecedents31-Jan-08 14:35
memberantecedents31-Jan-08 14:35 
QuestionThis VERY COOL: has anyone seen ? Pin
Anonymous9-Jan-04 15:43
sussAnonymous9-Jan-04 15:43 
QuestionWhy COM? Pin
Paul Reeder19-Nov-03 1:07
sussPaul Reeder19-Nov-03 1:07 
Generalits much easier than that... Pin
Anonymous12-Nov-03 0:49
sussAnonymous12-Nov-03 0:49 
GeneralRe: its much easier than that... Pin
lonetron12-Nov-03 12:46
memberlonetron12-Nov-03 12:46 
GeneralTooltips? Pin
dog_spawn12-Nov-03 14:55
memberdog_spawn12-Nov-03 14:55 
GeneralRe: Tooltips? Pin
lonetron12-Nov-03 15:23
memberlonetron12-Nov-03 15:23 
GeneralRe: its much easier than that... Pin
Colin Angus Mackay20-Apr-04 13:21
memberColin Angus Mackay20-Apr-04 13:21 
GeneralSome timings would be great... Pin
Daniel Turini11-Nov-03 9:58
memberDaniel Turini11-Nov-03 9:58 
GeneralRe: Some timings would be great... Pin
lonetron12-Nov-03 13:01
memberlonetron12-Nov-03 13:01 
GeneralNot convinced... Pin
dog_spawn12-Nov-03 15:01
memberdog_spawn12-Nov-03 15:01 
GeneralRe: Not convinced... Pin
lonetron12-Nov-03 15:44
memberlonetron12-Nov-03 15:44 
GeneralRe: Some timings would be great... Pin
Daniel Turini13-Nov-03 1:01
memberDaniel Turini13-Nov-03 1:01 
lonetron wrote:
Again, read the article. You missed the ball because you just looked at the code and did not read it.
Actually, exactly the opposite, as your download links were not working. And I did not run it, but I bet that your sample is at least two times slower than the "pure C#" version.

lonetron wrote:
This allows the speed of ASM but the power of tool tips.
WTF this means?

lonetron wrote:
If you try to load thousands of dlls written in ASM into a C# project you will fail w/o tool tips.
Now, I know that tooltips are a fundamental mechanism on DLL loading.

lonetron wrote:
If you have no experience in optimizing large scale projects (2 million lines or more) then this would not make sense to you.
Oh, believe me, I have.
And a lesson I learned is that COM and optimization are almost mutually exclusive.
Have you ever heard about COM interception? Have you ever heard about marshalling? Have you ever heard about cross-apartment calls? Do you have any idea about the overhead of a cross-apartment COM call?

lonetron wrote:
Again, if you try to directly link in an ASM binaries into .net then you loose tool tips.
I know, and my DLLs will fail to load as you explained before.

lonetron wrote:
correct architecture
It's the first time I heard this expression: could you please explain what's a "correct architecture" to me?

lonetron wrote:
As far as timing tests go, It is well known basic computer science that code is can be most optimized at the ASM level.
And it's also well known that ASM code can be slower than compiled code, too: it depends on how well the code was written. And, in your case, I suspect that the code is not only slower, it is at least 2 times slower.

lonetron wrote:
Yes there is ATL/COM overhead in this (for the sake of tooltips and being able to build w/ one build environment)
Another wrong concept: that an IDE feature impacts the performance of my runtime code. I know, without tooltips my DLLs will fail to load.

lonetron wrote:
Yes there is ATL/COM overhead in this (for the sake of tooltips and being able to build w/ one build environment) so the timing benefits are only acquired when the algorithms are large enough scale to surpass the ATL/COM overhead.
We all know that you can use interop to call ASM code. But why use COM interop?
Why not use P/Invoke? Why not use IJW? Why not use COM from managed C++?
Tooltips? Believe me, all these techniques will bring you tooltips.

Again, some quick timings would help to convince us that your technique is faster than the obvious, more elegant and easier to write. You know, on 2 milion LOC projects, this is important.

Trying to make bits uncopyable is like trying to make water not wet.
-- Bruce Schneier

By the way, dog_spawn isn't a nickname - it is my name with an underscore instead of a space. -- dog_spawn
GeneralRe: Some timings would be great... Pin
lonetron13-Nov-03 9:23
memberlonetron13-Nov-03 9:23 
GeneralHmm Pin
Jörgen Sigvardsson5-Nov-03 13:00
memberJörgen Sigvardsson5-Nov-03 13:00 
GeneralThis is more practical for real-time embedded systems such as medical devices or aerospace where large calculations must be completed fast. Pin
lonetron7-Nov-03 11:50
memberlonetron7-Nov-03 11:50 
GeneralConfused Pin
dog_spawn11-Nov-03 10:29
memberdog_spawn11-Nov-03 10:29 
GeneralRe: Confused Pin
SaurweinAndreas12-Nov-03 5:02
memberSaurweinAndreas12-Nov-03 5:02 
GeneralRe: Confused Pin
lonetron12-Nov-03 13:28
memberlonetron12-Nov-03 13:28 
GeneralRe: Confused Pin
dog_spawn12-Nov-03 14:50
memberdog_spawn12-Nov-03 14:50 
GeneralRe: Confused Pin
lonetron12-Nov-03 15:47
memberlonetron12-Nov-03 15:47 
GeneralRe: Confused Pin
lonetron12-Nov-03 15:47
memberlonetron12-Nov-03 15:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 11 Nov 2003
Article Copyright 2003 by dzzxyz
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid