
How HHVM Uses Modern C++ for Fun and Profit (2014) [video] - StylifyYourBlog
https://www.youtube.com/watch?v=XqK8Yuoq4ig
======
TazeTSchnitzel
I don't like how C was completely ignored when talking about languages with
comparable performance. :p

The Zend Engine 3 (aka the stock PHP 7 interpreter) is, according to one HHVM
developer, 'embarassingly competitive on the test suites', and it's written in
C.

/rant

C++ is probably much nicer to maintain, though. C ends up being macro hell.

~~~
Demiurge
He did not ignore C, he said C++ is much easier to maintain and lets you use
friendly features when it's not performance expensive. They use template as
much as possible over macros, C++ debuggers, lambdas, ref counting.

~~~
TazeTSchnitzel
I haven't watched the last 10 minutes yet, but there's no mention of C in the
talk before that. The only comparison is to Assembly.

~~~
Demiurge
And I can't really rewatch the first half hour, but I distinctly remember he
mentioned C at least two times at the begging, slightly before and during "Why
C++" part. The examples of template usage also imply things not possible in C.

~~~
ANTSANTS
I watched it last night and can confirm that C was not explicitly mentioned
once.

~~~
Demiurge
Wow, I guess I must have filled in some blanks... when he said 'other
languages' or something...

------
simula67
I remember someone from Facebook saying that ( can't find the link ATM ) when
HPHP was being built there was a competing project to trans-compile PHP to
Java. In the end they compared the performance and found that the C++ was
faster and went with it. Is it true ?

If it is, I wonder how they feel now that they have given up speed
improvements and built a VM in search of other advantages. At least if they
had chosen the JVM they could have taken advantage of interoperability that
JVM provides with other languages to convert at least part of their code base
to JVM languages, thereby possibly improving maintainability.

~~~
TazeTSchnitzel
Are you possibly referring to Quercus?
[https://en.wikipedia.org/wiki/Resin_%28software%29#Quercus](https://en.wikipedia.org/wiki/Resin_%28software%29#Quercus)

------
TazeTSchnitzel
I was expecting a talk about how cool C++11 features are. It was actually a
talk about how C++ lets you be super close-to-the-metal and do ridiculous
performance hacks. Interesting.

------
peterashford
Nobody uses C++ for fun. Unless they're a masochist.

~~~
dmpk2k
I do, and I'm not a masochist. I use it to write small games on my own time.

I used to loathe C++, but the C++11/14 dialect makes things much more
pleasant. There's still a lot of details you need to worry about, but
crucially these are details that other languages rarely give you a choice
about. The only real alternative was C, but I'm tired of reinventing wheels
and C++ has the STL. C also doesn't have the same support for value types
which C++ does, and value types are _really_ nice to use.

~~~
harry8
Value types, as opposed to polymorphic types, ie pointers (including
references)?

struct IntForFoo { int value; };

when you use it you write a function that takes an IntForFoo as one of its
arguments and you get type safety over other ints. Gently snarl at anyone who
does a .value outside such a function. Include a constructor function, an
assignment function... Remember that that typedefs just lie to you about the
type, they don't change it for type checking...

If you need to generate code, do it, macros and templates are just really
crappy ways of generating code; you can do a lot better when you need to. But
how often do you _really_ need to and how often are you worshipping at the
false idol of "must be general so can be used for 100 years" when that's
really not the purpose of the code?

Polymorphism and templates are overrated by C++ designers you don't actually
need them and they encourage some awful designs.

~~~
dmpk2k
One of the problems with structs in C, is that you must explicitly call the
destructor function if you have non-memory resources. In C++, a scope's
closing } always takes care of that for you.

As for the generated code bit, I assume you're referring to the STL. Writing
my own data structures is explicitly what I don't want to do anymore. STL has
it? Great, I use it and move on.

~~~
harry8
Sure use it if you understand it, ie vector where you reserve the memory up
front to avoid future allocations and pointer/iterator invalidations or you
don't care in which case use a friendlier or more powerful language maybe?

Writing datastructures is much of what programming is, I know that's not what
you meant and you use the basics of STL to build the real datastructure you
want, but you care about performance and with STL it's not targeted at your
case so when you get beyond prototype you have to rip it out anyway.

Automatic destructor calls on leaving scope is the nicest part of c++ - shame
it interacts with exceptions so nastily that nobody understands how it works.
"Exception safe code" yeah, right, not in c++.

Generated code - you can use macros, you can use templates. Or you can use
anything that generates text and does what you want, more nicely than that.

~~~
maxlybbert
> Sure use it if you understand it, ie vector where you reserve the memory up
> front to avoid future allocations and pointer/iterator invalidations or you
> don't care in which case use a friendlier or more powerful language maybe?

std::vector implements one of the simplest data structures in computer
science. If somebody doesn't understand the difference between, say, an array
(however you want to call it) and a linked list, I don't want them on my team.

> Writing datastructures is much of what programming is, I know that's not
> what you meant and you use the basics of STL to build the real datastructure
> you want, but you care about performance and with STL it's not targeted at
> your case so when you get beyond prototype you have to rip it out anyway.

What are you talking about? More than a decade ago, there was a lot of
interest in what kind of penalty there was in using C++ compared to C. There
were several benchmarks run and papers written, including one from Alexander
Stepanov (main contributor to the STL) that became an appendix to the standard
(
[http://std.dkuug.dk/JTC1/SC22/WG21/docs/PDTR18015.pdf](http://std.dkuug.dk/JTC1/SC22/WG21/docs/PDTR18015.pdf)
). The conclusion from the various experiments was that C++ performance was on
par with C, and C++ often provided opportunities to optimize more than C did
(e.g., passing a pointer to a function in C to the more common C++ practice of
passing a functor with an inlineable operator()). The STL relies on those
features that allow for a very efficient implementation. In fact, the main
complaint I've seen is that the STL struck the wrong balance of preferring
efficiency over generality ( [http://www.open-
std.org/JTC1/SC22/wg21/docs/papers/2007/n227...](http://www.open-
std.org/JTC1/SC22/wg21/docs/papers/2007/n2271.html) , [http://www.open-
std.org/jtc1/sc22/wg21/docs/papers/2005/n185...](http://www.open-
std.org/jtc1/sc22/wg21/docs/papers/2005/n1850.pdf) ).

> Automatic destructor calls on leaving scope is the nicest part of c++ -
> shame it interacts with exceptions so nastily that nobody understands how it
> works. "Exception safe code" yeah, right, not in c++.

A decade ago, there was a period of maybe six months where people thought it
might be impossible to write exception safe code in C++. It was solved, and
the solution was to use destructors. You may have heard of RAII (
[http://stroustrup.com/except.pdf](http://stroustrup.com/except.pdf) ).
Alexandrescu even made it incredibly simple to do (
[http://www.drdobbs.com/cpp/generic-change-the-way-you-
write-...](http://www.drdobbs.com/cpp/generic-change-the-way-you-write-
excepti/184403758) ).

> Generated code - you can use macros, you can use templates. Or you can use
> anything that generates text and does what you want, more nicely than that.

So I have a choice between using templates, which provide syntax checking and
type checking, or generating text in an ad-hoc way? And you actually recommend
monkeying with text generation?

For what it's worth, I do like C. But when I work in C, I need to rely on
something like Apache's APR or GNU's glib to provide me with useful data
structures. While those have been ported to all architectures I care about,
there's something to be said for having data structures in the core library.

I like C11. I like using _Generic in macros. I like variable argument macros.
But if I'm working on a project where I need variable argument macros, I'm
much more likely to pick C++ than to try to convince myself that I'm
generating correct source code through macros.

~~~
yongjik
> std::vector implements one of the simplest data structures in computer
> science.

It may "implement" one of the simplest DS, but this particular implementation
is anything but trivial to use. You can get a pointer to an element in it, and
keep using the vector, and the pointer will point to the correct element...
until you push_back() one too many element, it resizes, and then suddenly your
pointer is invalid.

But if you reserve() the right amount of space, you can keep it valid! If you
are very careful. But then you don't want to call reserve() too often because
it may actually turn your O(N) algorithm into O(N^2)! (Basically, calling
reserve() N times on O(N) length vector takes O(N^2) time, while doing
push_back() N times is guaranteed to be O(N).)

Sure, std::vector is useful, and I use it all the time, but it is anything but
simple. It's C++ after all. :)

~~~
dmpk2k
You have the similar problems if you build your own resizeable arrays in C,
unless you resort to an extra level of indirection (e.g. an array of pointers,
or pointers to array chunks).

std::vector has a series of gotchas, but any generic resizeable array which
has similar performance will have similar gotchas; std::vector wasn't made
that way just for giggles.

