C++: A language for next generation web apps
On Monday, I was pleased to be an uninvited speaker at Waterloo Devhouse, hosted in Postrank's magnificent office. After making some surreptitious alterations to their agile development wall, I gave a tongue-in-cheek talk on how C++ can fit in to a web application.
There were other cool presentations there too. Check it oot! Er, out!
During this presentation, I hope to convince you that the C++ programming language is ideal for developing your next web application.
You might be asking, Steve, Why C++? Why would I subject myself to this horrible language where you have to manage your own memory?
One reason is efficiency. Not only is C++ inherently fast. It also forces you to think differently. It discourages the use of overly complicated data structures that are built in to other languages. In C++, nesting more than one or two structures results in something that is simply too awkward to use. Instead, the you are forced to seriously consider using the simplest possible representation.
In addition, just about any library that you'd want to use has already been written, and has a free implementation available on the internet. Json, and CGI decoders are freely available. Also, as often overlooked, you have to handle UTF8 to Wide-character conversion, but this is easily achieved in a 10 line function.
The only thing you do have to be careful with is SQL database access. MySql is GPL'd, so you can't even link to its client library in a closed source app. SQLite seems to be free, except if you do business in Germany it is $1000 because they have a different definition of public domain.
Here is the first strategy that you might use to incorporate C++ into your web app. In this diagram, there are two things between the browser and your application -- the web server, and the php script. It is not as efficient as it could be, and there are also security implications with calling command line programs from php.
In this model, you write your C++ program as a CGI script directly, using one of the freely available query string parsing libraries. It is efficient and clean. When your browser requests information, the webserver starts your program, which spits out the response in json format. This response is then relayed back to the browser.
I use the above strategy for RhymeBrain. It includes advanced statistical algorithms that let it sound out any word that you put in. For example, you can enter the word "postrank" and see that it rhymes with such gems as "blank, prank, drank, tanked, and stank".
Because it's written in C++, I can run it on my super-powerful datacenter. It sits atop my sock drawer at RhymeBrain headquarters.
This powerful 1GHz beast can load the entire 2.6 million word database and fire back the response in about 50 milliseconds. It does this from a cold start, for each request.
But there is a third strategy: If you write your own webserver, you can cut out the middleman and serve the request directly. Your javascript code makes a request, and all your web server has to do is call a function to send back the results.
Writing a webserver isn't that hard. Here is the complete implementation of the Hibachi web server. It supports virtual hosts, and perl and php scripting, among other things. It was written by former Waterloo-ite Anthony Howe, and won the 2004 International Obfuscated C Coding Competition.
Inspired by Hibachi, I wrote my own webserver and built WebSequenceDiagrams (which runs in a real data center..). Doing it this way reveals a new business model. It is possible to package up the entire web application into a single installer that runs on Windows and Linux. Since it's all integrated, there is no need for customers to fuss around with Apache or the numerous other moving parts that could break if I shipped separate components. (You can run it in your organization for as little as $99).
The only disadvantage is that the final executable is really small -- around 700K. It may be a little smaller than some customers are expecting. During the presentation, it was suggested that I ship it as an appliance, in a huge box, to compensate.
Conclusion
I hope that I've convinced you of the benefits of C++ in your next web application:
www.digital-guide.org
hwww.digital-guide.org
Google uses C++, Facebook compiles PHP to C++ and everything you use on internet these days is made on C++, I.e. Your web server, browser, operating system, and the shitty interpreters for your stupid languages like java, Python and PHP.
Talking about performance, I will cleanup my ass with my computer sciences certificate if you find anything faster than C/C++ do anything.
This article is well done and well spirited, and just shows that it is a very good alternative to the shit most of people are brain washed.
<br>
I created my own webserver which is written and compiled in c++ and the pages are also written and compiled(dynamically) in c++. The coding is done always inside scriptlet <%%> and when code is changed it compiles dynamically in the server side when you access the page through the browser.<br>
<br>
Here you can check a website that is purely written in c++ www(.)invadev(.)com
- distributed systems
- simplicity
- performance
- ...
I've actually been developing a C++ web application framework over the last few years, and was recently asked by a friend to make an example video to summarise the "View" layer (it's an Model-View-Controller framework).
Essentially, the "View" layer is a template engine - much like Google ctemplate, which if you didn't know, is how Google's search results pages are rendered. Only the "View" layer in my framework is more expressive, much like a stripped back version of PHP. Seeing as both PHP and Java were developed in C/C++, such an abstraction of dynamic rendering should not seem novel. This allows you to change the HTML (or JSON, XML, etc) output without recompiling, which allows programmers to program and designers to design. (By the way, I believe PHP started out as a simple template engine, then features were added - functions, classes, namespaces, closures, lambdas, etc. In other words the language has evolved from a simple means of dynamic formatting to a fully fledged programming language, and, I believe is less suited to complex web applications when compared to C# or Java - or C++).
I agree with those who see the merits of C++ for web development, with the sheer power and expressiveness of the language requiring more skilled developers. This is a good thing, as it will help "raise the programmng bar". I see C++ coming back in a big way in the coming years.
Check out my video youtu.be/B7Uxbz_Ywa4
For those who still lack a clue, you have every right to write every shortcut you might need in any language you like.
Lastly, C is still far better than Cpp today and has the major benefit of not supporting OOP, so why not ditch the ++ altogether ? Are there libs or tools that you require that don't exist in C ???
For those who still lack a clue, you have every right to write every shortcut you might need in any language you like.
Lastly, C is still far better than Cpp today and has the major benefit of not supporting OOP, so why not ditch the ++ altogether ? Are there libs or tools that you require that don't exist in C ???
With 25 years experience of programming and you've never heard of portability with C# using Mono?
C++ is truly the portable language that can do everything. It's fast, flexible and frankly can be faster to develop in. There is this sort of myth that PHP is better for scripting. After 6 months on a PHP project, one really wishes it was C++ instead, as the complexity grows.
C# simply requires too much overhead and is restricted to Windows.
Java is a good choice, but the problem that Java solves is no longer relevent and fails with versioning and coherence.
The problem is HTML and Javascript. These are the awful things about the web. Two horrible technologies, which need to go. It all becomes spaghetti very quickly.
XAML is the Ideal UI, but unfortunately limited to .NET.
It's just not an ideal world, but if I had my way, the entire internet would run off of XAML and C++. However, that doesn't even exist at all, but should. I think if people wanted a functional language, they could do something like Google's Go.
www.binarytiers.org
It seems to be much more mature than a cppCMS or a cppSERV.
G-WAN (100 KiB), is a web server which offers C, C++ and Objective-C/C++ *scripts*... and which is much faster than Nginx and all others.
Even though this is tongue-in-cheek there is a lot of truth to be had when using C++ and a web framework like Wt.
Some people people think I'm crazy doing stuff for my website as cgi in c - but its so much faster! ... seeing pages of events listings (search results) come up in the browser in less time than it would normally take to load an interpreter (for any of the common web scripting languages) is addictive!
... and that is on a shared host as cgi using apache
Doing it in a language like c or c++ where you can compile a very tiny binary looks to me like a good way to go
- the less you need to load from the hard drive when someone hits a page the better!
portable? .. probably not (will at the very least need gcc and some system libraries) .. but its FAST!
Ok to be fair this is compared to scripts in an ordinary (shared) hosting situation using php, perl, etc not some high-end (as in too expensive to afford) dedicated box where interpreters/scripts/bytecode/data are cached in memory.
But honestly, I would question the ability to think critically of anyone who says their server should be written in C++ in this day and age.
First of all you would have to prove that your server is going to be CPU bound. Honestly, I have not seen too many things CPU bound in the last 5 years with the fast multicore CPUs always starved for data (and that includes some really large scale servers serving thousands of users). They are not even database server CPU bound, but are invariably I/O bound. Even if you have RAID of SSDs and gobs of RAM, your CPU running a typical server is still going to wait most of the time for data to come to it, and the user experience is going to be most impacted by network latency.
Second, you would have to demonstrate that you can do better in C++ (reasonable justification).
Even your example (pushing 1 GB of data) is not CPU bound, and if you implemented your server in C++ or Python or Ruby, or Java or Node.js you'd find similar times to move that data.
So, the question really becomes what is faster to develop in, maintain and add features to? Large C++ code base or a language that is designed specifically for the domain your problem lies in, in which the solution could be implemented with 10 to 100 times less code than in C++.
In this day and age, I would look into node.js or even python for writing specialty, high performance servers. node.js has proven itself and not too long ago set a record in requests served per/second.
www.webtoolkit.eu/wt
It's all about the people who will try to develop a toolkit (like Wt), the companies which will support it and the consumers that will find it easy to use.
Everything that is done in ASP.NET, or PHP, or JSP, can be done in C++, that is not an issue. What is the price to do it today without the standard C++ web toolkit, that is another story.
So, basically, C++ will last and will be used for a long time, for sure. There is always going to be a dilemma whether to learn it or to substitute it with some other language which offers some more friendly programming API.
But, Wt is freely available... And the new ones are coming for sure. :)
- Speed of development / expertise needed. While crack C++ guru will certainly be able to do it, most real-world programmers simply do not fall into that category - and never will.
- Division of work. If you hard-code your HTML in C++, then only a programmer can customize how resulting Web pages look, resulting in poor visual design of your Web site and/or excessive time consumed even for minor changes. If you dynamically replace parts of text templates to produce final HTML, you are simply reinventing what is already in "real" Web servers - why not spend that time on visual design of your site instead?
- State/session management. If you are using CGI model, you need to rebuild your state (data structures, database connections etc...) whenever an HTTP request comes (and new CGI executable is started). In NSAPI/ISAPI model (or whatever it's called in these modern times) your DLL sits in Web server's address space so you can cache your state, but than you have to be absolute guru to avoid resource problems - not just memory/resource leaks, but memory fragmentation as well.
So building Web sites in C++ is a little like building your own database engine - it can be done (even _should_ be done in some special cases), but most projects will be better served by prepackaged technology - be it RDBMS or Web server stack.
GPL only forces you to release the source code if you *distribute* the program.
With a server-side app, you don't ;)
Sorry I forgot to say "LONG LIVE C++", and "THERE IS ONLY ONE C++" as in "There is only one Ronaldo".
lolol
man you made me laugh like never "The only disadvantage is that the final executable is really small".
I always had this problem, seriously! A friendly low-ranked client once told me : "what! you mean to tell me that we paid $60,000 dollars for for three cheap floppies!", are you out of your mind! give me five CDs and i dont care if you fill them with high resolutin pictures".
lolololololol.
Thanks for the superb article. mrfarhat@hotmail.com
www.squidoo.com/sqlitetutorial
www.squidoo.com/sqliteprogramming
www.squidoo.com/sqlitedatabaseprogramming
I also have videos on YouTube (just search for "sqlite tutorial" and look for my videos with the black command line screen.
He is selling it.
love the customer expectation part
i had written c++ web app long back
1. It great grandparent was a dos program written in C++.
2. It uses a lot of secret special sauce that my company wouldn't want to sell as a php script.
3. It is blazingly fast.
4. The same base code runs on pocket computers PCs and webservers, because if (carefully) written without using templates, C++ is portable!.
It depends on what you want. If you want to go fast to market you might want to go with Rails or similar and replace it with faster implementations on a need basis...
Anyway, there are no "right recipes", you always trade things off.
This approach allows you to deploy your applications on simpler hardware (cheaper to replace as well as buy in the first place) with savings in almost every area and less downtime. Whats not to like?
It's nothing to do with the language, it's because Apple only allow Objective-C on the iPhone, because they are closed a source corporate machine - there are plenty of Java apps running on other mobile devices.
Your focusing on a slight performance gain (probably lost in browser to server latency anyhow) rather than productivity.
Lets take this blog for example, convert that into your next gen web app in C++, lets set a time limit say 24 hours. I don't think your get very far (compared to someone using a JIT'ed language) or you could prove me wrong of course :-)
C++ application binaries are not portable, but source code is, if you are not stupid enough to use a lot of platform specific functions without trying to abstract them.
Those who ignore the past are condemned to repeat it, BADLY.
Java gives you stuff to have a lazy stroll through a project but bloats things up. There's not much fun doing it.
Simple efficient code is the best solution in most of the cases. Machines love language that they can understand and that is machine code. Each level up from that adds more nonsense to what it is being said.
Java has its uses too.
The main bottlenecks were transfering the data from the logging applications' own memory into MySQL (since MySQL wasn't fast enough to handle 1GB/sec), and the TCP/IP connection between servers (I think we had a 1GBit/connection).
But don't get me wrong, Java is great for some things. For one without Java, the semiconductor industry would lose billions.
Java manages your memory by using more memory. Memory management is overrated. Most Java programs which do very little take up 100's of MB's, and leak, and when the garbage collector does work, it works hard (causes slow down).
In Java you may have a bug and the app still runs/leaks memory, in C if you have a bug it'll crash. I'd rather crash my car, and then take extra care on the roads, than have cancer and not no about it, until it's too late.
Java programs are slower too, which means you need more powerful CPU's. Java encourages a lazy way of coding and architecture, which will bite you in the a** in the long run.
C/++ code is portable ever heard of GCC, works on windows, linux, and just about everything? Tell me when someone makes a Java app for the iphone.
No offense to the wave of java kids out there. But there is a bigger difference between programmers who have an Assembly/C/C++ background and those that don't. Than there is between those who have a Java background and those who don't. Assembly/C/C++ foster a better, more critical way of thinking, than languages like Java, which teach you to hunt for the nearest library. "Oh my boss told me to make a First Person Shooter 3D game, where is the makeFPSGame(TYPE_3D); function and where is the Game API"
It could be easly done if the C++ was compiled to IL first.
Have a look at my jsc compiler at jsc.sf.net :) It will translate IL to javascript and more!
I do like the idea of a multitier application written as one application.
>have to handle UTF8 to Unicode conversion
Maybe anyone over 30 knows the difference between encoding and character set?
why not just extend with c/c++ servers like nginx/lighttpd?