Skip to content

Commit

Permalink
Improve autoloading performance
Browse files Browse the repository at this point in the history
  • Loading branch information
laruence committed Sep 1, 2012
1 parent ff81f79 commit a8c98e1
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 82 deletions.
4 changes: 2 additions & 2 deletions php_yaf.h
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: php_yaf.h 327414 2012-09-01 12:44:40Z laruence $ */
/* $Id: php_yaf.h 327415 2012-09-01 13:58:02Z laruence $ */

#ifndef PHP_YAF_H
#define PHP_YAF_H
Expand Down Expand Up @@ -82,7 +82,7 @@ ZEND_BEGIN_MODULE_GLOBALS(yaf)
char *environ;
char *directory;
char *local_library;
char *local_namespace;
char *local_namespaces;
char *global_library;
char *view_ext;
char *default_module;
Expand Down
4 changes: 4 additions & 0 deletions tests/003.phpt
Expand Up @@ -10,6 +10,9 @@ yaf.lowcase_path=0
ini_set("ap.lowcase_path", FALSE);
$loader = Yaf_Loader::getInstance(dirname(__FILE__), dirname(__FILE__) . "/global");
$loader->registerLocalNamespace("Baidu");
$loader->registerLocalNamespace("Sina");
$loader->registerLocalNamespace(array("Wb", "Inf", NULL, array(), "123"));
var_dump($loader->getLocalNamespace());
var_dump($loader->isLocalName("Baidu_Name"));

try {
Expand All @@ -25,6 +28,7 @@ try {

?>
--EXPECTF--
string(21) "Baidu:Sina:Wb:Inf:123"
bool(true)

Warning: Yaf_Loader::autoload(): Could not find script %s/Baidu/Name.php in %s
Expand Down
1 change: 0 additions & 1 deletion tests/025.phpt
Expand Up @@ -25,7 +25,6 @@ var_dump(Yaf_Loader::getInstance()->isLocalName("Bar_Name"));
--EXPECTF--
Yaf_Loader Object
(
[_local_ns:protected] => :Foo::Bar:Dummy:
[_library:protected] => /tmp
[_global_library:protected] => /php/global/dir
)
Expand Down
10 changes: 5 additions & 5 deletions yaf.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: yaf.c 327414 2012-09-01 12:44:40Z laruence $ */
/* $Id: yaf.c 327415 2012-09-01 13:58:02Z laruence $ */

#ifdef HAVE_CONFIG_H
#include "config.h"
Expand Down Expand Up @@ -54,7 +54,7 @@ zend_function_entry yaf_functions[] = {
};
/* }}} */

/* {{{ PHP_INI_MH(OnUpdateSeparator)
/** {{{ PHP_INI_MH(OnUpdateSeparator)
*/
PHP_INI_MH(OnUpdateSeparator) {
YAF_G(name_separator) = new_value;
Expand Down Expand Up @@ -196,7 +196,7 @@ PHP_RINIT_FUNCTION(yaf)
YAF_G(directory) = NULL;
YAF_G(bootstrap) = NULL;
YAF_G(local_library) = NULL;
YAF_G(local_namespace) = NULL;
YAF_G(local_namespaces) = NULL;
YAF_G(modules) = NULL;
YAF_G(base_uri) = NULL;
#if ((PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4))
Expand All @@ -219,8 +219,8 @@ PHP_RSHUTDOWN_FUNCTION(yaf)
if (YAF_G(local_library)) {
efree(YAF_G(local_library));
}
if (YAF_G(local_namespace)) {
efree(YAF_G(local_namespace));
if (YAF_G(local_namespaces)) {
efree(YAF_G(local_namespaces));
}
if (YAF_G(bootstrap)) {
efree(YAF_G(bootstrap));
Expand Down
38 changes: 17 additions & 21 deletions yaf_application.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: yaf_application.c 327285 2012-08-26 09:12:27Z laruence $ */
/* $Id: yaf_application.c 327415 2012-09-01 13:58:02Z laruence $ */

#ifdef HAVE_CONFIG_H
#include "config.h"
Expand Down Expand Up @@ -167,7 +167,22 @@ static int yaf_application_parse_option(zval *options TSRMLS_DC) {
}
if (zend_hash_find(Z_ARRVAL_PP(ppzval), ZEND_STRS("namespace"), (void **)&ppsval) == SUCCESS
&& Z_TYPE_PP(ppsval) == IS_STRING) {
YAF_G(local_namespace) = estrndup(Z_STRVAL_PP(ppsval), Z_STRLEN_PP(ppsval));
uint i, len;
char *src = Z_STRVAL_PP(ppsval);
if (Z_STRLEN_PP(ppsval)) {
char *target = emalloc(Z_STRLEN_PP(ppsval));
len = 0;
for(i=0; i<Z_STRLEN_PP(ppsval); i++) {
if (src[i] == ',') {
target[len++] = DEFAULT_DIR_SEPARATOR;
} else if (src[i] != ' ') {
target[len++] = src[i];
}
}
target[len] = '\0';
yaf_loader_register_namespace_single(target, len TSRMLS_CC);
efree(target);
}
}
}
}
Expand Down Expand Up @@ -270,8 +285,6 @@ static int yaf_application_parse_option(zval *options TSRMLS_DC) {
} while (0);

if (zend_hash_find(Z_ARRVAL_P(app), ZEND_STRS("system"), (void **)&ppzval) == SUCCESS && Z_TYPE_PP(ppzval) == IS_ARRAY) {
long idx;
uint len;
zval **value;
char *key, name[128];
HashTable *ht = Z_ARRVAL_PP(ppzval);
Expand All @@ -281,7 +294,6 @@ static int yaf_application_parse_option(zval *options TSRMLS_DC) {
zend_hash_move_forward(ht)) {
uint len;
long idx;
char *func;
if (zend_hash_get_current_key_ex(ht, &key, &len, &idx, 0, NULL) != HASH_KEY_IS_STRING) {
continue;
}
Expand Down Expand Up @@ -396,22 +408,6 @@ PHP_METHOD(yaf_application, __construct) {
RETURN_FALSE;
}

if (YAF_G(local_namespace)) {
uint i, len;
char *tmp = YAF_G(local_namespace);
len = strlen(tmp);
if (len) {
for(i=0; i<len; i++) {
if (tmp[i] == ',' || tmp[i] == ' ') {
tmp[i] = DEFAULT_DIR_SEPARATOR;
}
}
yaf_loader_register_namespace_single(loader, tmp, len TSRMLS_CC);
}
efree(YAF_G(local_namespace));
YAF_G(local_namespace) = NULL;
}

zend_update_property_bool(yaf_application_ce, self, ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_RUN), 0 TSRMLS_CC);
zend_update_property_string(yaf_application_ce, self, ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_ENV), YAF_G(environ) TSRMLS_CC);

Expand Down
78 changes: 27 additions & 51 deletions yaf_loader.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: yaf_loader.c 327414 2012-09-01 12:44:40Z laruence $ */
/* $Id: yaf_loader.c 327415 2012-09-01 13:58:02Z laruence $ */

#ifdef HAVE_CONFIG_H
#include "config.h"
Expand Down Expand Up @@ -191,14 +191,12 @@ static int yaf_loader_is_category(char *class, uint class_len, char *category, u
int yaf_loader_is_local_namespace(yaf_loader_t *loader, char *class_name, int len TSRMLS_DC) {
char *pos, *ns, *prefix = NULL;
uint prefix_len = 0;
zval *namespaces = zend_read_property(yaf_loader_ce, loader, ZEND_STRL(YAF_LOADER_PROPERTY_NAME_NAMESPACE), 1 TSRMLS_CC);

if (ZVAL_IS_NULL(namespaces)) {
if (!YAF_G(local_namespaces)) {
return 0;
}

pos = Z_STRVAL_P(namespaces);
ns = Z_STRVAL_P(namespaces);
ns = YAF_G(local_namespaces);

pos = strstr(class_name, "_");
if (pos) {
Expand All @@ -217,10 +215,11 @@ int yaf_loader_is_local_namespace(yaf_loader_t *loader, char *class_name, int le
}

while ((pos = strstr(ns, prefix))) {
if ((pos == ns) && *(pos + prefix_len) == DEFAULT_DIR_SEPARATOR) {
if ((pos == ns) && (*(pos + prefix_len) == DEFAULT_DIR_SEPARATOR || *(pos + prefix_len) == '\0')) {
efree(prefix);
return 1;
} else if (*(pos - 1) == DEFAULT_DIR_SEPARATOR && *(pos + prefix_len) == DEFAULT_DIR_SEPARATOR) {
} else if (*(pos - 1) == DEFAULT_DIR_SEPARATOR
&& (*(pos + prefix_len) == DEFAULT_DIR_SEPARATOR || *(pos + prefix_len) == '\0')) {
efree(prefix);
return 1;
}
Expand Down Expand Up @@ -271,8 +270,6 @@ yaf_loader_t * yaf_loader_instance(yaf_loader_t *this_ptr, char *library_path, c
object_init_ex(instance, yaf_loader_ce);
}

zend_update_property_null(yaf_loader_ce, instance, ZEND_STRL(YAF_LOADER_PROPERTY_NAME_NAMESPACE) TSRMLS_CC);

if (library_path && global_path) {
MAKE_STD_ZVAL(glibrary);
MAKE_STD_ZVAL(library);
Expand Down Expand Up @@ -516,64 +513,41 @@ int yaf_internal_autoload(char *file_name, uint name_len, char **directory TSRML
}
/* }}} */

/** {{{ int yaf_loader_register_namespace_single(yaf_loader_t *loader, char *prefix, uint len TSRMLS_DC)
/** {{{ int yaf_loader_register_namespace_single(char *prefix, uint len TSRMLS_DC)
*/
int yaf_loader_register_namespace_single(yaf_loader_t *loader, char *prefix, uint len TSRMLS_DC) {
zval *namespaces;
smart_str buf = {NULL, 0, 0};

namespaces = zend_read_property(yaf_loader_ce, loader, ZEND_STRL(YAF_LOADER_PROPERTY_NAME_NAMESPACE), 1 TSRMLS_CC);
int yaf_loader_register_namespace_single(char *prefix, uint len TSRMLS_DC) {
char *namespaces;

if (Z_TYPE_P(namespaces) == IS_NULL) {
smart_str_appendc(&buf, DEFAULT_DIR_SEPARATOR);
if (YAF_G(local_namespaces)) {
uint orig_len = strlen(YAF_G(local_namespaces));
YAF_G(local_namespaces) = erealloc(YAF_G(local_namespaces), orig_len + 1 + len + 1);
snprintf(YAF_G(local_namespaces) + orig_len, len + 2, "%c%s", DEFAULT_DIR_SEPARATOR, prefix);
} else {
smart_str_appendl(&buf, Z_STRVAL_P(namespaces), Z_STRLEN_P(namespaces));
efree(Z_STRVAL_P(namespaces));
namespaces = YAF_G(local_namespaces) = emalloc(len + 1 + 1);
snprintf(YAF_G(local_namespaces), len + 2, "%s", prefix);
}

smart_str_appendl(&buf, prefix, len);
smart_str_appendc(&buf, DEFAULT_DIR_SEPARATOR);

ZVAL_STRINGL(namespaces, buf.c, buf.len, 1);

smart_str_free(&buf);

return 1;
}
/* }}} */

/** {{{ int yaf_loader_register_namespace_multi(yaf_loader_t *loader, zval *prefixes TSRMLS_DC)
/** {{{ int yaf_loader_register_namespace_multi(zval *prefixes TSRMLS_DC)
*/
int yaf_loader_register_namespace_multi(yaf_loader_t *loader, zval *prefixes TSRMLS_DC) {
int yaf_loader_register_namespace_multi(zval *prefixes TSRMLS_DC) {
zval *namespaces, **ppzval;
HashTable *ht;
smart_str buf = {0};

namespaces = zend_read_property(yaf_loader_ce, loader, ZEND_STRL(YAF_LOADER_PROPERTY_NAME_NAMESPACE), 1 TSRMLS_CC);

if (Z_TYPE_P(namespaces) == IS_NULL) {
smart_str_appendc(&buf, DEFAULT_DIR_SEPARATOR);
} else {
smart_str_appendl(&buf, Z_STRVAL_P(namespaces), Z_STRLEN_P(namespaces));
}

ht = Z_ARRVAL_P(prefixes);
for(zend_hash_internal_pointer_reset(ht);
zend_hash_has_more_elements(ht) == SUCCESS;
zend_hash_move_forward(ht)) {

if (zend_hash_get_current_data(ht, (void**)&ppzval) == FAILURE) {
continue;
} else {
smart_str_appendl(&buf, Z_STRVAL_PP(ppzval), Z_STRLEN_PP(ppzval));
smart_str_appendc(&buf, DEFAULT_DIR_SEPARATOR);
} else if (IS_STRING == Z_TYPE_PP(ppzval)) {
yaf_loader_register_namespace_single(Z_STRVAL_PP(ppzval), Z_STRLEN_PP(ppzval) TSRMLS_CC);
}
}

ZVAL_STRINGL(namespaces, buf.c, buf.len, 1);

smart_str_free(&buf);

return 1;
}
/* }}} */
Expand Down Expand Up @@ -612,11 +586,11 @@ PHP_METHOD(yaf_loader, registerLocalNamespace) {
}

if (IS_STRING == Z_TYPE_P(namespaces)) {
if (yaf_loader_register_namespace_single(getThis(), Z_STRVAL_P(namespaces), Z_STRLEN_P(namespaces) TSRMLS_CC)) {
if (yaf_loader_register_namespace_single(Z_STRVAL_P(namespaces), Z_STRLEN_P(namespaces) TSRMLS_CC)) {
RETURN_ZVAL(getThis(), 1, 0);
}
} else if (IS_ARRAY == Z_TYPE_P(namespaces)) {
if(yaf_loader_register_namespace_multi(getThis(), namespaces TSRMLS_CC)) {
if(yaf_loader_register_namespace_multi(namespaces TSRMLS_CC)) {
RETURN_ZVAL(getThis(), 1, 0);
}
} else {
Expand All @@ -630,15 +604,18 @@ PHP_METHOD(yaf_loader, registerLocalNamespace) {
/** {{{ proto public Yaf_Loader::getLocalNamespace(void)
*/
PHP_METHOD(yaf_loader, getLocalNamespace) {
zval *namespaces = zend_read_property(yaf_loader_ce, getThis(), ZEND_STRL(YAF_LOADER_PROPERTY_NAME_NAMESPACE), 1 TSRMLS_CC);
RETURN_ZVAL(namespaces, 1, 0);
if (YAF_G(local_namespaces)) {
RETURN_STRING(YAF_G(local_namespaces), 1);
}
RETURN_NULL();
}
/* }}} */

/** {{{ proto public Yaf_Loader::clearLocalNamespace(void)
*/
PHP_METHOD(yaf_loader, clearLocalNamespace) {
zend_update_property_null(yaf_loader_ce, getThis(), ZEND_STRL(YAF_LOADER_PROPERTY_NAME_NAMESPACE) TSRMLS_CC);
efree(YAF_G(local_namespaces));
YAF_G(local_namespaces) = NULL;

RETURN_TRUE;
}
Expand Down Expand Up @@ -995,7 +972,6 @@ YAF_STARTUP_FUNCTION(loader) {
yaf_loader_ce = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
yaf_loader_ce->ce_flags |= ZEND_ACC_FINAL_CLASS;

zend_declare_property_null(yaf_loader_ce, ZEND_STRL(YAF_LOADER_PROPERTY_NAME_NAMESPACE), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(yaf_loader_ce, ZEND_STRL(YAF_LOADER_PROPERTY_NAME_LIBRARY), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(yaf_loader_ce, ZEND_STRL(YAF_LOADER_PROPERTY_NAME_GLOBAL_LIB), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(yaf_loader_ce, ZEND_STRL(YAF_LOADER_PROPERTY_NAME_INSTANCE), ZEND_ACC_PROTECTED|ZEND_ACC_STATIC TSRMLS_CC);
Expand Down
4 changes: 2 additions & 2 deletions yaf_loader.h
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: yaf_loader.h 324659 2012-03-31 09:01:41Z laruence $ */
/* $Id: yaf_loader.h 327415 2012-09-01 13:58:02Z laruence $ */

#ifndef YAF_LOADER_H
#define YAF_LOADER_H
Expand Down Expand Up @@ -90,7 +90,7 @@ int yaf_internal_autoload(char *file_name, uint name_len, char **directory TSRML
int yaf_loader_import(char *path, int len, int use_path TSRMLS_DC);
int yaf_loader_compose(char *path, int len, int use_path TSRMLS_DC);
int yaf_register_autoloader(yaf_loader_t *loader TSRMLS_DC);
int yaf_loader_register_namespace_single(yaf_loader_t *loader, char *prefix, uint len TSRMLS_DC);
int yaf_loader_register_namespace_single(char *prefix, uint len TSRMLS_DC);
yaf_loader_t * yaf_loader_instance(yaf_loader_t *this_ptr, char *library_path, char *global_path TSRMLS_DC);

extern PHPAPI int php_stream_open_for_zend_ex(const char *filename, zend_file_handle *handle, int mode TSRMLS_DC);
Expand Down

0 comments on commit a8c98e1

Please sign in to comment.