douyujun0152 2017-11-23 12:45
浏览 32
已采纳

如何提高自动加载性能

I Have a platform writen in PHP, and store a class definition in each file in following way:

filename.php = lower(<class name>) + ".php"

I also use autoload functions to get class name and, using a json generated file (by a custom tool that I made), load the especific file that contains the class definition (I tried to use blob before this, but is too slow).

Why a generated file? Because doing a file search on diretory tree in every request to find the corresponding class is IO intensive.

my generated json class map:

{
    "homecontroller": "path/to/dir/homecontroller.php",
    "database": "path/to/another/deep/dir/database.php"
}

Today this process of loading json, decoding and store in a array to use in autoload to find classes takes arround 30~50 milliseconds.

autoload, using the generated json class map:

$_runtimefile = "packages/.runtime-map";
$classmap = json_decode(file_get_contents($_runtimefile));;
function __autoload($classname)
{
    global $classmap;

    $classidentifier = mb_strtolower($classname);
    if(!array_key_exists($classidentifier, $classmap)) return;

    $filename = __DIR__ . "/" . $classmap->{$classidentifier};
    if(!file_exists($filename))
    {
        debug_print_backtrace();
        throw new RuntimeException("File {$filename} not found for class {$classname}");
    }
    require_once($filename);
}

But I want to do this in a better way, in performance point of view. What are the better ways to load/have a class mapping or autoload for large projects and how I can do this?

Additional informations:

  • The project has arround 300 classes.
  • Currently, a simple request takes a minimum of 30~50 milliseconds in localhost.
  • 写回答

2条回答 默认 最新

  • dousikuai5417 2018-05-18 20:17
    关注

    Based on suggestions of Elias Soares and Mark Baker I rewrite my code to use file mapping based on a php array.

    To create and recreate the php automatically I create d a little C# console program that works fine on my project.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看