Click here to Skip to main content
15,879,535 members
Articles / Artificial Intelligence
Article

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

Rate me:
Please Sign up or sign in to vote.
3.23/5 (41 votes)
10 Nov 20033 min read 112.3K   1.1K   33   23
Optimize your C# with Inline ASM via ATL and C++

Introduction

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

License

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


Written By
Web Developer
United States United States
Keep on coding!

Comments and Discussions

 
GeneralStop! Pin
antecedents31-Jan-08 13:38
antecedents31-Jan-08 13:38 
QuestionWhy? Pin
antecedents31-Jan-08 13:35
antecedents31-Jan-08 13:35 
QuestionThis VERY COOL: has anyone seen www.chatbotsearch.com ? Pin
Anonymous9-Jan-04 14:43
Anonymous9-Jan-04 14:43 
QuestionWhy COM? Pin
Paul Reeder19-Nov-03 0:07
Paul Reeder19-Nov-03 0:07 
Generalits much easier than that... Pin
Anonymous11-Nov-03 23:49
Anonymous11-Nov-03 23:49 
GeneralRe: its much easier than that... Pin
dzzxyz12-Nov-03 11:46
dzzxyz12-Nov-03 11:46 
GeneralTooltips? Pin
dog_spawn12-Nov-03 13:55
dog_spawn12-Nov-03 13:55 
GeneralRe: Tooltips? Pin
dzzxyz12-Nov-03 14:23
dzzxyz12-Nov-03 14:23 
GeneralRe: its much easier than that... Pin
Colin Angus Mackay20-Apr-04 12:21
Colin Angus Mackay20-Apr-04 12:21 
GeneralSome timings would be great... Pin
Daniel Turini11-Nov-03 8:58
Daniel Turini11-Nov-03 8:58 
GeneralRe: Some timings would be great... Pin
dzzxyz12-Nov-03 12:01
dzzxyz12-Nov-03 12:01 
GeneralNot convinced... Pin
dog_spawn12-Nov-03 14:01
dog_spawn12-Nov-03 14:01 
GeneralRe: Not convinced... Pin
dzzxyz12-Nov-03 14:44
dzzxyz12-Nov-03 14:44 
GeneralRe: Some timings would be great... Pin
Daniel Turini13-Nov-03 0:01
Daniel Turini13-Nov-03 0:01 
GeneralRe: Some timings would be great... Pin
dzzxyz13-Nov-03 8:23
dzzxyz13-Nov-03 8:23 
GeneralHmm Pin
Jörgen Sigvardsson5-Nov-03 12:00
Jörgen Sigvardsson5-Nov-03 12:00 
GeneralThis is more practical for real-time embedded systems such as medical devices or aerospace where large calculations must be completed fast. Pin
dzzxyz7-Nov-03 10:50
dzzxyz7-Nov-03 10:50 
GeneralConfused Pin
dog_spawn11-Nov-03 9:29
dog_spawn11-Nov-03 9:29 
GeneralRe: Confused Pin
Andreas Saurwein12-Nov-03 4:02
Andreas Saurwein12-Nov-03 4:02 
GeneralRe: Confused Pin
dzzxyz12-Nov-03 12:28
dzzxyz12-Nov-03 12:28 
GeneralRe: Confused Pin
dog_spawn12-Nov-03 13:50
dog_spawn12-Nov-03 13:50 
lonetron wrote:
the aritcle states that it is a simple case for the sake of demonstration but as this is only usefull in large architectures

Yes, the article states that, but it does not prevent it from being confusing. Without a more compelling arguement backed up with a good example people like me just think to ourselves "I am going to do it the way MSDN and the c# books say". In this case that means either use COM or PInvoke (as posted above).

Consider this questions: what is the point of this article? You have think of sufficient justification before you even start writing IMO.
GeneralRe: Confused Pin
dzzxyz12-Nov-03 14:47
dzzxyz12-Nov-03 14:47 
GeneralRe: Confused Pin
dzzxyz12-Nov-03 14:47
dzzxyz12-Nov-03 14: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.