
Add a const here, delete a const there - nikbackm
https://randomascii.wordpress.com/2017/01/08/add-a-const-here-delete-a-const-there/
======
chamakits
WARNING:

I believe the site has some malware injected in it. At least the mobile
version of it. After a few second it redirected me to some site with a fake
Facebook skin.

~~~
lorenzhs
Once again, this demonstrates that blocking advertisements is a really good
idea from a security perspective. It doesn't just reduce clutter and speed up
page loading times, but also prevents loads of malware issues. See also
[https://twitter.com/swiftonsecurity/status/62840155490772582...](https://twitter.com/swiftonsecurity/status/628401554907725824)

~~~
iokanuon
AdAway's Android host file seems to block it just fine, you don't even need a
fancy adblock plugin.

------
dzdt
This is begging for a C++ FQA style hate blog post. Paging Yossi Kreinin?
Seriously, "Delete const three times to move 53,064 bytes to read-only data
segment and save 729,168 bytes of code." WTF?

[1] [http://yosefk.com/c++fqa/index.html](http://yosefk.com/c++fqa/index.html)

[2] [http://yosefk.com/blog/](http://yosefk.com/blog/)

~~~
digler999
as others pointed out, its the compiler. but maybe this requires a Microsoft
FQA. Maybe these compiler "bugs" are really more microsoft "anti-features"
designed to slowly erode people's faith in C++ on MS platforms and nudging
them toward C# or other .NET "solutions" ?

I just can't believe that one of the biggest monolithic organizations with
unlimited budgets and decades of experience developing tools _for their own
platform(!!)_ would make such careless mistakes or miss out on rather obvious
optimizations. And it also sickens me that random devs out there are wasting
hours of their lives doing completely unpaid MS product QA work. At least when
jobs are outsourced to the third-world, the laborers are paid _something_.

~~~
goalieca
Microsoft compilers are dramatically improved since the VS 6.0 days. The days
before C# and vista. The days before herb sutter came in to save us.

------
tech2
I'm interested in the possible security improvements in the change which
didn't seem to be mentioned at all. Is all this data already in an NX area, or
was it somehow already protected via some other means?

~~~
brucedawson
None of the pointers were code pointers so there are probably no security
improvements. It might protect Chrome from a few hard to diagnose crashes
because now if a stray write hits those arrays it crashes instantly instead of
misbehaving later.

------
revelation
For embedded systems that extra const can mean the difference between having
that data in SRAM or Flash.

------
cixin
Wow awesome! It's partly these little optimizations that keeps C/C++ going.

I've never considered that const might have a performance benefit (and mostly
just use it as a compile time check). But will consider it in the future.

Would be interesting to see performance benchmarks for the changes made here,
as well as applying const in general.

~~~
brucedawson
Ignoring the VC++ compiler bug/quirk, the benefits for adding const tend to be
small and hard to measure. How do you measure the possible reduction in
private pages? How do you measure the productivity gains from slightly fewer
corrupted globals.

Usually you don't. You just tag your const globals as such and then get on
with your day. Once you develop the habit the cost is zero so measuring the
benefits isn't necessary.

------
adrianN
So will they put the consts back after the VS bug is fixed?

~~~
rkangel
If all the of the things which have had 'const' removed are members of structs
that are themselves 'const', then there is no need to re-add them.

------
bla2
MSVC is such a bad compiler. Yes, they are working on it, it's getting better,
but this is apparently a fairly new regression.

~~~
brucedawson
Chrome continues to use MSVC on Windows because it generates better code than
clang. So, MSVC is the worst compiler, except for the alternatives?

~~~
obl
Would appreciate a reference on that if you have it handy.

Is this more of a whole program measured X% regression on some perf benchs, or
specific target cases where the codegen is notably worse ?

~~~
brucedawson
I should probably take back my claim because I can't find the evidence to back
it up and I might actually be wrong. I thought I remembered that there were
some important cases where VC++ generated better code for Chrome, and some
binary size advantages, but I can't find my references.

------
wfunction
In my experience you should NEVER have const instance fields. Ever. If you
want a const reference, just make a pointer to const. If you need a const
value, just leave it mutable and access it through a const getter or work
around it some other way. The language just doesn't seem to have been designed
to make this particular feature actually worthwhile.

~~~
wyldfire
What's the drawback for a const member?

IMO `const` should be used liberally: members, locals and other declarations.

When you're on a team and you have a data structure that has const members, it
should give that newer/unfamiliar team member pause when they get the
compilation error because they added a method that modifies the `const`
member. Now they have an opportunity to take a step back and ask "Oh, why was
it designed this way? What other assumptions in the code are invalidated if I
need to remove the `const`?"

`const` for locals is good for those notorious ten-line functions that over
time multiply into a 150-line function. Yes, we should refactor that mega
function into something easier to digest. But right now I am trying to figure
out this bug: I can see that ^^ up there 'foo' was initialized as a particular
value, and down here it looks like it must have changed. Instead of looking
through all of the hundred lines of code to see who might've updated 'foo', I
know right away that a likely explanation is stack corruption.

This is BTW why I love that rust has opt-in mutability. It feels almost a
little silly sprinking `const` thither and yon when it's only a few select
places that it's actually un-needed.

~~~
brucedawson
Author here. I'm a big believer in adding const everywhere - local variables,
globals, member functions, etc.

Marking globals as const can move them to the read-only segment which has some
modest performance improvements, but in most other cases const doesn't affect
code-gen _at all_. Using const prevents programmers from doing certain things,
and the compiler enforces that. That suggests that the compiler already knows
whether or not programmers are doing those things - it doesn't need a 'const'
hint to tell it. And in those cases where it can't tell, the 'const' hint is
usually not sufficient for it to depend on.

In short, with the exception of moving globals to the read-only segment I am
not aware of _any_ performance improvements from using const.

------
voltagex_
Offtopic sidenote: I wish more open source projects had instructions as good
as [https://www.chromium.org/developers/how-tos/debugging-on-
win...](https://www.chromium.org/developers/how-tos/debugging-on-windows)

