[article] CppCon 2014 Notes

cpp,c++,type,template,performance,javascript

08 Sep 2014

Type Deduction and Why You Care

Scott Meyer

  • auto will create a new object, which is a by-value copy.
  • "A brace initializer has no type", (Scott repeated this sentence for thousands of times). That said, sometimes automatic type deduction using brace initializers will fail. Yet, using auto keyword will handle this point. C++ 14 is out. C++ 17 is on the way.
  • auto x1={17} and auto x1 {17} will be different in C++ 17, though now they are the same in most compilers except Visual C++. + One hand,
{
    const int cx = 0;
    auto lambda = [cx]{...};
  }
  // the lambda function will be compiled to following
  class UpToTheCompiler {
    private:
      const int ca; // one of the rare case where compilers will store the value differently
  }

On the other hand,

{
    const int cx = 0;
    auto lambda = [ca = cx]{...};
  }
  // On the other hand, the lambda function will be compiled to following
  class UpToTheCompiler {
    private:
      int ca;
  }
  • Don't use type id.
  • auto uses template deduction, not reference; decltype is a reference type.
  • On decltype
decltype(auto) function {
   return (expression); // reference to local variable; behavior undefined !!!
 }

Emscripten and asm.js: C++'s role in the modern web

Alon Zakai

  • It uses clang C++ as the frontend, also takes LLVM optimizer, libc++, libc++abi. That said, this project supports the C++ syntax whatever clang supports. (I asked what is the known not supported feature, Alon mentioned multi-threading was one of the few.)
  • It also implements SDL and OpenGL etc.
  • The style of the compiled code is asm.js.
  • Challenges: C++ has undefined behaviors. Compilers are permitted to utilize them. Javascript doesn't have undefined behaviors. All behaviors (though sometimes unreasonable, in my opinion) should behave the same across platforms.
  • Javascript applications must ship their own system libraries together.
  • During the development, fuzzing and csmith are used for testing.
  • Right now there are almost no known bugs for Emscripten. (WOW!!!)

How Facebook's HHVM Uses Modern C++ for Fun and Profit (Literally)

Drew Paroski

  • With the help of HHVM, it runs 10 times faster and there is 75% reduction in memeory.
  • PHP -> typed intermidiated representation (typed IR) -> x64 code
  • CPU work: HHVM runtime, PHP interpreter, machine code execution
  • It generates C++ templates code as the target, or as the process of "compilation".
  • Profiling, "Linux's perf is awesome". It keeps track of the full call stack of PHP; it uses sampling to test.
  • It uses rbp-chain to walk the call stack.
  • C++ , -fno-omit-frame-pointer flag may be helpful.
  • Try jemalloc instead of regular mellocs.
  • MADV_HUGEPAGE for TLB hardware.
  • Inclusive time sometimes is needed. (When profiling in most cases, CPU time is exclusive.)
  • Who pollutes the cache is more important than who suffers the cache pollution. (The first question is much harder to answer.)
  • Facebook in-house memcpy implementation rooted here.

Defensive Programming Done Right

John Lakos

  • How to address client misuse as a library developer?
  • Try bsls assert.
  • It controls how to handle undefined behaviors and has a finer CPU utilization control.
  • BSLSASSERTSAFE will kick in if someone only turns it on.
  • There are three different types of assertions, BSLSASSERTSAFE, BSLSASSERT, and BSLSASSERTOPT. Basically, BSLSASSERTSAFE is for inline calls, BSLSASSERT for most general uses, and BSLSASSERTOPT for extreme time critical cases.
  • There are two separate notions of undefined behaviors. Language undefined behaviors are "hard" undefined behaviors; library undefined behaviors are "soft" undefined behaviors, which are breaking the contract.
  • John quoted "Anything can happen: Your program can crash (or not), your computer can explode, your cat can get pregnant, etc." by Marshall Clow. (Marshall Clow added, "Anything can happen. My name missed an L!". John then quickly corrected his slides on the fly.)
  • Sometimes the compiler cannot see the assertions. That said, the compiler can do extreme optimizations resulting in breaking the contract, though by a first look from a programer perspective, the logic of code seems correct.
  • Use BSLSASSERT*IS_ACTIVE macros for release/debug mode.
  • John said, "If you think testing is boring, negative testing is boring-er!". (He was right. I nodded off for this part.)
  • Think for yourself. Why would we test undefined behaviors? How can we test them, if undefined?
  • Defensive programming is fault IN-TOLERANCE.

Efficiency with Algorithms, Performance with Data Structures

Chandler Carruth

  • Making faster programs saves the battery.
  • Not used processors are wasting power.
  • Substring searching example to demonstrate what is fast in practices.
  • Don't pass pointers in for return values.
  • SAY NO to linked list. Every step is a cache miss.
  • Just use vectors.
  • Latency Numbers Every Programmer Should Know
  • std::map is even worse. Unordered map will not save us either.
  • Good hash table design: Local probing, no buckets, small keys, and open addressing. If values are large, one can chase the pointers. That is worth the cost.
  • You have to keep both factors in mind to balance. Sometimes worse is better. Bubble sorting is the fastest when dealing 30 or 40 elements. Cuckoo hashing is asymptotically perfect and has many beautiful properties. Yet, each hit in cuckoo hashtable is a cache miss, which means it is really slow in practices.
  • Rule of thumb: use contiguous dense data structures.
comments powered by Disqus