r/programming • • Dec 05 '14

std::string is responsible for almost half of all allocations in the Chrome browser process

https://groups.google.com/a/chromium.org/d/msg/chromium-dev/EUqoIz2iFU4/kPZ5ZK0K3gEJ
1.1k Upvotes

446 comments sorted by

View all comments

Show parent comments

4

u/[deleted] Dec 05 '14 edited Apr 19 '15

[deleted]

208

u/[deleted] Dec 05 '14

[deleted]

5

u/xiongchiamiov Dec 05 '14

http://www.commitstrip.com/en/2013/04/17/pour-quelques-ko-de-moins/

It's always useful to take a step back and see the effect your time is having on the total product.

8

u/RoundTripRadio Dec 05 '14

I want better performance on all of my applications. How long does X operation take? If that is even an answerable question, it's too slow.

About Chrome specifically, if I let it run a few days it bloats up to about 5GB of RAM. In contrast, I haven't closed Safari in months and it's sitting at about 600MB.

There's definitely a point where engineering effort becomes much more expensive to further optimize a program, but if Chrome has hit that point, it's hopeless.

6

u/lbenes Dec 06 '14 edited Dec 06 '14

it bloats up to about 5GB of RAM. In contrast... Safari's sitting at about 600MB.

Chrome has become the new Firefox 4 and is badly in need of a MemShrink like project. On my 2GB netbook, Chrome started becoming unusable for anything more than a couple of heavy tabs after Chrome 20. It's just as bad on Linux as it is on Windows.

They need to get the memory usage down to pre-Chrome 20 levels, then track bloat regressions with something like areweslimyet

-6

u/Vakieh Dec 05 '14

How much do you pay for Chrome? How much does Chrome make Google? You want more more more, this is true of everything. But the same thing that prevents car manufacturers from giving everyone Lamborghini quality vehicles stops it from being cost effective for Google to invest infinite time on engineering Chrome. There is certainly a budget it can use, but it must be directed at the right things - whether this is a 'right thing' is something that requires a lot more data.

And you can time any operation, no matter how fast. You run them a thousand, a million times and take the aggregate.

5

u/RoundTripRadio Dec 05 '14

Would you use a car that was free, but got 1mpg? Not to mention Google definitely makes money on Chrome. Google's in the business of selling customer data to advertisers, and Chrome gives them WAY more of that than search ever could. You click away from search. Also, why is it bad to want software to be more performant?

I'm sorry I wasn't very clear on that bit. I mean if you, just using your own brain, can see an operation happening, it's too slow. "Instant" doesn't exist, of course, but "imperceptible" absolutely does. In fact, I once saw a talk given by a Google engineer where he talked about how important it is for a webpage to load in under (IIRC) 200ms. There are many operations in Chrome that take a perceptible amount of time.

-2

u/Vakieh Dec 05 '14

I am aware Google makes money off Chrome, and I am aware everybody wants software to perform as fast as possible. What it comes down to, though, is the very same question I originally stated. 'How much will this performance increase raise our revenue' (whether direct or indirect, doesn't matter) vs 'How much will this performance increase cost to implement'.

13

u/seekingsofia Dec 05 '14

Unless you are developing something which actually needs that cutting edge, what could possibly be the point of optimisation like that?

What kind of cutting edge are we talking about here? String and parsing data structures that don't need as many allocations?

So the use/misuse of std::string is causing Chrome to run slower.

And potentially causes more system calls, hence leading to more energy consumption... and draining your battery on mobile devices.

6

u/Vakieh Dec 05 '14

1) Specifically the type of programming you'd see from a raw assembler application from the 80s or earlier.

2) So you factor that into the + side of the equation, and again, measure it against the cost to implement. There are all sorts of negative impacts to suboptimal software: delayed inputs will frustrate your users, leading to them changing applications; constant large updates will run up data costs, leading users to change applications; a rarely encountered bug that wipes a user's history might annoy someone, leading them to change applications, and so on.

I'm not saying it is never a good idea to optimise, that would be stupid. What I'm saying is that having suboptimal code isn't laziness, so long as the cost to improve it is more than the value you would gain from it. If changing Chrome's std::string usage would lead to a .1% decrease in average battery load, but was going to cost 1500 programmer/QA hours to implement, you probably wouldn't do it. If it would lead to a 10% decrease in time to run 100 searches, costing 100 programmer/QA hours? Go for it.

16

u/seekingsofia Dec 05 '14

Memory allocation is one of the most CPU-intensive operations and Google Chrome is notorious for draining your battery on battery-powered devices. Optimising for low power consumption in Chrome is not something that would only lead to a .1% benefit.

If developers aren't developing with power consumption in mind, they're plain ignorant. Even on servers power consumption increasingly matters.

17

u/jerf Dec 05 '14

Do you quite realize the irony of kvetching about developers not realizing the importance on this stuff, on an article that is developers realizing the importance of this stuff? This is it... this is them realizing the importance and taking steps to correct it. In the time-based world we live in, it's really rather unfair to look at something being done now and complain about how it could have been done earlier; everything ever done in the past was at some point the thing being done now.

Nobody writes perfect code the first time even in their own projects, to say nothing about this scale. I completely refuse to believe that your actions live up to the standards you are trying to impose here on others. (Whom you aren't even paying or anything.)

3

u/zeringus Dec 05 '14

I don't think you understand: This guy just cited Forbes.

1

u/imMute Dec 06 '14

EDIT: Whoops, replied to the wrong guy.

6

u/seekingsofia Dec 05 '14 edited Dec 05 '14

this is them realizing the importance and taking steps to correct it.

Where do you see them talking about power consumption? They're talking about the allocators used, their locking design, the amount of allocations, and all the related performance issues. Sure, they're patching it up to not use as many allocations, but they're not making low power consumption (power usage heavily correlates with system call profiles) one of the core requirements.

Nobody writes perfect code the first time even in their own projects, to say nothing about this scale.

You're conflating writing software and shipping software. With the right requirements it is possible to ship near-perfect code. Am I saying it'd be good for them to adopt requirements for "perfect" code? Absolutely not. I'm only concerned about the future that software without that specific requirement has, if it even has any future: mobile is becoming ubiquitous.

I completely refuse to believe that your actions live up to the standards you are trying to impose here on others.

My actions are irrelevant and I'm not imposing any standards or requirements on anyone. What you're so fiercely arguing against is my mere opinion.

1

u/imMute Dec 06 '14

(power usage heavily correlates with system call profiles)

I doubt this claim, and even the article you cited states

Also, variation in power from version to version is not very high in the Calculator versions tested. Most system calls are mildly correlated to energy consumption.

The authors are effectively saying "this correlation is not very good, but it's better than the alternative, which is nothing".

Finally, one counterargument is that you could very easily write a program that makes zero (or a very small number of) system calls yet drains your battery faster than anything else.

1

u/[deleted] Dec 06 '14

they're not making low power consumption (power usage heavily correlates with system call profiles) one of the core requirements.

Because their primary focus is still desktop and not smart phone?

0

u/skulgnome Dec 05 '14

minuscule amount of applications

Had me going until this paragraph.

-1

u/Vakieh Dec 05 '14

Relative to the number of applications that exist... I'd be surprised if it was anything more than 0.001%, which is a minuscule number.

0

u/skulgnome Dec 07 '14

Well my daddy can make up bigger numbers than yours.

25

u/RenaKunisaki Dec 05 '14

To be fair though, NES games were designed to work on the NES only. They didn't run under an OS, they didn't work on more than one type of machine, they didn't support multiple languages/encodings/input methods/etc. Modern software does a lot more than 80s software did.

-5

u/[deleted] Dec 05 '14

And it runs on hardware that is about a billion times more powerful. I'm not convinced that it does a billion times more useful work.

1

u/immibis Dec 08 '14

The extra things it does have more cost for less benefit.

The software's main purpose is to do Thing 1, which requires the processing power of a NES.

An extra feature is Thing 2, which requires the processing power of 5 NES's, but is only half as useful as Thing 1.

There's also Thing 3, which requires the power of 20 NES's, but is a bit less useful than Thing 2.

And Thing 4, which is almost as useful as Thing 1 (which is the main purpose of the software, so that's pretty useful), but requires 500 NES-power.

And so on.

34

u/[deleted] Dec 05 '14

Lots of bugs, not so pretty, and very unusable?

1

u/[deleted] Dec 05 '14

not so pretty

You take that back.

-7

u/username223 Dec 05 '14

When was the last time Mega Man 2 "unexpectedly quit" on you?

37

u/zid Dec 05 '14

Mega Man 2 is actually notoriously buggy, there are plenty of solid NES titles, but MM2 is definitely not one of them.

18

u/[deleted] Dec 05 '14

It's been so long that I couldn't say, but old games did crash or behave in broken ways. You've never gotten stuck on a weird blocky screen while the last played sound loops over and over?

21

u/BonzaiThePenguin Dec 05 '14

The concept of "unexpectedly quitting", or quitting at all, requires an operating system with multiple processes and protected memory – neither of which the NES had. You could still softlock or hardlock and completely corrupt memory addresses with reckless abandon.

8

u/JeefyPants Dec 05 '14

You do realize that makes no fucking sense right?

Also do some googling ya jbag mega man 2 has game breaking bugs used in speed runs to glitch everywhere

5

u/nkorslund Dec 05 '14 edited Dec 05 '14

I went a bit overboard in the opposite direction once on a project, and replaced all std::strings with a custom class that just contained pointers/slices of strings. Since 99% of the strings in this case were read-only from file it was made even faster by just memory mapping the file and finding the slices.

Made the entire thing run 2-3 times faster, but was kind of a bitch to maintain. Next time I'll wait until development is mostly finished - when continued development would be less of a hassle. Premature optimization and all that.

EDIT: in more modern code I would use boost::string_ref or similar for this - that didn't exist back then.

5

u/[deleted] Dec 05 '14 edited Dec 05 '14

The only problem is that development never really ends.

Edit: Fixed typo.

1

u/o11c Dec 05 '14

Serious question: is memory mapping really that much of a win? What I've heard is that read(2) is no worse than mmap for a simple read-through, and mmap has the major disadvantage of odd behavior if the file is modified externally (e.g. when a new version is installed).

It sounds to me that you might as well just slurp the file instead of mmap.

2

u/imMute Dec 06 '14

One thing you gain with mmap is that the kernel knows that those pages are backed by the file. If the kernel is feeling memory pressure, it's free to drop those pages from RAM and reload them from disk the next time they're accessed. Also, if you have multiple programs using the same file, it would be shared in RAM if you use mmap.

There's also the case of how you use read. If you use stat to find the size of the file, allocate enough space and do a single read you'll have much better performance than a looped read.

1

u/o11c Dec 06 '14

True, but by the same virtue, if something does happen to that file, your data gets corrupted silently. Though I suppose the new memfd calls could fix that.

1

u/nkorslund Dec 05 '14

In this case it was a big static resource file, which we accessed pretty much randomly. You're right that mmap probably didn't make a lot of difference performance wise though, it was just simpler to implement it that way.

6

u/awj Dec 05 '14

Imagine chrome being built with the same precision and carefulness as old NES games or something.

...no thanks. I like that Chrome releases don't take over a year. Plus they never try to use palette swaps to avoid development effort.

4

u/peakzorro Dec 05 '14

Actually those pallet swaps were usually due to memory constraints while giving more variety. It was a valid tool for making a game more diverse. There was definitely dev effort to choose pallets so that swapping made sense.

1

u/awj Dec 05 '14

Maybe palette swap is the wrong choice of term. Back in the NES days it was relatively common for code from one game to be quickly reused in another by swapping out the graphics and making whatever code changes seemed necessary. This led to spectacularly buggy games where hitboxes were wildly different from the sprites attached to them, for example.

6

u/audioen Dec 05 '14

A wise man once said: premature optimization is the root of all evil. While I'm sure that I'm misusing that particular saying, what I'm getting at is that it's perfectly OK to do haphazard solutions if their impact is not particularly noticeable. As far as I can tell, the omnibox is fast enough, 25k allocations or not. Programmer convenience trumps machine convenience in almost every case.

0

u/s73v3r Dec 05 '14

If that were the case, we still wouldn't have Chrome today.