marcw / twig-heroicons

Heroicons <3 Twig
MIT License
11 stars 5 forks source link

Make icons cacheable inside opcache #3

Open alexander-schranz opened 2 years ago

alexander-schranz commented 2 years ago

Currently file_get_contents is everytime used to load the icon. This means it need to load and read the icon from the filesystem. This can be an overhead. I would suggest to create a script which will create a PHP class per icon e.g.:

namespace MarcW\Heroicons\Icons\Solid

use MarcW\Heroicons\IconInterface;

class AcademicCap implements IconInterface {
    public static function getIcon(): string
    {
         return '<svg ... >';
    }
}

Then we can get the icon with:

return call_user_func_array(['MarcW\\Heroicons\\Icons\\' . $solidOrOutline . '\\' . $icon], ['getIcon']);

The advantage of this is that the icon are cacheable inside the opcache of php and don't need to be loaded everytime when being accessed. The svg I would keep in the repository, the classes could be generated over a script inside this repository and so the classes are easy to update if you update the resources directory with new icons. What do you think?

marcw commented 2 years ago

That would mean generating 450+ classes, which seems counter-intuitive to me.

marcw commented 2 years ago

Is there any other projects doing that? Or is there any data points that would help taking a decision? I have no strong opinion on this really, but I want to be sure we have a real problem at hand.

alexander-schranz commented 2 years ago

As example I can give you Azure App Services which has a really slow IO on this kind of files and there its better this can live somewhere else then read always from the harddisk and opcache is mostly the simpliest thing without any additional cache layer. And I would create a class per icon so only the files opcache which are really used. I would mark the generated classes as @internal so they will not be used directly and we can change them the way we want in the future.