Search code examples
phpbrowscap

Browscap.ini PHP startup delay


I observed that enabling the browscap.ini entry in the php-cli.ini file increases the startup time for php

[browscap]
browscap = /etc/browscap.ini

time php -r 'echo "Hello\n";'
Hello

real    0m1.709s
user    0m1.358s
sys 0m0.348s

****VS the below****

[browscap]
;browscap = /etc/browscap.ini

time php -r 'echo "Hello\n";'
Hello

real    0m0.041s
user    0m0.029s
sys 0m0.011s

Now I know that php looks up the browscap.ini file when a function like get_browser() is called. I can understand the lag if such a function is used.

I don't think php would be loading the browscap.ini(which can be large) data into memory on each startup. But why the huge delay in php startup ?

Maybe it tries to check if the browscap.ini file exists on every startup or some other validation ? Could not find anything in the php docs.

So why the huge difference in php startup times ?


Solution

  • PHP loads its entire configuration (including browscap.ini) on startup, which in CLI is every time you invoke php. It doesn't matter if you don't call get_browser() in your script, it doesn't even matter if you give php no script to interpret at all; the browscap will get loaded anyway if the config file says so. The reason the difference in startup time is noticeable, is because typical browscap.ini is usually relatively large.

    Here's a bunch of results from my machine using different sized browscap files and simple php -v:

    • no browscap: 0m0.030s
    • lite_php_browscap.ini (222 KB): 0m0.040s
    • php_browscap.ini (16,301 KB): 0m0.640s
    • full_php_browscap.ini (33,714 KB): 0m1.290s