upstream imports r301
adapt optimisations/cleanups from upstream
upstream imports r335