News for hackers. Stuff that matters.
Re:Std::string half of all allocations in the Chrome browser... by rwmj on Friday December 05, 2014 @11:28AM (#8705287)
Or proper garbage collection so you don't need to worry about who owns the string and you don't have the big overhead of ref-counting.

Why on earth are they using C++ for a web browser anyway? That's about the worst possible choice of programming language for that problem domain.

Re:Std::string half of all allocations in the Chrome browser... by kibwen on Friday December 05, 2014 @11:52AM (#8705427)
Because they forked it from WebKit which is derived from KHTML, which has a history that stretches back to 1998. Better to ask which languages would have been a better fit for a web browser in the context of the late nineties that have had the staying power of C++ (I'm guessing you'll come up with a rather short list).

If you want to see an attempt to write a new browser engine in a language that is memory-safe by default, I suggest you check out Mozilla's Servo engine: https://github.com/servo/servo

Re:Std::string half of all allocations in the Chrome browser... by titanomachy on Friday December 05, 2014 @11:56AM (#8705457)
This is awesome! Tons of respect for Mozilla for supporting this kind of research.
Re:Std::string half of all allocations in the Chrome browser... by titanomachy on Friday December 05, 2014 @11:51AM (#8705423)
All 5 major web browsers are written in C++.

EDIT: Chrome, Firefox, Safari, IE, Opera

Re:Std::string half of all allocations in the Chrome browser... by hutattedonmyarm on Saturday December 06, 2014 @06:52AM (#8709019)
Sure that Safari is C++ and not Objective-C?
Re:Std::string half of all allocations in the Chrome browser... by mahouse on Saturday December 06, 2014 @09:26AM (#8709250)
Well WebKit is C++, but the UI is probably Obj-C.
Re:Std::string half of all allocations in the Chrome browser... by IvyMike on Friday December 05, 2014 @11:50AM (#8705413)
So which web browsers are not written in C++?
Re:Std::string half of all allocations in the Chrome browser... by rwmj on Friday December 05, 2014 @11:55AM (#8705452)
Almost all of Windows (including apps that predominantly do string and tree manipulation) is written in C++. Almost all of Linux userspace is written in C. These are still terrible, insecure, fragile languages which cause frustration and loss to programmers and users every day.
Re:Std::string half of all allocations in the Chrome browser... by vardump on Friday December 05, 2014 @03:44PM (#8706784)
Shame you're downvoted, because you're pretty much right. Don't get me wrong, correct code can very well be written in C/C++. It's just 95% of the programmers using those languages are not very skilled. The programs tend to be wrong and fragile. In C++, most of this fragility derives from its C-roots.

C/C++ is also a poor match to today's CPUs. It's very slow compared to what the hardware is capable of. Compare for example with Intel ispc (https://ispc.github.io/). It gives some idea how much performance we're currently missing.

When C was young, memory latency was typically 1 or 2 clock cycles. There was no pipelining, at most a simple state machine that would finish in a few clock cycles. A branch didn't cost much. A few cycles at most. Neither did a pointer reference. Random access was almost as fast as sequential access.

Today's CPUs have memory latency of 150-300 clock cycles. A modern CPU core can typically retire 1-4 instructions per clock cycle. A single instruction takes typically about 16 clock cycles from decoding until retire. So CPUs have to often execute blind and just guess where the execution flow will go. Branches modify this flow. CPUs simply guess the flow, branch predict. When they're wrong, they just have to invalidate currently executing instructions and start again. Branches are something to avoid. Especially unpredictable ones. Function pointers are branches. Even though they can be predicted, they often just fall out of the branch predictor cache.

We need something that can minimize the costs modern CPUs are bad at. C/C++ is very branchy and uses slow function pointers often (vtable, switch jump tables, etc).

The problem is, there's more variation among CPUs than ever before, even within same instruction set architecture. For x86, not only the costs for instructions are wildly different, but the instruction set support is fragmented.

C/C++ is not the last word. Unsafe and way too slow compared to what current hardware is capable of. The problem is, the language that is safer and a good match just does not exist yet. Some safety can be sacrificed for greater speed, but it should be situational choice by the programmer, not the only way or even default.

C/C++ is what I do at my day job.

Re:Std::string half of all allocations in the Chrome browser... by tedunangst on Friday December 05, 2014 @04:54PM (#8707141)
Neither of you are really answering the question. You still haven't proposed a language is better.

When the half dozen browsers that represent about 99.9% of the browswer market all use C++, perhaps the explanation is not "They're all wrong."

Re:Std::string half of all allocations in the Chrome browser... by vardump on Friday December 05, 2014 @05:18PM (#8707253)
> When the half dozen browsers that represent about 99.9% of the browswer market all use C++, perhaps the explanation is not "They're all wrong."

I didn't say they're wrong by any means! C++ is the horse and cart. Performance wise, the programming language automobile doesn't exist yet. Of course one should and must use what is currently available!

The best language for writing a web browser does not exist yet. Or for writing other similarly complicated applications with performance requirements.

So, yes, C++ is probably the best language for writing a web browser right now. The cleanest dirty shirt. Certainly not the safest, but out of high level languages it has the best performance. It has large enough work force, important if you're building a large project. C/C++ interfaces with current operating systems natively. And a lot of other considerations make it a sensible choice.

But C/C++ is rather far from what a language could be. It's very bug prone. Overcomplicated. Hard to maintain.

And it is slow. Not compared to other languages, but to CPU performance potential. I can write very fast software in C++ -- if I effectively fall back to assembler or at least compiler intrinsics. In other words, by being a human compiler.

Re:Std::string half of all allocations in the Chrome browser... by pjmlp on Friday December 05, 2014 @05:46PM (#8707364)
Around 20 years ago we had quite a few systems programming languages to choose from.

The widespread of UNIX into the industry, killed the other languages because they weren't the UNIX system programming language and other system vendors weren't able to fight against UNIX based Workstations.

So like JavaScript in the browser, C eventually killed the alternatives in the workstation market.

C++ was able to gain industry acceptance because C++ compiler vendors were actually C compiler vendors bundling C++ in their products, since C++ came from AT&T as well.

Additionally, the computers started to be fast enough for mainstream VM based systems.

The business application developers moved along to VM based languages, while the system programmers focused on the languages that were supported by larger OS vendors, C and C++.

All the compiler vendors selling system programming languages compilers that didn't enjoy first class treatment on a mainstream OS, either closed down or changed business.

So 20 years later, C and C++ became the only alternatives for systems programming, unless one wants to play with dead languages.

With Ada being used in projects that required it, or for software scenarios where human life are at risk like aviation, train control systems, medical devices and so on.

And now we are getting beaten every day in security exploits due to daggling pointers and out of bounds errors.

Re:Std::string half of all allocations in the Chrome browser... by vardump on Friday December 05, 2014 @06:26PM (#8707552)
C++ bugs are sometimes - too often - faster to find even from disassembly than from source code. Seriously. Yes, it's a very slow approach. But you can see what's really going on, and the picture is shockingly different from the source code point of view. While disassembly is verbose, there's no syntactic sugar that can obscure true function. C++ features, like operator overloading and exceptions, locally hide actual functionality. I've seen another language that has a lot of different ways to do same thing and enabled write-only development. Perl.

No, disassembly listing is not what I typically use for debugging. Just one of the tools. The point is, something is wrong when disassembly can be easier to understand than source code!

So depressing when you need to deal with it often. Our best tool is not very good.

Well, all this puts bread on my table, so I guess I shouldn't complain too much...

Re:Std::string half of all allocations in the Chrome browser... by ConceptJunkie on Monday December 08, 2014 @11:14AM (#8717159)
Well, the problem with C++ is that it's way too easy to take its features too far. I've seen commercial C++ libraries that overloaded operators to an absurd degree. I don't recall the name at the moment, but it was a database library.

Overloading operators is a great feature, but should be used as sparingly as possible, and only where it makes intuitive sense.

Templates offer even more rope to hang yourself, and even though I'm a late convert to using templates, I would never suggest the feature goes too far. It's kludge fuel, no doubt, but the language needs the feature, and it allows you to do things that are truly useful and elegant.

It never occurred to me that viewing the disassembly could be useful for debugging, and I would imagine it's not normally useful unless you are really doing some pretty sophisticated stuff with the language. It would be extremely educational to understand what the compiler actually does however, but if inspecting the disassembly is instructive on how best to use the language, then I would suspect the language design, or at least the compiler, is doing something wrong.

I do think C++ requires a little too much consideration of how certain operations are implemented, e.g., this very discussion, but it's a price I'm willing to pay for a language that lets me do things any way I want to do them.

I program in Python in my spare time and for small scripting tasks, and can't imagine choosing C++ over Python for any personal project I've done in the past couple years (which tend to be small anyway), but I use C++ at work and am very happy to continue using it after 20 years on and off (mostly on).

Re:Std::string half of all allocations in the Chrome browser... by fleitz on Friday December 05, 2014 @04:22PM (#8706985)
>> It's just 95% of the programmers using those languages are not very skilled.

High level languages just make it easier for crappy programmers to approximate a working solution.

Re:Std::string half of all allocations in the Chrome browser... by ConceptJunkie on Monday December 08, 2014 @11:21AM (#8717214)
This is true for C++ and doubly true for Java.

In 20 years, I don't think I've ever seen a commercial third-party C++ library that I thought was competently designed and implemented. I was about to offer the exception of mysql++, but then I remembered it's an open-source library so it wouldn't count as commercial. C++ seems to breed a lot of crappy but marginally useful code.

I haven't done much Java, and I don't think it's a bad language, I've never seen a Java development environment that I would even wish on my enemies. Java seems to breed bloated over-engineered but under-flexible designs that require the programmer to do way too much of the kind of stuff that we invented computers to do for us in the first place.

Re:Std::string half of all allocations in the Chrome browser... by spinlock on Friday December 05, 2014 @08:28PM (#8707982)
what languages are more appropriate for today's hardware? The majority of my experience in C (Linux) is on embedded hardware (and quite a few years ago). I've never run into wasted cycles before but that setup is really different from a modern cpu on a desktop.
Re:Std::string half of all allocations in the Chrome browser... by panzi on Friday December 05, 2014 @12:09PM (#8705510)
> Almost all of Linux userspace is written in C.

Depends on what you count. Qt can be seen as (part of the) Linux userspace. KDE applications are usually written in C++ (partially in QML+JavaScript), Gtk applications are written in C or Vala (a few in C#). If you don't count DE frameworks basically the only thing left would be libc, which of course is C (but available in some form under every noteworthy OS).

Re:Std::string half of all allocations in the Chrome browser... by srj on Saturday December 06, 2014 @08:39AM (#8709191)
What's fragile, insecure, and terrible about them? I've written C/C++ (among other languages) for years at work and it's quite rare that I see a bug that could be attributed to the language.
Re:Std::string half of all allocations in the Chrome browser... by robotresearcher on Saturday December 06, 2014 @09:45AM (#8709281)
You rarely see uninitialized pointers or off-by-one array bounds errors? I wish that was true for my colleagues.
Re:Std::string half of all allocations in the Chrome browser... by srj on Thursday December 11, 2014 @12:22AM (#8733330)
Late reply - sorry that I missed your response.

I've found that RAII classes like std::unique_ptr have really helped. Errors like an uninitialized pointer are often caused by poor ownership semantics in the program design, and I don't think you escape that in reference counted languages. You'll still have a messy class design without clear ownership, and there are types of resources other than memory to manage (file handles, locks, sockets).

I'm not saying that your colleagues are bad engineers, I've had many troubles with ownership design over the years and still do at times. I just don't think C++ is unique in this regard even if it does force you to think about it more regularly since memory interaction is so common.

Re:Std::string half of all allocations in the Chrome browser... by ibebrett on Friday December 05, 2014 @12:14PM (#8705537)
what experience do you have with writing c++? what is the last large project you completed with it? i'm not a huge fan of c++, but it has been the foundation of an extremely large number of wildly successful and pervasive projects.
Re:Std::string half of all allocations in the Chrome browser... by rwmj on Friday December 05, 2014 @12:44PM (#8705711)
I have in the past written many large C++ projects. I now try to avoid it as much as possible, but the last time I programmed in C++ was just two weeks ago[1], and it reminded me how bad it was -- terrible error messages, and abysmal performance of the final program. At some point I will have to replace it because the program doesn't meet our performance requirements, and no one can tell me why[2]. (Edit: Apparently [2] has been answered now)

[1] http://git.annexia.org/?p=virt-bmap.git;a=tree

[2] https://stackoverflow.com/questions/27152834/how-to-improve-...

Re:Std::string half of all allocations in the Chrome browser... by DannyBee on Friday December 05, 2014 @05:04PM (#8707182)
Of all the things you shudl expect from a non-C++ language, performance is highly unlikely to be on that list.

If you can't get something to be performant with C++, it's pretty unlikely you'll get it to be performant with most other languages.

(Unless you want to try it in fortran)

Re:Std::string half of all allocations in the Chrome browser... by tptacek on Friday December 05, 2014 @05:57PM (#8707425)
I expect C programs to outperform C++ programs, simply by virtue of being more explicit (about everything, from allocation to levels of indirection).
Re:Std::string half of all allocations in the Chrome browser... by vardump on Friday December 05, 2014 @06:45PM (#8707628)
Not always faster.

Try comparing qsort to stl::sort...

Hint: stl::sort is a lot faster. It can inline the comparison function. Ok, it just shows how slow function pointers are. I'm assuming compilers still can't optimize the function pointer away in qsort.

Of course a specialized sorter in C would be faster, one that can inline comparison function. C++ copy pasting automation... err, I mean templates are useful, because it can often inline your function and data type in the algorithm without using function pointers. At cost of generating same or similar algorithm code multiple times bloating the binary.

Re:Std::string half of all allocations in the Chrome browser... by nostrademons on Friday December 05, 2014 @01:53PM (#8706143)
Clang's error messages are much better than GCC - it will even spell-correct your typos and suggest which symbol you actually wanted to type. Around 2010 Google switched to a hybrid Clang + GCC environment (the same source code would be piped to both compilers, the error messages from Clang would be piped back to the user while the object files from GCC were linked to form the final binary), and C++ developer happiness skyrocketed.
Re:Std::string half of all allocations in the Chrome browser... by pja on Saturday December 06, 2014 @04:24AM (#8708789)
To be honest, I think gcc has pretty much caught up with clang with gcc-4.9 when it comes to error message quality.

I have a slight preference for the way clang chooses to highlight the entire AST parent node that it thinks is responsible for the error, but the approach g++ takes is perfectly clear.

Re:Std::string half of all allocations in the Chrome browser... by groby_b on Friday December 05, 2014 @08:39PM (#8708022)
It has been answered, and according to the answer, there's an easy 20x-30x speedup to be had. Maybe your problem is not C++ per se.

I wouldn't be surprised if std::unordered_set would yield further speedup. (It doesn't look like you need the order criterion). Other things worth trying are:

* Not using a set at all - it seems most sets only have 3-4 elements. You might as well use a vector. * It doesn't look like your intervals overlap, and it looks like they're already ordered. interval_map may be unnecessary, and it might be worth keeping the data in a simple vector. * You don't want to copy object_set, either. A const-ref is more than enough * And finally, if you want to veer into nit-pick territory, you want to use range-based loops. It might or might not yield better compiled code. It's definitely much more readable.

  for (const auto& iter : map->equal_range(window))
    for (const auto& obj: iter->second)
      f(iter->first->start, iter->first->end, obj.c_str(), opaque);
I don't disagree with the abysmal error messages, or the thought that C++ is a beast to learn and control. But if there's one thing it does give you, it's decent performance, if used properly.

Let's put it that way: The browser people don't write C++ because they think it's such an awesome language :)

Re:Std::string half of all allocations in the Chrome browser... by IvyMike on Friday December 05, 2014 @01:29PM (#8705980)
In #2, I'm curious why you didn't like sehe's answer?
Re:Std::string half of all allocations in the Chrome browser... by sedachv on Saturday December 06, 2014 @05:42AM (#8708908)
C: Lynx, w3m, Mothra Java: Lobo, gngr (https://gngr.info/) Limbo/Inferno: Charon Lisp: Closure, eww
Re:Std::string half of all allocations in the Chrome browser... by panzi on Friday December 05, 2014 @12:01PM (#8705481)
Sharing mutable buffers? Nope, you would still need to switch to immutable strings or copy the buffers.
Re:Std::string half of all allocations in the Chrome browser... by Scramblejams on Friday December 05, 2014 @05:43PM (#8707355)
By the way, reference counting may not have to be as bad as it is. Looks like there's a lot of opportunity for improvement. See here: http://users.cecs.anu.edu.au/~steveb/downloads/pdf/rc-ismm-2...
Re:Std::string half of all allocations in the Chrome browser... by ibebrett on Friday December 05, 2014 @12:15PM (#8705552)
how do you "share" objects that multiple pieces of code wan't to own. you need to copy if you are passing around a mutable string.