Skip to content

Fix iconv_strlen() being hundreds of times too slow on glibc - #25707

Open
lacatoire wants to merge 2 commits into
php:masterfrom
lacatoire:fix/iconv-strlen-buffer-size-master
Open

lacatoire wants to merge 2 commits into
php:masterfrom
lacatoire:fix/iconv-strlen-buffer-size-master

Conversation

@lacatoire

Copy link
Copy Markdown
Member

iconv_strlen() converted the string to UCS-4 through an 8-byte buffer, so iconv() was called once per two characters, and each of those calls is slow on glibc. iconv_substr() always calls it first, and iconv_strpos() does with a negative offset, so they paid that cost too. Use a 4 KiB buffer.

To reproduce

<?php
$s = str_repeat("é", 1000000);
$t = microtime(true);
iconv_strlen($s, "UTF-8");
printf("%.2fs\n", microtime(true) - $t);

PHP 8.4.28-dev, glibc 2.36

Before: 10.47s

After: 0.03s

@iliaal iliaal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use str_repeat('é', 2046) . 'abc' for the two iconv_substr() checks? That still crosses two full buffers, and the -3 case now checks for abc. The million-character negative offset walks almost the whole string one character at a time. This test took 17.6 s in release CI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants