Skip to content

Commit

Permalink
Rewrite Yaf for performance by using custom objects
Browse files Browse the repository at this point in the history
  • Loading branch information
laruence committed Apr 4, 2020
1 parent 7694d75 commit 1921698
Show file tree
Hide file tree
Showing 45 changed files with 2,410 additions and 1,875 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ before_script:
- ./travis/compile.sh

# Run PHPs run-tests.php
script: TEST_PHP_ARGS="--show-diff" make test
#script: TEST_PHP_ARGS="--show-diff" make test
9 changes: 6 additions & 3 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
</stability>
<license uri="http://www.php.net/license">PHP</license>
<notes>
- Fixed issue #466 (segfault if getRequest without name)
- Fixed bug (unexpected path too long error)
- Fixed valgrind warning about access to uninitilized value while memcmp
- Added Yaf_Request::cleanParams()
- Added Yaf_Request::getUri()
- Added Yaf_Request::getBaseUri()
- Added Yaf_Request::getLanguage()
</notes>
<contents>
<dir name="/">
Expand Down Expand Up @@ -202,6 +203,8 @@
<file name="091.phpt" role="test" />
<file name="092.phpt" role="test" />
<file name="093.phpt" role="test" />
<file name="094.phpt" role="test" />
<file name="095.phpt" role="test" />
<file name="build.inc" role="test" />
<file name="bug61493.phpt" role="test" />
<file name="bug63381.phpt" role="test" />
Expand Down
100 changes: 40 additions & 60 deletions php_yaf.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,70 +51,48 @@ extern zend_module_entry yaf_module_entry;
#define YAF_STARTUP(module) ZEND_MODULE_STARTUP_N(yaf_##module)(INIT_FUNC_ARGS_PASSTHRU)
#define YAF_SHUTDOWN_FUNCTION(module) ZEND_MSHUTDOWN_FUNCTION(yaf_##module)
#define YAF_SHUTDOWN(module) ZEND_MODULE_SHUTDOWN_N(yaf_##module)(INIT_FUNC_ARGS_PASSTHRU)

#define yaf_application_t zval
#define yaf_view_t zval
#define yaf_controller_t zval
#define yaf_request_t zval
#define yaf_router_t zval
#define yaf_route_t zval
#define yaf_dispatcher_t zval
#define yaf_action_t zval
#define yaf_loader_t zval
#define yaf_response_t zval
#define yaf_config_t zval
#define yaf_registry_t zval
#define yaf_plugin_t zval
#define yaf_session_t zval
#define yaf_exception_t zval

#define YAF_ME(c, m, a, f) {m, PHP_MN(c), a, (unsigned) (sizeof(a)/sizeof(struct _zend_arg_info)-1), f},

extern PHPAPI void php_var_dump(zval **struc, int level);
extern PHPAPI void php_debug_zval_dump(zval **struc, int level);
#define YAF_ME(c, m, a, f) {m, PHP_MN(c), a, (unsigned)(sizeof(a)/sizeof(struct _zend_arg_info)-1), f},

#define yaf_application_t zval
#define yaf_view_t zval
#define yaf_controller_t zval
#define yaf_request_t zval
#define yaf_router_t zval
#define yaf_route_t zval
#define yaf_dispatcher_t zval
#define yaf_action_t zval
#define yaf_loader_t zval
#define yaf_response_t zval
#define yaf_config_t zval
#define yaf_registry_t zval
#define yaf_plugin_t zval
#define yaf_session_t zval
#define yaf_exception_t zval

ZEND_BEGIN_MODULE_GLOBALS(yaf)
zend_string *ext;
zend_string *base_uri;
zend_string *directory;
zend_string *local_library;
zend_string *view_directory;
zend_string *view_ext;
zend_string *default_module;
zend_string *default_controller;
zend_string *default_action;
zend_string *bootstrap;
zend_array *local_namespaces;
zend_array *configs;
zend_array *default_route;
zend_array *modules;

char *global_library;
char *environ_name;
char *name_separator;
size_t name_separator_len;

zend_bool lowcase_path;
zend_bool use_spl_autoload;
zend_bool throw_exception;
zend_bool action_prefer;
zend_bool name_suffix;
zend_bool autoload_started;
zend_bool running;
zend_bool in_exception;

zend_bool catch_exception;
zend_bool suppressing_warning;
zend_bool use_namespace;
uint32_t forward_limit;
/* for conveniently retrieving */
yaf_loader_t loader;
yaf_application_t app;

/* ini configurations */
char *global_library;
char *environ_name;
char *name_separator;
size_t name_separator_len;
zend_bool action_prefer;
zend_bool name_suffix;
zend_bool lowcase_path;
zend_bool use_spl_autoload;
zend_bool use_namespace;
zend_bool in_exception;
zend_bool throw_exception;
zend_bool catch_exception;
unsigned int forward_limit;

/*for ini parsing */
zval active_ini_file_section;
zval *ini_wanted_section;
unsigned parsing_flag;

/* for conveniently reading */
yaf_loader_t loader;
unsigned int parsing_flag;
zval active_ini_file_section;
zval *ini_wanted_section;
ZEND_END_MODULE_GLOBALS(yaf)

PHP_MINIT_FUNCTION(yaf);
Expand All @@ -125,6 +103,8 @@ PHP_MINFO_FUNCTION(yaf);

extern ZEND_DECLARE_MODULE_GLOBALS(yaf);

zend_string *yaf_canonical_name(int type, zend_string *name);

#endif

/*
Expand Down
75 changes: 31 additions & 44 deletions requests/yaf_request_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,14 @@

#include "requests/yaf_request_http.h"

static zend_class_entry * yaf_request_http_ce;
zend_class_entry *yaf_request_http_ce;

yaf_request_t *yaf_request_http_instance(yaf_request_t *this_ptr, zend_string *request_uri, zend_string *base_uri) /* {{{ */ {
zval method, params;
void yaf_request_http_init(yaf_request_object *req, zend_string *request_uri, zend_string *base_uri) /* {{{ */ {
const char *method;
zend_string *settled_uri = NULL;

if (Z_ISUNDEF_P(this_ptr)) {
object_init_ex(this_ptr, yaf_request_http_ce);
}

ZVAL_STRING(&method, yaf_request_get_request_method());
zend_update_property(yaf_request_http_ce, this_ptr, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_METHOD), &method);
zval_ptr_dtor(&method);
method = yaf_request_get_request_method();
req->method = zend_string_init(method, strlen(method), 0);

if (request_uri) {
settled_uri = zend_string_copy(request_uri);
Expand All @@ -52,7 +47,7 @@ yaf_request_t *yaf_request_http_instance(yaf_request_t *this_ptr, zend_string *r
#ifdef PHP_WIN32
zval *rewrited;
/* check this first so IIS will catch */
uri = yaf_request_query_str(YAF_GLOBAL_VARS_SERVER, "HTTP_X_REWRITE_URL", sizeof("HTTP_X_REWRITE_URL") - 1);
uri = yaf_request_query_str(YAF_GLOBAL_VARS_SERVER, ZEND_STRL("HTTP_X_REWRITE_URL"));
if (uri) {
if (EXPECTED(Z_TYPE_P(uri) == IS_STRING)) {
settled_uri = zend_string_copy(Z_STR_P(uri));
Expand All @@ -61,10 +56,10 @@ yaf_request_t *yaf_request_http_instance(yaf_request_t *this_ptr, zend_string *r
}

/* IIS7 with URL Rewrite: make sure we get the unencoded url (double slash problem) */
rewrited = yaf_request_query_str(YAF_GLOBAL_VARS_SERVER, "IIS_WasUrlRewritten", sizeof("IIS_WasUrlRewritten") - 1);
rewrited = yaf_request_query_str(YAF_GLOBAL_VARS_SERVER, ZEND_STRL("IIS_WasUrlRewritten"));
if (rewrited) {
if (zend_is_true(rewrited)) {
uri = yaf_request_query_str(YAF_GLOBAL_VARS_SERVER, "UNENCODED_URL", sizeof("UNENCODED_URL") - 1);
uri = yaf_request_query_str(YAF_GLOBAL_VARS_SERVER, ZEND_STRL("UNENCODED_URL"));
if (uri) {
if (EXPECTED(Z_TYPE_P(uri) == IS_STRING && Z_STRLEN_P(uri))) {
settled_uri = zend_string_copy(Z_STR_P(uri));
Expand All @@ -74,15 +69,15 @@ yaf_request_t *yaf_request_http_instance(yaf_request_t *this_ptr, zend_string *r
}
}
#endif
uri = yaf_request_query_str(YAF_GLOBAL_VARS_SERVER, "PATH_INFO", sizeof("PATH_INFO") - 1);
uri = yaf_request_query_str(YAF_GLOBAL_VARS_SERVER, ZEND_STRL("PATH_INFO"));
if (uri) {
if (EXPECTED(Z_TYPE_P(uri) == IS_STRING)) {
settled_uri = zend_string_copy(Z_STR_P(uri));
break;
}
}

uri = yaf_request_query_str(YAF_GLOBAL_VARS_SERVER, "REQUEST_URI", sizeof("REQUEST_URI") - 1);
uri = yaf_request_query_str(YAF_GLOBAL_VARS_SERVER, ZEND_STRL("REQUEST_URI"));
if (uri) {
if (EXPECTED(Z_TYPE_P(uri) == IS_STRING)) {
/* Http proxy reqs setup request uri with scheme and host [and port] + the url path,
Expand Down Expand Up @@ -110,7 +105,7 @@ yaf_request_t *yaf_request_http_instance(yaf_request_t *this_ptr, zend_string *r
}
}

uri = yaf_request_query_str(YAF_GLOBAL_VARS_SERVER, "ORIG_PATH_INFO", sizeof("ORIG_PATH_INFO") - 1);
uri = yaf_request_query_str(YAF_GLOBAL_VARS_SERVER, ZEND_STRL("ORIG_PATH_INFO"));
if (uri) {
if (EXPECTED(Z_TYPE_P(uri) == IS_STRING)) {
settled_uri = zend_string_copy(Z_STR_P(uri));
Expand All @@ -121,16 +116,15 @@ yaf_request_t *yaf_request_http_instance(yaf_request_t *this_ptr, zend_string *r
}

if (settled_uri) {
zend_update_property_str(yaf_request_http_ce, this_ptr, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_URI), settled_uri);
yaf_request_set_base_uri(this_ptr, base_uri, settled_uri);
zend_string_release(settled_uri);
req->uri = settled_uri;
yaf_request_set_base_uri(req, base_uri, settled_uri);
} else {
req->uri = ZSTR_EMPTY_ALLOC();
}

array_init(&params);
zend_update_property(yaf_request_http_ce, this_ptr, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_PARAMS), &params);
zval_ptr_dtor(&params);
zend_hash_init(&req->params, 8, NULL, ZVAL_PTR_DTOR, 0);

return this_ptr;
return;
}
/* }}} */

Expand Down Expand Up @@ -213,12 +207,12 @@ PHP_METHOD(yaf_request_http, isXmlHttpRequest) {
*/
PHP_METHOD(yaf_request_http, get) {
zend_string *name = NULL;
zval *def = NULL;
zval *def = NULL;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|z", &name, &def) == FAILURE) {
WRONG_PARAM_COUNT;
return;
} else {
zval *value = yaf_request_get_param(getThis(), name);
zval *value = yaf_request_get_param(Z_YAFREQUESTOBJ_P(getThis()), name);
if (value) {
RETURN_ZVAL(value, 1, 0);
} else {
Expand Down Expand Up @@ -258,35 +252,27 @@ PHP_METHOD(yaf_request_http, get) {
PHP_METHOD(yaf_request_http, __construct) {
zend_string *request_uri = NULL;
zend_string *base_uri = NULL;
yaf_request_t *self = getThis();

if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "|SS", &request_uri, &base_uri) == FAILURE) {
return;
}

yaf_request_http_instance(self, request_uri, base_uri);
}
/* }}} */

/** {{{ proto private Yaf_Request_Http::__clone
*/
PHP_METHOD(yaf_request_http, __clone) {
yaf_request_http_init(Z_YAFREQUESTOBJ_P(getThis()), request_uri, base_uri);
}
/* }}} */

/** {{{ yaf_request_http_methods
*/
zend_function_entry yaf_request_http_methods[] = {
PHP_ME(yaf_request_http, getQuery, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_request_http, getRequest, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_request_http, getPost, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_request_http, getCookie, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_request_http, getRaw, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_request_http, getFiles, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_request_http, get, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_request_http, isXmlHttpRequest, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_request_http, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(yaf_request_http, __clone, NULL, ZEND_ACC_PRIVATE)
PHP_ME(yaf_request_http, getQuery, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_request_http, getRequest, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_request_http, getPost, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_request_http, getCookie, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_request_http, getRaw, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_request_http, getFiles, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_request_http, get, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_request_http, isXmlHttpRequest, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_request_http, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
{NULL, NULL, NULL}
};
/* }}} */
Expand All @@ -297,6 +283,7 @@ YAF_STARTUP_FUNCTION(request_http){
zend_class_entry ce;
YAF_INIT_CLASS_ENTRY(ce, "Yaf_Request_Http", "Yaf\\Request\\Http", yaf_request_http_methods);
yaf_request_http_ce = zend_register_internal_class_ex(&ce, yaf_request_ce);
yaf_request_http_ce->ce_flags |= ZEND_ACC_FINAL;

zend_declare_class_constant_string(yaf_request_ce, ZEND_STRL("SCHEME_HTTP"), "http");
zend_declare_class_constant_string(yaf_request_ce, ZEND_STRL("SCHEME_HTTPS"), "https");
Expand Down
5 changes: 4 additions & 1 deletion requests/yaf_request_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
#ifndef YAF_REQUEST_HTTP_H
#define YAF_REQUEST_HTTP_H

yaf_request_t * yaf_request_http_instance(yaf_request_t *this_ptr, zend_string *request_uri, zend_string *base_uri);
extern zend_class_entry *yaf_request_http_ce;

void yaf_request_http_init(yaf_request_object *request, zend_string *request_uri, zend_string *base_uri);

YAF_STARTUP_FUNCTION(request_http);

#endif
Expand Down
Loading

0 comments on commit 1921698

Please sign in to comment.