Optimize and simplify `encode_unicode_codepoints`