Some reminiscences, some memories

Just another boring day
« TurboGears 学习笔记-控制器
使用日立 Feature Tools 软盘镜像解决 Ubuntu 磁盘的 load/unload 问题 »

16

Sep

为什么“is_file比file_exists快N倍”

mikespook 

首先,写本文的原因是看到番茄的这个 post:http://www.tblog.com.cn/archives/675

我没有重新测试,对于番茄的测试,我觉得绝对靠谱。

But why?

于是打开 php 的代码寻找到了 is_file 和 file_exists 的实现。其实原因简单到让人无法相信!

这两个函数是在 /php-5.2.6/ext/standard/filestat.c 中实现的,代码如下:

1 /* {{{ proto bool is_file(string filename)
2 Returns true if file is a regular file */
3 FileFunction(PHP_FN(is_file), FS_IS_FILE)
4 /* }}} */
5
6 /* {{{ proto bool file_exists(string filename)
7 Returns true if filename exists */
8 FileFunction(PHP_FN(file_exists), FS_EXISTS)
9 /* }}} */

其中的 FileFunction 是一个宏:

1 /* another quickie macro to make defining similar functions easier */
2 #define FileFunction(name, funcnum) \
3 void name(INTERNAL_FUNCTION_PARAMETERS) { \
4 zval **filename; \
5 if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &filename) == FAILURE) { \
6 WRONG_PARAM_COUNT; \
7 } \
8 convert_to_string_ex(filename); \
9 php_stat(Z_STRVAL_PP(filename), (php_stat_len) Z_STRLEN_PP(filename), funcnum, return_value TSRMLS_CC); \
10}

到这个宏为止,还看不出来为什么两个函数会有这么大的差别。关键就在 php_stat 这个函数中。不过这个函数实在太长了,不全贴上来。以 5.2.6 版本代码为例。第 770 行:宏 IS_ACCESS_CHECK 对文件先做了一次检查,也就是 if(type == FS_EXISTS)。如果 type 为 FS_EXISTS 就调用宏 VCWD_ACCESS(virtual_access 函数的包装) 对访问权限进行验证。如果能执行到 919 行时,文件一定是存在的,直接返回 True。
而 is_file 的判断就简单得多,前面的验证一路都是 false 就来到了 913 行,这时调用系统宏 S_ISREG 来判断文件是否存在。

对于 virtual_access() 函数,我了解不多,不过猜测可能需要验证文件的用户权限和组权限,所以操作多过 is_file 的 S_ISREG 宏的验证。

This entry was posted on Tuesday, September 16th, 2008 at 08:19 and is filed under PHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

6 Responses to “ 为什么“is_file比file_exists快N倍” ”

  1. # 1 小路 Says:
    September 18th, 2008 at 06:48

    受神的感召~~,发文一篇~
    http://www.mikale.org/html/2008/09/68.html

  2. # 2 mikespook Says:
    September 18th, 2008 at 08:51

    鸟人,不要什么都往我头上扯。我即不懂 tsrm 也不知道 access()。

  3. # 3 小路 Says:
    September 19th, 2008 at 01:41

    不要太生气,已经修改了,哈哈

  4. # 4 mikespook Says:
    September 19th, 2008 at 01:52

    老子很生气,后果很严重……
    没看出来你改了啥。
    呜呼,wordpress 什么时候会有 trac 那种版本控制功能呢?diff 一下就好了……

  5. # 5 小路 Says:
    September 19th, 2008 at 06:03

    教导的是,受到星星大神的启示,偶知道神是怎么批量造人的,男人和女人diff一下,然后patch一下,就可以了

  6. # 6 網站製作學習誌 » [Web] 連結分享 Says:
    October 15th, 2009 at 08:46

    [...] 为什么“is_file比file_exists快N倍” [...]

Leave a Reply

Trackback URI | Comments RSS

 

September 2010
M T W T F S S
« Aug    
 12345
6789101112
13141516171819
20212223242526
27282930  

Recent Comments

  • cbf107 on 我们精通精通
  • mikespook on 我们精通精通
  • == on 我们精通精通
  • 老熊 on 关于Web编程异步模型的白日梦
  • Some reminiscences, some memories » Blog Archive » Akihabara 指南目录 on Akihabara 指南

Tags

乱码 协程 地震 备案 安装 广州 异步 性能 我爱发明 扯淡 招聘 游戏 漏洞 翻译 豆瓣 39.com Adobe akihabara config countdown Demo Flash game engine golang google html5 issue linux Micromedia MongoDB mysql NetBeans nginx NoSQL oracle PHP phpunit Python trac ubuntu xdebug xml xubuntu yield Zend Framework

Blogroll

  • Blog on 27th Floor
  • DBA notes
  • Tim[后端技术]
  • 唐海燕
  • 小众音乐
  • 抚琴居
  • 某人的栖息地
  • 番茄’s Blog
  • 網站製作學習誌
  • 纸老虎传媒

Old friends

  • cbf107

Only

  • 媚惑桃花

OurPNP

  • Biaoest:标的最高级
  • flexsns-官方博客
  • PNP University
  • PNP街坊
  • smallfish 鱼哥的窝子
  • 冰山日志
  • 墙外的光神V5
  • 夜雨's Blog
  • 小冬kobe
  • 小路的奇异世界
  • 广州萌芽工作室
  • 老甘blog
  • 赖勇浩的编程私伙局

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Powered by WordPress
mikespook.com 粤ICP备09065095号