33 points · by jgalvez · 18y ago · 86 comments
I've written a C++ Apache module for a simple web app before. It's definitely harder than using a scripting framework though, not so much because of the language, but more that frameworks have functions that help you do just about anything (e.g. sanitize input, construct safe SQL). In C++ land you're more on your own since most people aren't crazy enough to build a web app in C++.
Also, if you go down this route and you're particular about C vs. C++, check out mod_cpp for building Apache modules: http://sourceforge.net/projects/modcpp/
I echo the advice that if you have something performance-sensitive, build it in C/C++, but proxy it through something built with a better-known framework. Thrift is great for this: http://incubator.apache.org/thrift/
I heard about Thrift when Protocol Buffers came along but didn't bother to look into it. Is it any better than PB? Does anyone have any experience on this to share?
Personally, I like Thrift better. More languages and greater community participation.
http://uk.techcrunch.com/2008/09/21/techcrunchtalk-video-sta...
Rather, they use C++ because the library that powers their IM client, libpurple, is written in C. Writing a C/C++ bridge to whatever (Java, Ruby, Python, PHP) isn't hard and there are lots of generic solutions out there already, e.g., Thrift.
It uses c++: http://www.okws.org/doku.php?id=okws:tutorial
Use KLone http://koanlogic.com/klone/ .
It lets you write web pages with embedded C/C++ code (php-style escaping).
It's VERY fast and provides all common functions/features you need to parse incoming HTTP variables, sessions, file uploads, etc.
If you need more features you can link it against any C/C++ library and call library functions from within web pages.
If you're doing something computationally intensive (like, a dating or quiz site, for example, with the impressive statistical analysis that OkCupid and HelloQuizzy deliver up very quickly), then it would pay to look at an approach that's working well right now.
Let me know what you end up doing.
Build on a mature best-of-breed product that provides a quality programming model and all required HTTP and logging services. Also easily take advantage of other apache mods such as memcached or wackamole.
Good luck.
Contact me if you are looking for someone to do that kind of work.
If I were starting a new web app from scratch today, I would code it in C using Thomas Boutelle's cgic library. Of course, it seems one rarely starts anything from scratch these days.
Please contact me at jonas@codeazur.com.br with IM info, I'd love to introduce you to some of the stuff we want to do (so maybe you can help).
That said, even as a hard-core C++ guy, I can't see why you'd really want to write a web application in C++. If you want C++-like syntax, you'd be better off with Java for web stuff.
And start saving money for memory and large amount of servers...
Chances are you may end up with a story like this: http://groups.google.com/group/comp.lang.python/browse_threa...
Albeit micro-benchmarks may indicate that Python is 100-200 times slower than C++, they may not be applicable to the real world. Python can be very efficient. And when combined with libraries like NumPy, beating it's performance with hand-crafted C++ is difficult.
Emperor has no clothes.
On the upside, it was 100 lines instead of several thousand.
On the downside, it was several hundred times slower.
I didn't bother trying to write serious simulation codes in Python after that.
My point is that some people assume that if they would just sit and write something in C++ without putting enough time and effort and skill in it, it would be somehow magically faster and better than high-level language, just because it's C++. They are mistaken.
Python standard library is extremely fast. For example 'list.sort()' in Pytohn could run 3 times faster than C++ 'sort(vector<string>)'.
Here are few examples when simple Python code runs faster than the same code in C++: http://groups.google.com/group/comp.lang.python/browse_threa...
Again comparing apples to oranges.
I can't wait for them to finally get the on the fly compiler working. Good times coming in the language competition.
There are many ways to look at complexity as an abstract concept. You could look at code length. Or you can count how many 'degrees of freedom' your code allows you to have, and be sure that you need to control each of those. C++ affords too much freedom for developer.
The trend meanwhile is towards reducing complexity at system/core level and focusing on application unique added value. Today, in large web applications, complex behavior is build using simple components. Those components are normally done in C, and then wrapped by some scripting language. C is not too flexible. The components are rock-solid as a result, they lack unexpected behaviour. Their code's 'degrees of freedom' are easier to control.
On top of that, there is trend towards frameworks in scripting languages which reduces 'degrees of freedom' at core/foundation level even further. Finally, there is this REST thing that wants to standardize API of a typical web app, offering us even larger blocks to code to work with. Again, all these trends have one objective - allowing to focus on unique application functionality, on interaction with other systems etc and hiding underlying complexity from the developer.
By using C++ you are likely to get complex behaviour in your code very early, at a very fundamental level. As you add functionality, the complexity/behaviour quirks will grow exponentially and you will fail.
I am convinced that Microsoft failure with Vista is mostly due to them using C++. They were simply defeated by mounting complexity, not by lack of engineering talent. Meanwhile, Unix success (Apple OS X included) is determined by relative 'dumbness' of underlying C language, Unix tradition of small single-purpose tools etc.
It is an event-driven, staged (threads communicating via message passing) application server, written in C++. It has stages for HTTP server and client, disk IO, gzip, logging (basically all CPU-intensive or IO tasks). Messages between stages are defined in IDL. You can send those over the network via Sun-RPC or JSON-RPC.
Not directly related to your request, but still handy: it also has a stage that wraps the Python VM, so you can write parts of your app in Python.
There are plenty of C/C++ HTTP libraries and frameworks out there. Just proxy your homebrew app server behind squid, lighttpd, apache, nginx, or $YOUR_FAVORITE_PROXY and you are set.
In fact, it makes it really easy to develop and test your app that way. There is no web server to configure and you can easily stick in printfs all you want. I use CherryPy (standalone HTTP framework for Python) that way exactly for these reasons. I'd do the same with C++.
The traditional HTTP side of it is trivial (just use nginx or something like that.) For template engine you have already Clearsilver and it's the fastest that I know of by an order of magnitude.
The key is solving how the application's developers could extend the web application. You can't expect them to code in C++ and compile every little change to their site. That's the beauty of CMF systems like Zope, you can put plain Python code in there with pages and components.
Doing a CMF in any lower level language and having to switch to a higher level language VM for every snippet would kill your original performance savings.
Find an element to fix that and you'll have a strong new contender in the CMF arena. Stuff like OpenMP and all that comes bundled in the new C++ libraries, transparently.
(BTW IMHO don't even think of starting without memorizing the Essential C++ books and all it's need-to-know patterns.)
1 - build your app as an extension of an existing http server. Start with lighttpd or nginx source and build out from there. nginx has a nice module structure. Perhaps your app can be just a new module. Both of these code bases are C and use event driven sockets. You might be able write your extensions in C++.
2 - Use a known solid http C++ library to roll your own server. Depending on the complexity of your app, you may end up spending a lot more energy going this route.
My relay server for http://shellshadow.com is C++. I had my C++ programmer look at the structure and methods used in one of the two http servers named above. We ended up not re-using their source (our relay isn't http protocol) but did get some useful insight for developing our server daemon.
You may want to investigate the following libraries:
fcgi++: Interface with the web server using fast cgi; keep the program resident in memory between requests.
botan: For generating cryptographic hashes.
cgicc: For handling input from the web browser.
xerces-c: For implementing a templating system and generating the output which gets sent to the web browser.
libpqxx: Interfacing with Postgres.
cppunit: Unit-testing critical or difficult-to-test bits.
It's not as hard as everyone likes to make it sound (assuming you are on the right side of the C++ learning curve). The real question is whether you want to spend your time writing a framework rather than actual applications.
Now, the concept is moot...a good OO web platform is so much more robust.
This seems like a good guide on how not to fuck things up with C++.
You can write your events in C++ or Delphi or both. (flame on)
If you want to see how much better an ORM in ruby/C can be, look at datamapper http://datamapper.org. This is mainly used with merb. The new datamapper uses data_objects which has been completely rewritten with C underpinnings to be much more efficient than previous datamapper. I haven't seen any recent benchmarks against ActiveRecord. But I do believe the datamapper team met their goals of being faster.
In fact we, the core DM contributors, have spent very little time optimizing the library. Up until now we've been mostly focused on creating a clean API, and keeping the internals simplified. I would expect the gap to widen much further once the API stabilizes, and the spec coverage gets nearer 100% (it was in the low 90's last I checked).
Ive written a lot of C++, it was 'the one true way'.. but then I did see alot of people using python, ruby.. I learned a bit of lisp, and kind of de-conditioned myself.
If your not gonna use Lisp/arc/scheme/python/ruby... then, actually, I would go with C and not C++. You are just above the metal and you see exactly whats going on, and you dont spend time making cleverly convoluted constructs of infinite generality...
In C, you just get shit done. With a bit of discipline you can get a lot of the benefits of OO and even some of the benefits of FP in your C code. Heresy, I know.
I do love C++ still, its nice to see good RNG and regex APIs going into the language.. but its a bit verbose on balance.
gord.