C# High performance programming

I have noticed a number of comparisons with C++ and C# for high performance systems , and the same mistakes are frequently made. I will go over a C# queue which is frequently used in high performance systems.

1. Any linked list system in C# will preform badly unless -

  • You manage your memory yourself via unsafe pointers OR
  • You recycle nodes ( which will still not offer great or C++ performance)

The reason for this should be obvious since any new to create a new record is FAR more expensive than enqueing and most locks.  Hence very high performance C# datastructures really need to be based on arrays or be unsafe .

2. Structs and Queues are very dangerous

structs can offer excellent performance especially when passed around via ref. however there are a number of issues..

You can pass in structs via ref and retrieve them via out. Note however if you use ,out a queue will reasign the space immediately and hence your data will get over written so this is prob a bad idea.

Also note when using ref with a struct it can have unintended consequences eg

AMethod( ref myStruct)

{

array[0] = mystruct;

}

is almost the same as the method without the ref , the ref will cause an additiona CIL loadobject and hence perform worse.

 

                              

Next Generation Operating System Why needed and Why now.

I am comming more and more to the conclusion that existing Operating System do not meet our needs nor can they be changed to handle these ( look at the cost for Windows 7 to improve parralelism cite) .  This is not just Windows but also Symbian , Linux , OSX , Chrome etc.  Im also saying UNIX is reaching its use by date.

Obviously the installed base is massive making change painfull but i believe it will come.  The Niche markets of Security , , CLoud computing and Mobile devices especially dont feel the impact of an existing installed base as much.

While there are many smaller benefits the key benefits are

1. Performance on modern hardware . This is not just execution performance  but also how applications are written for eg parallelism , while languages have made great strides in this area ( ie Hascal, F# and OCaml)  they get stuck at the iPC level on the Operating System .

2. Asyncronous nature of modern communication most OS IPC is inefficient ( to what it could be ) for Asyncronous  IPC. ( it requires extra threads , wastes a return etc).

3. Disk bound. All modern OS are heavily disk bound.  While traditionally this makes sense we are rapidly aproaching diskless computers with internet ( or file server)  storage . To some extent people have pushed for this but the OS nature has held as back.  This especially affects UNIX with its everything is a file concept and existing file based apps. Can anyone argue against remoter boot of Proms and USB sticks and network/internet  based access ?

Computers are regularly waiting on the DIsk , primarily for page swapping , yet page swapping is almost redundant today  , the function can better be done with application swapping. Pageing results in a significant Operating system overhead in terms of performance but worse in terms of complexity , bugs and battery life / power usage.

4. Reliability . Significant increases have been achived in message based micro kernels the next generation operating systems will do this without the performance cost.  Its worth noting that these message based Micro Kernel OS performance penalty has been getting worse due to the extra memory cost and memory speed has been growing slower than CPU. 

5. Last but not least is security.  There are 3 main issues

  • Operating systems need to accept the user will agree to things asked and need to limit the damage. This is possible with non intruesive methods as shown by capROS , Eros and Coyotos.   These operating systems follow the POLP/POLA and applications have very low ambient authority.  Hence any breach will have limited impact.
  • Existing operating system applications and services , can be made to crash and expose memory and data for futher exploitation , typesafe and memorysafe OS will massively reduce this most common form of attack.. The OS itself is only vulnerable through a specific bug and without things like buffer overuns these are much rarer.  
  • UnIdentified applications . Apple has made strides in this area with approved apps though more for comercial reasons ( and since the approval proses is not independent people are starting to disable it ). Certificate signed .NET assemblies work pretty well as a measure of trust.

6. Resource management .

             How often do computers crawl because a backup , Virus scan or index is running ? While these threads run at low priority  there is no or little management of resources and since everything runs of the disk (see 3) things are waiting for all the time despite the fact that what they are waiting for is small.

 

Most of these things require applications to be written differently but note this is also being required for specific reasons eg parralelism , Minimizing GC pauses , Cloud applications , Asyncronous applications , new languages Caml/Hescal/F# and last but not least writing applications for POLP/POLA ( Principal of Least priviledge/Authority) all require changes in the way we write application.  New generation operating systems should assist and encourage such programming.  

 In terms of why needed i think the recent Google attacks have shown that modern security is not effective enough.

 

Developer Book Syndrome

A colleague of mine once stated that creativity is rare in technical people I had my doubts at the time as I’m fairly creative and often build unorthodox solutions tailor made to the problem but the longer I’m in the industry the more I agree.

The biggest symptom of this is something I called book syndrome. This is where famous author xyz ( eg Lowly , Fowler etc etc ) writes a strategy in a book and people use it in completely in inappropriate circumstances. When challenged on the solution they use the authority of the author to emphasize they are doing the right thing. While this technique may provide some job protection it is questionable that the famous author intended to apply it where it was applied ( eg Juval Lowly SOA 1 service per class  ) and in many cases like Fowler there are many methods yet a single patterns is repeated ad nauseum without any concern for the special cases for each project..

And it is not just books, Universities have a lot to answer for drumming in OO yet for a lot of small to medium projects scripts and RAD provide better and more consistent results  (   OO /RAD is Software theory relevant) . The fact is the bedrock of nearly all programming theory is based on 90% of the cost of an application being maintenance while true in the 50-80s this is clearly not true in the huge amount of small applications and scripts that never enter software studies ; in fact most of these programs never change over the life time of the program ( and are completely rewritten in a new technology after 10-15 years) is completely at odds with most of software development theory. I’m not against OO but I am against using OO in many inappropriate situations ( eg blindly following book – book syndrome) – even in an OO house RAD development will be faster , cheaper and deliver more reliable project deliveries.

The whole concept of an OO development house , an SOA house or a RAD house seems very wrong . Each has their pros and cons for each project and any competent team should be familiar with all techniques and use them where appropriate.

Every project needs to be taken on its own merits and facts like these are FAR more important whether OO is better than RAD or SOA.
 - Amount of change /maintenance over the life of the project
 - Fixed price contract
 - Team skills (Architecting for the team)
  - Breaking down the work ( eg SOA service layer and RAD front ends)

SOA 1 class per service Juval Lowy framework

I posted this a year ago.... I'm really against this make each class a separate service concept. It reminds me of the Microkernel and especially workplace OS but worse. The idea of microkernels was each service ( like memory) was an easily managed entity with carefully defined inputs and outputs which could be consumed.) Workplace OS took this to limit and performance was so poor that no amount of optimization would help. So they had no choice but to scrap the project ( were talking $2B 10 years ago) the bad thing is once such a design is made and implemented just like workplace OS it is very difficult and expensive to fix. Classes by nature frequently communicate with other classes ,serializing everything makes this far to slow. Think about big orders with 10,000 line items each line item could be a separate service call to add up some total and what do you gain ? I consider service to service communication and some sort of nearness analysis a vital part of SOA and grouping related classes into services will help significantly by - Increasing performance - Allow for better maintenance and less change as related changes will deploy together . - Allow for better OO/maintenance and Agile techniques within each service. ( though less outside) - Allows better integration and use of different services such as Java , CRM/ERP services etc. - Encourages chunkier cross service calls and better attention paid to the service interface ( for easier use, compatibility/maintenance and performance) - Discourages the service as a DB store proc wrapper. - Avoids having to put performance hacks in the wrong place to try to avoid cross class calls. This does not mean services should be 20 classes 3 is a good line but 1 to 9 is ok in a number of cases depending on class relationships. Basically if 2 classes communicate all the time they should be in the same service. I can see in 5-15 years hosted environments/frameworks hosting classes which can be moved to machines or be in proc though its far too early for this and you NEED the ability for classes to communicate via normal inproc ( without serialization) . I not you mention this for the future but telling people to use each class as a service now without some more comments on the hazzards ( especially when they don't have a lot of experience in SOA) will lead to a lot of frustration for some .

Obsession with new toys

I will be returning to eventing/EDA  soon ...

There have been a number of posts lately on Windows Workflows 4s break with the past citing continual change and lack of support being a big problem.

Id like to make a few comments on this. The number one productivity killer in .NET today is undoubtedly new technologies and learning how to implement them well.  While these are without doubt superior to past products eg Remoting /WCF , WIndows Forms / WPF , DataSets/LINQ for Sql people seem to be making the move because the feel its new and hence will be supported longer.

This is completely wrong  thinking.   There are still  solutions today where remoting and Datasets are the most appropriate technical choice and likely the best choice in terms of skill levels.  To this day Microsoft still provides the occasional patches to the system. It is often the case with many systems that exist and are maintained many years after the developers even stop support .. Open Source code for example comes with no guaranteed support at all and many projects die yet still provide useful tools and forums for many years . In comparison Microsoft products have legions of people who can assist and forums are full of work arounds it is far cheaper to apply these to an existing app then rewrte it to take advantage of  some new code Microsoft has written. eg if you want ( Not  my choice) a distributed object system with tightly bound types Remoting and writing your own security is still a better choice than Windows Communication Foundation.

Often in these cases the main driver is political and a hard excuse to change something, people need to be less afraid of using older code /libraries its very common in the non Microsoft world and has trivial implications for software development. Obviously non supported hardware  and shrink wrapped software is a different issue.

C# and F# are complementary

 I just had a lot of repetative web work to do and im starting to look at F# , it looks like F# fits really well with C# replacing the batch files / scripts you use and great for proto typing.

Things where C# are better
Lib /UI Development
Writing large assemblies
When you need low level control or unsafe.
When you use TDD

When F# is better.
When you need quick and dirty codes ( utilities)
Screen / Web scraping
AI scripts
Mathematics / financial scripts
Workflow scripts
Frequently changing code.
Project control.

How often with C# did you have to do something really simple like put in a bunch of reg setting  (from a reg file) , control nmake/make  for daily builds , prototype code to get quick data to test something etc. F#   seems to do all these things better.  TDD  test scripts should work very well with F# as well.

C# for low latency applications

There is no argument modern languages  such as Java and C# allow quicker , safer and easier to maintain applications yest there is still a very large amount of C/C++ applications being written.

A lot of use of C++ is simply for 3 reasons
  - A view of better performance
  -  Maintaining an existing application  
  - GC pauses
  - Environment requires it.

Lets look at these issues


In practice with realistic budgets a Java/ C# applications will tend to have better performance due to better and easier to use collections and optomizations.  The main performance difference relates to GC costs though the new Concurrent GC should remove most of these for multi core. The idea its not native is a fallacy cince you can use the Mono AOT compiler to generate x86 compiled code.
 

GC Pauses are quite valid and is a major obstacle for low latency applications.  There are a number of things you can do
- There is nothing stopping you using C# and managing your own memory for key performance critical areas. While unsafe is discouraged  C# with some unsafe code is better than C++.
- The  new Concurrent GC should reduces pauses by 80%
- Use load balancing SOA/EDA services , this solves  the issue for most cases such as Market data . Its worth noting there is a new event which can take the service out of a cluster while it does a collection.

Also Maintaining an existing application ( or staff skill set) is quite valid  though the point comes when you are maintaing a Large C#/Java code base and have a few C++ bit left.

Lastly some environments do require it such as Windows device drivers , though i do question why more embedded systems dont use managed languages like Tiny OS ( which uses Java) . After all even operating systems are being built in C# ( Singularity , Midori , Cosmos , MOSA)

WS-Eventing

WOW  , I got a very polite response from an issue I reported ( see earlier article in Blog) with WS-Eventing. I wish work related bugs reports were handled so nicely.

I do disagree however it would have been nice to at least add this as Optional to the standard , though hope is not lost for a standard fast WS-Eventing as they may introduce it for WS-Topics. All it needs is a comment and then vendors will implement it as at the moment they do their own thing.

Here is the transcript.


Ben,

 

At our F2F meeting this week we had a chance to discuss this issue. Basically there are several ways to simplify subscriptions, which can remove some extra processing such as XPath matching. One I know well is WS-Topics and Gilbert Pilz proposed a simpler approach in Comment #2 against the Issue: http://www.w3.org/Bugs/Public/show_bug.cgi?id=6917

 

In the end the WG decided to close this issue with no action, meaning we will not specify a specific simplified format for subscriptions. However, there are places in the subscription where WS-Topics could be used and some implementations might support them easily. Likewise other strategies, such as Gil's approach would also work. Therefore we will put discussion and advice into out primer to help people exploit this optimization strategy.

 

Lastly, we did look at how WS-Topics might be normatively referenced in WS-Eventing, but the WS-Topics spec was written as if it was meant to be used only with WS-Notification. Therefore, we felt that normative references to WS-Topics would cause more confusion than necessary. However, as one of the WS-Topics WG members, I believe WS-Topics will easily compose with WS-Eventing.

 

Thanks you for you contribution and let me or the other members of the WG know if there is anything else we can do for you.

 

 

This is probably too late but i posted this on my blog
(http://www.shanghai-software.com/blog/archive/2009/02/15/ws-eventing-flaw.a
spx) and someone said i should sent it to this list.
 
 
 
 
 
 
 
Just doing some work on eventing and having a look at a number of
implementations there is a pretty annoying feature / flaw .
 
By default the only filter supported is Xpath and the specification
specifically states that
 
This specification does not constrain notifications because any message MAY
be a notification. from http://www.w3.org/Submission/WS-Eventing/.
<javascript:void(0);/*1234671797014*/> 
 
 
I have no issues for this - sometimes you want any message , however this is
a very expensive way to do this especially when you have a large amount of
events with different topics all coming through a single  notification
service ( load balanced) .
 
 
All the implementations i have seen ( including the sample in the
specification ) introduce the concept of a topic in the header to route the
data. 
 
 
eg
 
 
Sample in spec uses a header
 
 
    <ow:EventTopics>weather.report weather.storms</ow:EventTopics>
 
 
and a custom filter
 
 
 <wse:Filter xmlns:ow="http://www.example.org/oceanwatch"
 
 
(43)           Dialect="http://www.example.org/topicFilter" >
 
 
(44)         weather.storms
 
 
(45)       </wse:Filter>
 
 
 
 
Look  at some implementations
 
 
MSE ( Managed Service Engine from Microsoft)  ,Uses a custom filter
http://services.microsoft.com/2006-07/ServicePlatform/MSE6/Eventing/EventFil
ter/    which includes topics in the body of their custom event message.
 
 
 
 
Roman kiss implementation includes a string topic in his subscribe message
and has one topic per notification end point.
 
The end result is everyone will build their own system and filters.
 
 
Basically all the authors recognize that  the default spec will have
performance issues and can be easily optimized ; 
 
 
 
 
 
So why didn't the spec include an optional header on notifications and a
simple canonical topic/subtopic  filter ? This way the majority of eventing
systems can talk to each other without resorting to expensive XPath queries
.. Take the weather example would you like to handle weather messages from a
million subscribers using Xpath?  With such a filter you can quickly route
via the topic in the header to different eventing services ( or servers)
and then handle an xpath body query..
 
 
 
 
 
 
 
 
Regards,
 
 
 
 
 
Ben .

The End of Linux ...and Windows

New generation of OS coming.

Operating Systems are about to enter their 4th generation with a revolutionary change.  These being
  • Basic
  • TimeSharing
  • Hardware memory protection ( most were UNIX/MULTICS derived designs)
  • Managed OS. ( or OOOS)

While Singularity/Midori is a Manged OS it is still a traditional style OS. Not all 4th gen OS will be so traditional ,  things a operating system does like memory segmentation are not even needed.

A better name for these OS may be an Object Oriented Operating System or even Object Oriented Framework.  Tradditional things like IPC (Asynch = events) and security can be done with language features (Interfaces ,Private members etc) especially using Capability OS techniques  with the reference being a capability. In effect  these OS are  just huge OO applications with user apps being part of it. 

The only thing regarding scheduling that may be required is preemption especially on single threaded CPU's.  Yet this can still be done entirely in the OO framework .

Why do this ?
            Better performance than existing OS due to no IPC/memory overheads.
            More reliable  , the OS can be easily tested and even proved.
            More secure. ( really more secure ,not like OSX/Linux  due to fewer attacks)

All these things are especially needed in small internet linked devices ( including phones , Netbooks etc) , notice the latest iphone patch ? It will not be long before you will NEED a virus scanner and firewall on your phone as they will be attacked  like windows.

I believe this is totally a revolutionary step as big as protected memory systems ( which are not needed now) were in building a stable OS remember early versions of Windows compared to NT /2000/XP .

However research is completely lacking . Consider the nameless OS im  building with the Cosmos managed OS toolkit.

  • Managed OS - very little research
  • Single Address space - no modern research eg Multi core .
  • Asych IPC  - little modern research 
  • Capability Managed OS - no one has done it . 
  • OO security for Capabilities - no one has done it.
  • Moving hardware access into user process and limit with capabilities - Very few people have done it most modern designs have user mode drivers but the hardware access is the HAL. 
  • No Kernel calls - Minix is the only other OS I know of where IPC is used for Kernel calls .
  • No kernel - Even  "Nano" kernels have primatives and hardware in the Kernel .

How does this effect Linux  ? I dont believe the open source community can move significant resources fast enough for a new completely incompatible Linux ( including all C and C++ user applications) and it needs a very dedicated individual to complete a kernel .  Compare Linux ( mainly Linus initial effort ) to Gnu Herd ( an open source effort) .  Note there are small open source managed OS efforts (Cosmos and MOSA) .

Why can Microsoft do it ?  Because they have been working on this since 2002-2003 and have released a "Research Kernel" in 2005. They can pressure driver manufacturers and most .NET and Java applications can be fully supported and MS will move key apps like Office , IIS , Sql server  to the new platform.  

There is a real danger to open source where MS will sell a Midori OS to corporates like they did with NT and leave windows as a cheap alternative if Midori sells well.  While this may not maximize short term revenue it will  hold Microsofts dominant position and reinforce there sales for other applications eg Word for Windows 2 ( when windows was released)  ,Office 2000 and SQL server for NT /win 2000.

Anyway Midori if the designers get it right will make its appearance in the next Major Windows Mobile version ,  it will be a great  area as a lot of backward compatibility will be provided via Compact Framework.

More information on Midori

Very curious on Midori and where it is going ..

So based on the following information i will entertain some speculation.

The following is a subset of people who are working on Midori ( see http://www.nabble.com/Midori-in-The-Register-td18739750.html)
Ravi Pandya,
Dean Tribble,
Pavel Curtis,
Eric Northup,
Chris Brumme

Jonathon Shapiro

Recent papers

Ravi Pandya  (Architect Technical Strategy)  ( has some comments related to small hand held devices/ netbooks and Security / identity
                  Abstracting Operating environment from Operating System

Dean Tribble ( PrincipalArchitect  work in security) Recent patents
                    Concurrent exception handling
                     Abstracting Operating environment from Operating System
                      Hierarchical reservation resource scheduling infrastructure
                      Co-ordinating Chores in muli processing

Pavel Curtis
(Architect) Lots of work on social networking.

Eric Northup ( SOftware ENgineer) , worked on Coyotos (Security, Capability secure Windowing and kernel proof)

Chris Brumme (Architect/ Destinguished engineer) .  Major player in the .NET CLR v interesting blog

I’m now on an incubation team, exploring evolution and revolution in operating systems.  This is a fascinating area that includes devices, concurrency, scheduling, security, distribution, application model, programming model and even some aspects of user interaction (where I am totally out of my depth).  And, as you might expect with my background, our effort also includes managed programming.

This is 2006 and is when the Incubation project (MIdori started) not Singularity ( which had major releases in 2006 but is a research project) . My guess is Midori is about 3-4  years under development ( eg started when the singularity  papers were published)  .

J Shapiro has a paper on automated device driver testing . Probably a senior architect position major player with Eros and Coyotos capability OS.


Make your own conclusion but at least 5 senior architects are on the team . Im really curious if they are considering capabilities my guess is they are at least going to try /investigate it since they are hiring people from the Coyotos project. Also from Chris 's comment it is likely to be a revolutionary change ( which could very well be a pure managed capability system) .