Pomax / PHP-Font-Parser

This is a PHP code library for parsing TTF/OTF fonts from file.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Way to enhance speed for string input

9tontruck opened this issue · comments

When processing multiple glyphs, the processing speed is so slow and ,in my case, server's memory exceeds limit so cannot get desired result properly or quickly.
I hope this is a solution for this problem.

foreach ($text as $letter) {
    while($glyph) {
        if($glyph->letter == $letter) {
            echo $glyph->d;
        }
        $glyph = nextGlygh();
    }
}

Above algorithm makes server soooo busy because searching element in stdClass and looping over time allocates too much memory in the system. I don't know why the php creators wrote like this, but this is the nature of php just like you said.

$count = strlen($text);
$glyph = firstGlyph();
while($glyph && $count>0) {
    foreach ($text as $letter) {
        if($glyph->letter == $letter) {
            $cache[$letter] = $glyph->d;
            $count--;
        }
    }
    $glyph = nextGlygh();
}

foreach ($text as $letter) {
    echo $cache[$letter];
}

This algorithm is way faster and does not allocate wasteful memory. Hope this helps to enhance your library.

As in the other bug, I don't see that behaviour when testing with very long strings. It's very fast, and only uses 30MB memory, so please look at your own code first, starting with what you're doing in nextGlyph().

As far as the parser is concerned: this is already what the code does. There is already a cache inside the font object that doesn't search for a glyph a second/third/etc time if you search for it; it simply immediately returns the letter's data. Using microtime(true) timing on the code to see how long each letter takes, shows that looking for a specific letter will be considerably slower the first time compared to any subsequent times you look it up on the same Font object, because the first time it has to do the lookup, and subsequence lookup are just cache-returns.