103

It seems like most of common web browsers (Firefox, Chrome, Safari) are developed using C++. Whys is that so?

tshepang
  • 305
  • 2
  • 18
Nipuna
  • 1,306
  • 2
  • 13
  • 17

14 Answers14

170

Another way to ask the question is what kind of support does a browser need? The short list is:

  • Support for parsing (needed to make sense of [X]HTML, CSS, and [ECMA/Java]Script)
  • Tree walking/interpreting features (part of parsing and building UI)
  • Support for accelerated graphics
  • Fast networking
  • For the more advanced browsers: control over processes and isolating memory between pages
  • Must work on all supported platforms

Most languages have some sort of parsing support. You have parser generators for C, C++, C#, Java, etc. However, C and C++ have quite a few years head start on the rest of the alternatives so the algorithms and implementations are more mature. Accessing accelerated graphics in Java is a no go, unless you have some native extensions to make it work. WPF on C# provides access to accelerated graphics, but it is too new to have a serious browser built with the technology.

Networking is actually the least of the reasons to choose C++ over Java or C#. The reason is that communication is many times slower than the rest of the processing that goes on to display the page. The raw speed of the wire is the limiting factor. Both Java and C# have non-blocking IO support, as does C++. So there really is no clear winner in this area.

Why not Java? Have you ever tried to build a UI with Java? It feels cumbersome and slow compared to anything else out there, because it is. No accelerated graphics is also a big negative here. Java's sandboxing is really good, and can help improve the security of a browser if it is used correctly, but it is a pain to configure and make work. Not to mention the graphics format support lags behind most modern browsers.

Why not C#? If your only target is Windows, C# might actually make a good representation. The problem comes when you want to support anything else. Mono hasn't caught up enough to be considered cross platform enough for this task--particularly with accelerated graphics support and WPF. Who knows how long that will take to change.

Why not C? There's a C compiler for just about every platform out there (including embedded devices). However, there's a lot that C does not do for you that you will have to be extra vigilant about. You have access to all the lowest levels of the APIs, but the majority of C developers don't do GUIs. Even the C GUI libraries are written in an object oriented manner. As soon as you start talking UI, an object oriented language starts making better sense.

Why not Objective C? If your only target is Apple, it makes a lot of sense. However, most developers don't know Objective-C, and the only reason to learn it is to work on NeXT or Apple boxes. Sure you can use any C library with Objective-C, and there are compilers for many platforms, but finding people to work on it will be a touch more difficult. Who knows? Maybe Apple can turn this perceived deficiency around.

Why C++? There's a C++ compiler for just about every platform out there. Almost every GUI library has a C++ interface, sometimes it's better and sometimes it's just different. For example, Microsoft's ATL is a lot better than win32 C function calls or even the MFC library. There's C++ wrappers for GTK on Unix, and I'd be surprised if someone didn't have a C++ wrapper around Apple's Objective-C GUI library. Process management is easier within C++ than Java or C# (those details are abstracted away for you). It's perceived speed comes more from hardware acceleration than it does raw performance. C++ does take care of more things for you than raw C (such as bounded strings), but still gives you freedom to tweak things. Not to mention a number of libraries needed to render web pages are also written in C or C++.

For the time being, C++ does edge out the alternatives.

tshepang
  • 305
  • 2
  • 18
Berin Loritsch
  • 45,784
  • 7
  • 87
  • 160
  • 4
    For non-Apple and non-NeXT platforms, there is the GNUStep collection. It's *mostly* compatible with Cocoa, and runs damn near everywhere. – greyfade Jan 31 '11 at 20:13
  • 5
    Remember that Java mustn't use Swing (which is a crappy library) for the GUI. For instance, we have Qt bindings. – Anto Jan 31 '11 at 20:16
  • Agreed about Swing... Qt would have licensing issues if the browser writers wanted to release on any open source license other than GPL. Otherwise, the cost of Qt licensing would cause the browser to no longer be free--something that Opera is still struggling with. – Berin Loritsch Jan 31 '11 at 20:22
  • 2
    According to http://my.opera.com/kilsmo/blog/2008/01/29/opera-is-not-based-on-qt Opera is *not* based on Qt – Anto Jan 31 '11 at 20:33
  • 2
    I never said Opera was based on Qt. I meant to imply that a non-free browser is really hard to sell when there are so many excellent free options. – Berin Loritsch Jan 31 '11 at 21:00
  • Fantastic answer. Very informative and well explained! – Trufa Jan 31 '11 at 22:07
  • 1
    +1 - excellent answer. People like you make the world smarter. – orokusaki Feb 01 '11 at 01:27
  • Good answer. Although I am not a huge C++ fan, building something like a web browser it seems to be the right choice. – Craig Feb 01 '11 at 02:57
  • 1
    Don't say that nobody does GUIs in C. There are a lot of people who program for GTK+ in plain C. – Ken Bloom Feb 01 '11 at 04:49
  • 1
    I didn't say _nobody_ does GUIs in C. I just said that the majority don't. Or at least of the C developers I know (who do kernel level stuff, drivers, embedded software, etc.). – Berin Loritsch Feb 01 '11 at 04:59
  • +2 - Thanks Berin! that was a Great Answer. It's well structured and very informative. – Nipuna Feb 01 '11 at 05:15
  • I find Java Swing GUIs very fast, and they are graphic accelerated. – Jonas Apr 04 '11 at 23:17
  • @Jonas, graphics accelerated Swing apps is a very new development. It's one of the reasons why Eclipse created its alternative to Swing. I will say this based on my own experience developing Swing apps: you have to be a lot more careful than with alternative UI platforms. – Berin Loritsch Apr 05 '11 at 01:59
  • @Berin: You are right about that. – Jonas Apr 05 '11 at 06:38
  • 8
    Availability of parser generators isn't really that relevant — in all browsers HTML, XML, and JS parsers are hand-written, and CSS is in some. – gsnedders Jul 31 '13 at 16:29
  • @BerinLoritsch Hardware accelerated swing came out with 1.5 I don't consider a decade ago a very new development. http://docs.oracle.com/javase/6/docs/technotes/guides/2d/flags.html – stonemetal Jul 31 '13 at 16:46
  • There are a few inaccuracies here. First of all, yes, GUI development in Java might suck (maybe less so with JavaFX) but so far all the alternatives in C++ I have seen suck at least the same amount so that's not actually a fair comparison (simply saying one option sucks, so the other must be better). Furthermore, at least Swing is hardware-accelerated for a while now and you can access OpenGL from Java. Regarding C#, bringing WPF into the discussion when you mention cross-platform support is a non-starter. Besides, C# *didn't even exist* when development of most current browsers started. – Joey Jul 31 '13 at 18:28
  • Also worth noting that users don't need to install a VM to run a C++ browser. – Thomas Eding Jul 31 '13 at 18:56
  • Honestly a lot of it comes down to the enemy you know vs. the enemy you don't. I doubt you'll be able to get an all Java browser to be as performant as Firefox (possibly Internet Explorer but that's a very low bar). I made my living doing Java programming for a while, and it's the last thing I would choose for making a browser. There's a lot of practicalities involved here that neither Java or C# address. – Berin Loritsch Jul 31 '13 at 18:58
  • @BerinLoritsch I'm not an IE user by any stretch but doesn't it beat Firefox in almost every benchmark these days? – Kirk Broadhurst Jul 31 '13 at 20:14
  • Depends on which version of IE and Firefox you are comparing. Benchmarks are also only part of the picture. I've always found IE to be very slow and cumbersome to work with. Still have to support it with some apps. – Berin Loritsch Jul 31 '13 at 21:09
  • Comment on C#: WPF is not only new. Using it to build a modern Web browser will also introduce portability problems since it is a proprietary technology targeting exclusively Windows platforms, and when developing a solution like a Web browser that can potentially be interesting to a very wide audience it does not make too much sense to restrict yourself to a single platform. – akhilless Sep 29 '13 at 12:08
  • @akhilless, wording like "to restrict yourself" is misleading. WIth Windows you have access to 75% of PC's where a modern browser would be able to benefit performance wise the technology in question, not counting XP which would bring the percentile of targetable clients to around 84. Buddy, that's a pretty big market for a "restrictive" platform. And with Xamarin on Android you'll be able to port around 90-99% of your app depending on your .NET skill to a 75-78% of the mobile device market made up of Android and WM. Back in 2013 when you made the comment things were looking up for .NET. – JasonXA Nov 08 '15 at 22:18
  • @JasonXA appreciate your comment. But I clearly wrote that word restrict refers to the number of platforms that can be supported by WPF. If you use WPF your are restricting yourself to one platform. I neither did discuss the marketshare of Windows nor named it a restricted platform. – akhilless Dec 28 '15 at 20:42
  • @akhilless That's what the word implies. It doesn't matter the count, be it 1 or 10. – JasonXA Jan 03 '16 at 22:07
91

I've decided to write a novel about this in hopes that people will gloss over it and upvote me. No, no, just kidding! I suffered over every word. Every word, I tell you!

Ask 'when' before 'why'

All major web browsers can trace their origins back to the 90s. Konqueror became Safari and Chrome; Netscape became Firefox; IE and Opera are still IE and Opera. These browsers all have a 15 year head start on incumbents.

I suggest you even try to name an acceptable cross-platform (Windows/Mac/Unix and even worse) language that was available in around 1995 when modern browsers originated. To build the core in anything but C/C++, you'd probably have had to build or buy and modify a compiler and platform libraries.

How about today? What are the alternatives?

Just for fun, let's think about the problem today. Yes, there are alternatives, but there are still major problems.

Language choice presents at least these problems:

  1. Knowledge problems - Hiring/training developers or attracting contributors
  2. Organizational/social problems - Language acceptance
  3. Language implementation: Speed, platform support, tooling
  4. Language power

1: Knowledge problems

Where do you get people who know the language or can learn it? This is an obstacle for languages like OCaml, F#, Haskell, Common Lisp and D that are fast and high-level enough to write a browser in nicely, but have few followers (In the 10k-100k range, maybe) even if you liberally count all the hobbyists and academics.

2: Social/Organizational problems

Corollary to the cargo-cult answer above:

  • An open source browser not using C, C++, C# or Java will supposedly have difficulty with contributors.
  • A proprietary browser not using C, C++, C# or Java will get project managers severely yelled at in most organizations.

3. Technical problems

Even in modern times, you need a fairly fast language for the computation intensive parts of rendering pages and running Javascript. You can choose to supplement that with a high-level language for building GUI elements, etc. (e.g. the Firefox approach of C++ and Javascript) but you have to have close integration between the languages; you can't just say "Okay, C# and Lua." You'll probably have to build and debug that bridge yourself unless you choose C or C++ as the base language.

Cross-platform development is another bag of worms. You could use C# or F# and cross your fingers on GTK# and Mono being alive and well in the future. You could try Common Lisp, Haskell, OCaml... Good luck getting everything working on Windows and Mac and Linux.

4. Language Power

After all of that, you have to build an enormous amount of functionality, so if you choose a low-level language you need an even huger army of coders than before. Note that no one has really built a browser from scratch in about fifteen years. That's partly because (surprise!) it's hard.

Specifically, having a Javascript interpreter is problem 3 (acquire one) or problem 4 (build one).

Conclusion:

If you developed a three-platform (Windows/Mac/*nix) browser today (early 2011), what are some of the choices?

  • C: See (2). Everyone's going to clamor for C++. Have fun selecting a cross-platform toolkit or building one (1, 2, 3 and 4). See also (4); have fun building a stable, secure browser in it.
  • C++: Have fun selecting a cross-platform toolkit or building one (1, 2, 3 and 4). Have fun (4) building stable, secure browser in it.
  • C or C++ and HLL: Your best bet. Pick your poison on the dynamic language; See (1) and (2). Too many good languages, too few followers of each. (1, 2, 3 and 4) on toolkit.
  • Java: Second best bet, if you have to please middle management. See (4); building huge things in Java takes a lot more code than in anything else on this list but maybe C.
  • Scala: Beats Java on (4); (1) and (2) but it's catching on.
  • C and Javascript: As a special case, this is appealing because you already have to build or acquire and assimilate Javascript interpreter. (Hence Firefox.) (1, 2, 3 and 4) on toolkit; the Mozilla people built their own IIRC.
  • C#: Have fun on (3). You're probably stuck with GTK#, however good that is, or building your own layer and renderer above GTK# and Windows Forms.
  • Ruby/Python/Perl/Racket/Lua/Erlange etc.: You've got (3) on cross-platform widget libraries and speed. Moore's law is with you on (4); the growing demand on browsers is against you.
  • OCaml, Haskell, Common Lisp, Smalltalk: (1) and (2) in spades. No speed issues, probably, but (3) for cross-platform development, and you'll have to build your own everything or bridge to C/C++ libraries somehow.
  • Objective-C: (3) I'm not sure how cross-platform development would work out here.

If we see another major browser rise in the next few years, I would bet it will be written in C or C++ and a dynamic language (Like Firefox), whether open source or proprietary.

Edit (July 31, 2013): Commenters on Hacker News seem to be mentioning Rust and Go (not specifically in connection with my answer), which fall vaguely into the "miscellaneous fast" bucket. Trying to keep this list of languages egalitarian and up to date will be a losing battle, so instead I'm calling it a representative sample as of the time of writing and leaving it alone.

Jesse Millikan
  • 1,601
  • 15
  • 22
36

Speed

As ugly as it is, C++ is still what you use when you want a fast application and full control over the code.

This is why games, non-core parts (such as file importers) of Office, and more are still written in C++.

Edited to include response from MSalters

Ryan Hayes
  • 20,139
  • 4
  • 68
  • 116
  • 3
    Other than games, I don't believe that these reasons are why those apps are written in C++. Though if you have first hand knowledge I'm happy to be proven wrong. – Henry Jan 31 '11 at 21:37
  • 2
    first hand knowledge? VS 2010, Office 2010 are both huge app suites yet they are extremely fast at what they do. While both have a rather large COM legacy, and MS heritage, performance is still the most important thing for users. – Anonymous Type Jan 31 '11 at 21:42
  • 8
    What else would office be written in? VB? C and C++ are the only options for Microsoft to write large apps in. Don't say C# please – Toby Allen Jan 31 '11 at 23:45
  • 1
    @toby forgive my ignorance but why not C#? It is their language after all, I'd thought they'd champion it more. Isn't C# touted for it's readable code, productivity boost from its libraries, and almost comparable speed to C and C++? – greatwolf Jan 31 '11 at 23:55
  • @Toby: It's not Office, but Visual Studio 2010's interface is built on WPF/.NET 4.0. Using the right language for the right thing is what's important here. C++ is a good choice for the grunt work of some of these applications for speed. In VS2010's case, MS chose WPF because it is a great presentation framework compared to other options. – Ryan Hayes Feb 01 '11 at 00:04
  • 4
    @Victor: I haven't seen the source for VS2010, so it may very well be written entirely in C#. – Ryan Hayes Feb 01 '11 at 00:05
  • 1
    Office is mostly a .Net application nowadays, with C++ still used in many non-core parts such as file importers. Source: Microsoft Visual Studio team, by personal communication. – MSalters Feb 01 '11 at 09:02
  • 2
    Inertia is the real reason why most Windows applications are written in C++. How many applications you use are actually CPU bound? – Joren Feb 01 '11 at 14:01
  • @Ryan Hayes, @Victor T., AFAIK, Visual Studio still has it's [core written in C++](http://stackoverflow.com/questions/246913/is-visual-studio-written-in-winforms). But SharpDevelop and MonoDevelop are completely written in C#. – Bobby Feb 08 '11 at 11:54
  • 3
    If office is a C# app, why was the original Ribbon a MFC control, and we had to wait ages for a C# one to be developed? none of these huge apps will be rewritten in C#, they'll be wrapped in a WPF gui (like VS2010) and the bulk of the old code will be reused. Even MS doesn't have the resources to completely rewrite their biggest apps - not if they want to spend time adding features to them too. – gbjbaanb Apr 11 '11 at 23:14
  • C++... Ugly ? :( – dsocolobsky Oct 15 '11 at 08:58
  • 1
    "full control" lmao. They use templates for damn near everything in WebKit, and you can't even control the size of the ints in classes. Full control my ass. – MarcusJ Nov 01 '16 at 23:19
17

Portability

I can only guess, but you are mentioning software products that target multiple platforms, and C++ can be compiled to any platform.

Pete
  • 8,916
  • 3
  • 41
  • 53
  • +10 - Other than raw performance, I think this is the REAL reason most browsers are in C++ with the exception of IE. Most browsers work on multiple platforms and there are few languages/frameworks that can perform at the same level AND be cross-platform compatible. – Jordan Parmer Jan 31 '11 at 19:40
  • I thought this at first too, but for a GUI centric application like a web browser. Is C++ really that much more portable to other Operating systems than say Java? – JohnFx Jan 31 '11 at 21:44
  • 1
    Is a browser really that GUI centric? – Kris Van Bael Jan 31 '11 at 22:34
  • @JohnFx - Correct, the GUI part is probably not that portable. But the core of e.g. a browser application is about handling HTML, creating DOM trees, parsing javascript and executing it fast enough. A well designed application can easily share the same core and have different UI code for each platform. And yes, C++ is much more portable than Java (but without GUI), because for C++ you only need a compiler targeting the CPU. For Java you need the full framework. – Pete Feb 01 '11 at 07:37
  • It was my understanding that most of the guts of HTML processing is done by a few core tools like WebKit. Perhaps it is because those are in C++ that the whole browsers are? – JohnFx Feb 01 '11 at 14:08
13

(I've been working on Firefox for about five years.)

The questioner is right that a lot of Firefox's code is C++, and in fact C++ is the majority if you count by lines of code (although that doesn't tell the whole story, since we have a lot of JavaScript, and JS is more concise than C++).

But in reality, Firefox is written in a lot of different languages:

  • C++
  • C (NSS, NSPR, various libraries we've imported)
  • x86 and ARM assembly
  • JavaScript
  • XUL (an HTML-like markup language) and CSS
  • Objective C (MacOS-only code)
  • Java (Android-only code)
  • Multiple custom interface-definition languages (XPIDL, IPDL)
  • WebIDL (another interface-definition language, but this one isn't custom, although the code generator is)
  • Python (code generators)

I sure am forgetting some.

This list is important because it hints at the incredible complexity that sits behind a web browser.

Yes, Firefox has a lot of C++ code, and yes, that has something to do with the fact that C++ was the best language for this sort of thing when Netscape was founded. But I also contend that there exists no better language today for a lot of what we do.

No other language has as strong an ecosystem of libraries (we rely heavily on external code). Few other languages give you full-stack control like C++ (we regularly tweak our custom heap allocator and do all sorts of memory-unsafe things to be faster or use less memory). Few other languages let you re-implement most of the standard library in a sane way (we have our own strings and collections implementations, tuned to our needs). Few other languages let you implement your own garbage collector. And so on.

Although C++ is the obvious choice for a lot of what we do, the people who suggest that we could write a browser in Java and write our own JVM if necessary are on to something. This is essentially what we do, but with JavaScript instead of Java. Of course, much of the browser isn't written in JavaScript. But a surprising amount is.

tshepang
  • 305
  • 2
  • 18
Justin L.
  • 101
  • 1
  • 2
12

Well, you'd have to ask the developers of those products directly to get the answer, but I suspect it's a combination of familiarity (it's what those developers knew best), performance (compiling to a native binary as opposed to bytecode), and tools (compared to languages like C, C++ is full of nice labor-saving gadgets like the STL).

John Bode
  • 10,826
  • 1
  • 31
  • 43
10

History

Each of the browsers has some history that influenced the choice of language.

For example, both Chrome and Safari are based on WebKit, which has its origins in the KDE project's KHTML part. KDE originally was created (in part) as a demonstration of the Qt GUI toolkit, so KDE is, overall, a C++ project. All new KDE projects were, at the time, written entirely in C++, so it was a logical choice for KHTML. It has since been ported to use other GUI toolkits.

Opera's Presto engine was written with performance and a small binary size in mind: C++ was the logical choice.

Microsoft's IE was written as a collection of ActiveX components, which could have been written in any language that has COM bindings, but was likely written in a subset of C++, because the majority of their codebase is already written in that language.

Netscape's Mozilla was written in C++ likely because portability was a major concern of theirs. C and C++ compilers are (virtually) ubiquitous, and so it was a logical choice.

There's no inherent technical reason for these choices. It just "seemed like a good idea at the time."

greyfade
  • 11,103
  • 2
  • 40
  • 43
8

Networking in C and C++ is easy to optimize, since you don't have to use libraries if you don't want to. I suspect that C++ is the language of choice because it allows the advantages of C:

  • Speed
  • Optimization
  • A certain amount of portability
  • Compiled language, not interpreted

coupled with the advantages of OOP:

  • Extensibility
  • Easier visualization
  • Better library support for non-critical tasks such as string processing and data structures
Michael K
  • 15,539
  • 9
  • 61
  • 93
  • Doesn't Java or C# has those advantages? – Nipuna Jan 31 '11 at 18:29
  • 5
    I have developed apps in both, and I'd say that for limited network functionality they are fine. However, I would not want to build anything that centered on the network portion as a browser does. I would want a lot more control over what was happening, and I would want a *compiled language*. – Michael K Jan 31 '11 at 18:32
  • Java and C# are also compiled. C# would have an advantage over Java when it comes to building the GUI--a critical part of any browser. Java would have an advantage with portability. Both Java and C# are recompiled on the target platform--for arguably better speed improvements. – Berin Loritsch Jan 31 '11 at 19:35
  • 5
    No, they are interpreted. They compile to bytecode and run on a virtual machine. That VM does not have a one-to-one correspondence from it's instruction set to the native one. – Michael K Jan 31 '11 at 20:28
  • 1
    You couldn't have this more backwards! Networking; you could shell out to curl and the browser would be just as fast. The network CODE is not the slow bit. And what is sockets if it's not a library? String Processing; This is the core of any html browser, it's NOT non-critical and I can't think of a single language that has worse string handling than C++ other than C. – Henry Jan 31 '11 at 21:53
  • @Henry I'm not sure what aspect of C++'s string handling you're not happy with, but IMO C++ handles strings better than any other language I've used. It's the only one I'm familiar with that has built-in operations and string manipulation algorithms that will work on all combinations of zero-terminated, length counted, 8-, 16- and 32-bit char types and mutable or immutable strings. And if that wasn't enough, it provides the language features necessary to write your own string implementation. – Jules Jan 22 '15 at 11:35
4

When the first lines of code for the first round of browsers were written, C# and Java did not exist. Nor did Ruby. Python may have been around, but it was still a tiny homebrew project at that point.

Basically, there really werent any other choices other than C++ that would allow one to build a browser that would be fast and run on many different platforms.

So why were they written in C++? Because that was the only language available that they could be written in.

GrandmasterB
  • 37,990
  • 7
  • 78
  • 131
  • The new round of browsers don't necessarily have that legacy constraint. – Berin Loritsch Jan 31 '11 at 19:39
  • 1
    Not sure what you mean by 'new round'. But FF and IE have code bases that go back to the mid 90s, and most new browsers use one of the rendering engines (for example, Chrome uses WebKit) – GrandmasterB Jan 31 '11 at 20:08
  • 2
    FF got rid of the legacy Netscape code (i.e. that mid 90s bloat) and implemented their own rendering engine. WebKit is also a comparatively new rendering engine (used by Chrome and Safari). IE still has legacy bloat which continues to weigh it down. So I'll disagree here. – Berin Loritsch Jan 31 '11 at 20:18
  • 2
    According to wikipedia at least, both gecko and webkit started around 1998. C# wasnt around then, and java was new on the scene (and super sluggish and truly awful at gui's back then), so that wouldnt have been a feasable option. So I dont know what other languages would have been suitable. Maybe Pascal. – GrandmasterB Jan 31 '11 at 20:28
  • 1
    @Berin Loritsch: Yes, but at no point did they simply throw out *all* the existing code and start over (on everything) from the very beginning, which is pretty much what they'd have had to do to convert to another language. In addition, people who knew/know C++ well enough for their decision about FF to stick would be likely to switch to a different language anyway. – Jerry Coffin Jan 31 '11 at 20:40
  • 2
    @Berin Loritsch Rubbish. FF is still based on Gecko (1997) and Webkit is based on khtml (1998). – Henry Jan 31 '11 at 21:46
  • When Apple started WebKit in 2002, Java was clearly around. It is based on a 1998 code, but nobody forced them to use it in 2002. – LennyProgrammers Feb 01 '11 at 08:58
4

Because the browsers (e.g., HotJava, obviously enough written in Java) written in other languages have never been achieved any substantial degree of market acceptance/penetration.

I can't say anything about the current iteration (or most recent--hasn't been updated in quite a while) of HotJava, but when I tried it, lack of market penetration seemed (at least to me) extremely easy to understand -- it was ugly, slow, and incompatible with quite a few web pages. Ultimately, it seemed to be based on a premise that never panned out: that the web would consist primarily of Java applets, with HTML as little more than a wrapper telling which applets to display where.

Part of it is probably also historical: most of the big web browsers have been around a long time. When they were first written, the landscape was much different: C++ was a "hot" new language, so it was being used for a lot of new development. Browsers have become some of the most heavily used software around, while many others from the time have faded into oblivion.

I think the displayed "attitude" of the language has an effect as well: C++ (like C before it) has always emphasized practicality and pragmatism. That basic attitude tends to attract programmers who are also pragmatic. Many other languages place a great deal more emphasis on things like elegance -- and in so doing, they attract programmers who think the same way. The problem with that is what I call the "Lisp effect". Symptoms include:

  1. Endless arguments over the most elegant implementation of the most trivial things.
  2. Inability to freeze features and finish something that can be shipped (even with flaws)
  3. Inability to compromise. Anybody who disagrees with me is not just wrong, but must be either stupid or evil.

There are more, but you get the general idea (and yes, I'm exaggerating to some degree--but only to a degree). Yes, some of the code you get will be astoundingly beautiful--but chances are that it's six months late, and mostly incompatible with every other piece of code in (what's supposed to be) the system, and by the time you receive it there's a pretty fair chance something else has changed enough that you can't use it at all.

There are also languages that would undoubtedly work just fine, but (rightly or wrongly) simply don't have (or at the crucial time, didn't have) the market share for anybody to have ever written a browser in them. Given the size and complexity of a complete browser, it takes a lot of people and quite a bit of time to develop one. With that kind of investment, many people get relatively conservative about things like development tools.

Jerry Coffin
  • 44,385
  • 5
  • 89
  • 162
  • 1
    WRT point 3, I will definitely assert, without qualification, that writing network-facing software in the C family **is** either stupid or evil, because it entails either unknowingly (stupid) or knowingly (evil) working with a system widely known to introduce security holes that **will** cause harm to your users. It's morally equivalent to giving a soldier body armor with a target painted on it. – Mason Wheeler Jan 31 '11 at 20:03
  • 9
    @Mason: Your display of exactly the bigotry in question is certainly appreciated. – Jerry Coffin Jan 31 '11 at 20:10
  • 1
    Um Glassfish is a web *server* (technically, "app server"), not a web *browser*. It's now the reference implementation for most of the JEE standards. Doesn't detract from the second half of your argument, though. – TMN Jan 31 '11 at 20:11
  • @Jerry: You can call it "bigotry" if you want, but that doesn't change the facts, and the fact is that this has been known to be unsafe since freaking 1988! – Mason Wheeler Jan 31 '11 at 20:16
  • 1
    @TMN: Oops -- I must have remembered the wrong name. "HotJava" maybe? I can't remember for sure. Regardless of the name, Sun did release a browser written in Java that was slow, ugly, etc. – Jerry Coffin Jan 31 '11 at 20:16
  • 2
    @Mason: Nonsense. One security hole (that had already been fixed, but many sysadmins hadn't bothered to install updated code) isn't even close to proof that all code written in the same language "will cause harm" or anything of the sort. OpenBSD (for one example) has a considerably *better* security history than the Pascal-based version of MacOS even aspired to. – Jerry Coffin Jan 31 '11 at 20:20
  • @Jerry: You're missing the point. It's not the one specific exploit, (and IIRC the Morris Worm utilized more than one exploit to propagate,) it's the fact that the underlying language flaw that made the exploit possible cannot be fixed, and it keeps showing up even today, more than 20 years later, causing millions upon millions of dollars worth of damage *repeatedly.* – Mason Wheeler Jan 31 '11 at 20:39
  • 5
    @Mason: No, you are. First, the Morris worm exploited a proram that used `gets`, which is a terrible function, but hardly unavoidable (and certainly not "fundamental" to the language, or anything like that). Second, C++ is not the same language as C in any case. Third, OpenBSD demonstrates quite nicely that secure software can and is written in C. There's no "underlying language flaw" that prevents writing solid, secure software in C. The tiny market share of OpenBSD indicates that security isn't a major concern for most people. – Jerry Coffin Jan 31 '11 at 20:48
  • @Jerry: `gets` isn't the fundamental flaw I'm referring to; the buffer overflow in `gets` is, and the number of buffer overflow errors that have consistently shown up since then indicates that it's not as simple to write solid, secure software in C as you seem to think. – Mason Wheeler Jan 31 '11 at 21:07
  • 6
    @Mason: the buffer overrun in `gets` is a simple consequence of the fact that you don't pass it the length of the buffer you're using. Nothing fundamental to the language about it -- you could do the same thing in Pascal (and I have). Writing software that's secure against an intelligent attacker isn't easy regardless of language. Based on experience in all three, it's a little easier in C than in Pascal, and a *lot* easier in C++ than in C. – Jerry Coffin Jan 31 '11 at 21:17
  • @Jerry: How did you pull that off in Pascal? It's a lot harder to screw up because the length of the buffer is part of the parameter type and the compiler can apply bounds checking. (Unless you're working directly with pointers, of course, which 99% of the time you shouldn't and don't have to in Pascal.) – Mason Wheeler Jan 31 '11 at 21:45
  • @Mason: ISO Pascal had a conforming array type (I think that was the name) that let you write a routine that took arrays of different sizes, and you had to pass the size separately, about like in C. In Turbo Pascal, it only takes a simple comment (`{R-}`, if memory serves..) – Jerry Coffin Jan 31 '11 at 21:54
3

Cargo-cult programming. The perception that "C++ is fast" is still out there, (despite poorly-thought-out language-level features like its badly broken object model that slow things down,) and people want their browsers to be fast, so they write in C++.

In a sane world, people writing network-facing software would be horrified at the mere thought of using a language that comes saddled with all of C's inherent security issues, and actually doing so would be an act of criminal negligence. (Just look at how many buffer overflow exploits have been found against various browsers in the last 15 years or so! How many millions of dollars of damage are these coders responsible for?)

There are other compiled languages capable of creating fast binaries. The problem is that they don't have the same exposure as the C family, and we all have to suffer for it.

Fun fact: By the time the Morris Worm hit the Internet in 1988, conclusively demonstrating the problems with writing OSes and network-facing software in C, (which have still not been solved to this day, because they're inherent flaws in the language,) Apple had been releasing the most advanced operating system the world had seen so far, for several years already, written in Pascal.

Mason Wheeler
  • 82,151
  • 24
  • 234
  • 309
  • +1 for the cargo cult ref. What language would you use? – Michael K Jan 31 '11 at 18:59
  • 2
    @Michael: based on his history, it's pretty easy to guess that Mason's answer would be "Delphi". – Jerry Coffin Jan 31 '11 at 19:08
  • @Jerry: Or some flavor of Pascal in general. It tends to get the details that the C family gets wrong right, frequently *before* the C family gets them wrong in the first place. – Mason Wheeler Jan 31 '11 at 19:10
  • 15
    Huh? I liked the Mac OS just fine, but to call it the most advanced OS the world had seen is silly. It wasn't even in the same ball park as UNIX and VMS, let alone the IBM big iron systems: single user, no virtual memory, limited process management. To be sure it had a very nice windowing system, but even that was derivative from Xerox Parc, and a bunch of academic projects. I also think that much of the Mac OS was actually written in M68k assembly. The libraries used the Pascal function call standards because it was expected that Pascal would be the primary application language. – Charles E. Grant Jan 31 '11 at 19:18
  • @Charles: Your list of things that the Mac OS didn't have is a bunch of things that it didn't *need*. It was designed to fit the needs of ordinary users and make it simple to work with, which is far more difficult than building a system for other engineers like UNIX, VMS, etc. And yes, some of the underlying concepts came from PARC, but most of the fundamental UI principles we still take for granted today were Apple innovations, not PARC stuff. – Mason Wheeler Jan 31 '11 at 19:25
  • @Charles: As for the 68k assembly, most of the OS was in fact written in Pascal, with QuickDraw being one of the major exceptions. On the very early models, the hardware was too limited to run the output of the pascal compilers they had fast enough to have a usable system, so they basically compiled the code to optimized ASM by hand. – Mason Wheeler Jan 31 '11 at 19:28
  • 5
    Pre-2000s Apple's operating systems weren't any more stable than the MS equivalent. When I was working two projects in the 90s, one with Mac OS and one with NT I had the same number of crashes and reboots required. Now they are all some sort of C based language. (Apple uses Objective-C for its current stuff). Security may be more difficult in C based languages, but using a different language doesn't make it suddenly more secure. – Berin Loritsch Jan 31 '11 at 19:29
  • 13
    @Mason, the fact that the Mac didn't need those features at the time doesn't mean those are insignificant. You made an unqualified statement that the Apple OS was the most advanced in the world, but apparently what you really meant was that it had the most sophisticated user interface. That is a defensible statement, but rather less grand than what you wrote. Writing a usable GUI is hard. Writing a reliable paged memory system is hard. Saying that one is more significant then the other is silly. Consumer level computing as we know it now couldn't exist without both. – Charles E. Grant Jan 31 '11 at 19:39
  • @Charles: I didn't mean to imply that those are insignificant. Just that they weren't needed at the time, and when they *were* needed, they got added. Mac OS had virtual memory and pre-emptive multitasking by the mid 90s. On the other hand, the *nixes are just barely beginning to get good, workable UIs over the last couple years. – Mason Wheeler Jan 31 '11 at 19:58
  • @Berin: Well, your experience was very different than mine. The only time I *ever* saw Mac OS Classic crash and require a reboot was when I was programming on it and did something stupid. – Mason Wheeler Jan 31 '11 at 19:58
  • 5
    @Mason: your experience clearly differs (hugely) from anybody and everybody else's -- in many respects. :-) – Jerry Coffin Jan 31 '11 at 20:27
  • 3
    @Mason: LOL at referring to pre-Mac OSX as "advanced". Apple OS was a constant source of crashes, not least because of its extremely rudimentary file system. – Anonymous Type Jan 31 '11 at 21:52
  • 1
    @Mason: MacOS was lagging a lot behind MS Windows in sophistication for a long time. You can say that it was limited in what it tried to do, but such minimalism isn't itself advanced. I stuck with it because I liked the UI and some of the software for it was great (ever use Macintosh Common Lisp?), not because there was anything else advanced about it. Heck, when did you get to stop assigning hard memory sizes to applications? – David Thornley Jan 31 '11 at 22:22
  • @David: You say that like it's a bad thing. Memory limits are something I really miss. If Windows had that, there would be a lot less bloatware clogging up our systems today. – Mason Wheeler Jan 31 '11 at 22:26
  • 3
    @Mason: It's becoming increasingly clear that we're from two different universes. Memory limits were a guessing game. Guess too high or too low, and something has memory issues. They did not reduce bloat, but rather instantiated it. In the meantime, the OS accumulated cruft. There were something like four different incarnations of the File Manager by the end, and MacOS had to support all of them. – David Thornley Jan 31 '11 at 22:46
  • @David: What does the file manager have to do with memory limits? And are you honestly saying you honestly prefer the model where every process on the system supposedly has the entire multi-gigabyte virtual address space to play with and ends up consuming a good fraction of it as often as not, leading to several seconds of disk thrashing for simple tasks like restoring a minimized window? I once heard, back in the late 90s, someone say that "every coder should be required to ensure their app runs properly on a 486." 486es are a bit more dated now, but I like the basic spirit of that idea. – Mason Wheeler Jan 31 '11 at 22:54
  • @Mason: The file manager was something else I remembered from the old days. And, yes, I prefer the model where the memory I paid for gets used on demand, rather than as how somebody guessed. I don't get thrashing like you describe. Further, I paid good money for my Core 2s (yeah, a little old now), and want to be able to run applications designed for them. – David Thornley Feb 01 '11 at 14:40
  • Mason, C++ it's not just considered fast, it *is* fast, I'm not sure what you mean by "broken object model", C++ is a performance oriented language designed for performance, C#/Java aren't, ironically nor are languages that are perceived as playing in the same turf as C++ like D and Go, yes a big part of writing in C++ is the momentum it's tracked, but make no mistake, if you want absolute control about every aspect of the execution, and a large set of abstractions that cost virtually nothing, C++ is one of the only few (if not the only) options out there – Ion Todirel Aug 10 '13 at 05:16
2

Access to system-level APIs

All browsers have to interface with the OS at some point, and most of the major OSes have well-established C and C++ APIs and libraries. It's usually easier to work with those APIs in C or C++ rather than write wrappers.

TMN
  • 11,313
  • 1
  • 21
  • 31
0

Legacy compatibility - can't throw away old code

It has nothing to do with the merits of C++ vs other languages. You can surely write a better browser from scratch in a language like Haskell; a project this important could even implement their own JVM if they needed to guarantee some performance characteristics. Like how Facebook wrote their own PHP compiler/optimizer.

A browser that breaks on non-standard markup is worse than useless. Legacy compat is so critical and so complex that a rewrite is just not an option. Lots of money and time is invested in battle-tested security etc, you can't just throw that investment away. Again, like how Facebook is still written in PHP.

Dustin Getz
  • 197
  • 4
  • I can understand why people might not upvote this... but downvoted? That's weird. Here's a +1 for you. – Thomas Eding Nov 14 '13 at 00:10
  • You have a (small) point, but your first sentence throws that away. Of course it does have to do with the merits of C++ vs other languages as well. – chtenb Apr 27 '14 at 10:38
0

Control and Portability

most of the speed arguments can go either way, but in anything where you need precise control over how something gets done many of the higher level languages will rain on your parade. There are exceptions to this, but most of them are not cross-platform enough to count in something like a browser.

Bill
  • 8,330
  • 24
  • 52