c++: port over go optimisation (efa1672ad5c8fa41550a611217ec3fe239cfd3c6)