This is mostly to address comments in D64179. Since the updated code is
quite different, I created this new patch for review.
Unit tests: pass. 62418 tests passed, 0 failed and 845 were skipped.
clang-format: fail. Please format your changes with clang-format by running git-clang-format HEAD^ or applying this patch.