Removes dependency on ruby 2.7 and isn't much slower (cherry picked from commit 786579c0b8ee5311af04feb036d04b78efd4b5b2)