Skip to content

Commit

Permalink
Merge pull request #2003 from sjinks/obj-opt
Browse files Browse the repository at this point in the history
Optimize/annotate kernel/object.h
  • Loading branch information
Phalcon committed Feb 10, 2014
2 parents 38f03d4 + 477c367 commit dd1e4a6
Show file tree
Hide file tree
Showing 20 changed files with 418 additions and 476 deletions.
22 changes: 9 additions & 13 deletions ext/annotations/adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,26 +158,22 @@ PHP_METHOD(Phalcon_Annotations_Adapter, get){
* Get the class name if it's an object
*/
if (Z_TYPE_P(class_name) == IS_OBJECT) {
const zend_class_entry *ce = Z_OBJCE_P(class_name);
PHALCON_INIT_VAR(real_class_name);
phalcon_get_class(real_class_name, class_name, 0 TSRMLS_CC);
ZVAL_STRINGL(real_class_name, ce->name, ce->name_length, !IS_INTERNED(ce->name));
} else {
PHALCON_CPY_WRT(real_class_name, class_name);
}

PHALCON_OBS_VAR(annotations);
phalcon_read_property_this(&annotations, this_ptr, SL("_annotations"), PH_NOISY_CC);
if (Z_TYPE_P(annotations) == IS_ARRAY) {
if (phalcon_array_isset(annotations, real_class_name)) {
PHALCON_OBS_VAR(class_annotations);
phalcon_array_fetch(&class_annotations, annotations, real_class_name, PH_NOISY);
RETURN_CCTOR(class_annotations);
}
annotations = phalcon_fetch_nproperty_this(this_ptr, SL("_annotations"), PH_NOISY_CC);
if (phalcon_array_isset_fetch(&class_annotations, annotations, real_class_name)) {
RETURN_CTOR(class_annotations);
}

/**
* Try to read the annotations from the adapter
*/
PHALCON_INIT_NVAR(class_annotations);
PHALCON_INIT_VAR(class_annotations);
phalcon_call_method_p1(class_annotations, this_ptr, "read", real_class_name);
if (Z_TYPE_P(class_annotations) == IS_NULL) {

Expand All @@ -203,7 +199,7 @@ PHP_METHOD(Phalcon_Annotations_Adapter, get){
}
}

RETURN_CCTOR(class_annotations);
RETURN_CTOR(class_annotations);
}

/**
Expand Down Expand Up @@ -279,7 +275,7 @@ PHP_METHOD(Phalcon_Annotations_Adapter, getMethod){
PHALCON_GET_HVALUE(method);

if (PHALCON_IS_EQUAL(name, method_name)) {
RETURN_CCTOR(method);
RETURN_CTOR(method);
}

zend_hash_move_forward_ex(ah0, &hp0);
Expand Down Expand Up @@ -370,7 +366,7 @@ PHP_METHOD(Phalcon_Annotations_Adapter, getProperty){
PHALCON_GET_HVALUE(property);

if (PHALCON_IS_EQUAL(name, property_name)) {
RETURN_CCTOR(property);
RETURN_CTOR(property);
}

zend_hash_move_forward_ex(ah0, &hp0);
Expand Down
71 changes: 30 additions & 41 deletions ext/annotations/collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,49 +118,38 @@ PHALCON_INIT_CLASS(Phalcon_Annotations_Collection){
*/
PHP_METHOD(Phalcon_Annotations_Collection, __construct){

zval *reflection_data = NULL, *annotations, *annotation_data = NULL;
zval *reflection_data = NULL, *annotations, **annotation_data;
zval *annotation = NULL;
HashTable *ah0;
HashPosition hp0;
zval **hd;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 0, 1, &reflection_data);
phalcon_fetch_params(0, 0, 1, &reflection_data);

if (!reflection_data) {
reflection_data = PHALCON_GLOBAL(z_null);
if (!reflection_data || Z_TYPE_P(reflection_data) == IS_NULL) {
return;
}

if (Z_TYPE_P(reflection_data) != IS_NULL) {
if (Z_TYPE_P(reflection_data) != IS_ARRAY) {
PHALCON_THROW_EXCEPTION_STR(phalcon_annotations_exception_ce, "Reflection data must be an array");
return;
}

if (Z_TYPE_P(reflection_data) != IS_ARRAY) {
PHALCON_THROW_EXCEPTION_STR(phalcon_annotations_exception_ce, "Reflection data must be an array");
return;
}
if (Z_TYPE_P(reflection_data) == IS_ARRAY) {

PHALCON_INIT_VAR(annotations);
array_init(annotations);

phalcon_is_iterable(reflection_data, &ah0, &hp0, 0, 0);

while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {

PHALCON_GET_HVALUE(annotation_data);

PHALCON_INIT_NVAR(annotation);
object_init_ex(annotation, phalcon_annotations_annotation_ce);
phalcon_call_method_p1_noret(annotation, "__construct", annotation_data);

phalcon_array_append(&annotations, annotation, 0);

zend_hash_move_forward_ex(ah0, &hp0);
}

phalcon_update_property_this(this_ptr, SL("_annotations"), annotations TSRMLS_CC);

PHALCON_MM_GROW();

PHALCON_INIT_VAR(annotations);
array_init_size(annotations, zend_hash_num_elements(Z_ARRVAL_P(reflection_data)));

for (
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(reflection_data), &hp0);
zend_hash_get_current_data_ex(Z_ARRVAL_P(reflection_data), (void**)&annotation_data, &hp0) == SUCCESS;
zend_hash_move_forward_ex(Z_ARRVAL_P(reflection_data), &hp0)
) {
PHALCON_INIT_NVAR(annotation);
object_init_ex(annotation, phalcon_annotations_annotation_ce);
phalcon_call_method_p1_noret(annotation, "__construct", *annotation_data);
phalcon_array_append(&annotations, annotation, 0);
}


phalcon_update_property_this(this_ptr, SL("_annotations"), annotations TSRMLS_CC);
PHALCON_MM_RESTORE();
}

Expand All @@ -173,12 +162,12 @@ PHP_METHOD(Phalcon_Annotations_Collection, count){

zval *annotations;

PHALCON_MM_GROW();
annotations = phalcon_fetch_nproperty_this(this_ptr, SL("_annotations"), PH_NOISY_CC);
if (Z_TYPE_P(annotations) == IS_ARRAY) {
RETURN_LONG(zend_hash_num_elements(Z_ARRVAL_P(annotations)));
}

PHALCON_OBS_VAR(annotations);
phalcon_read_property_this(&annotations, this_ptr, SL("_annotations"), PH_NOISY_CC);
phalcon_fast_count(return_value, annotations TSRMLS_CC);
RETURN_MM();
RETURN_LONG(0);
}

/**
Expand Down
13 changes: 9 additions & 4 deletions ext/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include "debug.h"
#include "exception.h"
#include "version.h"

#include <ext/standard/php_string.h>
Expand Down Expand Up @@ -448,8 +449,9 @@ PHP_METHOD(Phalcon_Debug, _getArrayDump){
continue;
}
if (Z_TYPE_P(v) == IS_OBJECT) {
zend_class_entry *ce = Z_OBJCE_P(v);
PHALCON_INIT_NVAR(class_name);
phalcon_get_class(class_name, v, 0 TSRMLS_CC);
ZVAL_STRINGL(class_name, ce->name, ce->name_length, !IS_INTERNED(ce->name));

PHALCON_INIT_NVAR(var_dump);
PHALCON_CONCAT_SVSVS(var_dump, "[", k, "] =&gt; Object(", class_name, ")");
Expand Down Expand Up @@ -534,9 +536,10 @@ PHP_METHOD(Phalcon_Debug, _getVarDump){
* If the variable is an object print its class name
*/
if (Z_TYPE_P(variable) == IS_OBJECT) {
const zend_class_entry *ce = Z_OBJCE_P(variable);

PHALCON_INIT_VAR(class_name);
phalcon_get_class(class_name, variable, 0 TSRMLS_CC);
ZVAL_STRINGL(class_name, ce->name, ce->name_length, !IS_INTERNED(ce->name));

/**
* Try to check for a 'dump' method, this surely produces a better printable
Expand Down Expand Up @@ -1051,11 +1054,12 @@ PHP_METHOD(Phalcon_Debug, onUncaughtException){
char* link_format;
zend_bool ini_exists = 1;
zval z_link_format = zval_used_for_init;

zend_class_entry *ce;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 0, &exception);
PHALCON_VERIFY_CLASS_EX(exception, zend_exception_get_default(TSRMLS_C), phalcon_exception_ce, 1);

/**
* Cancel the output buffer if active
Expand All @@ -1081,7 +1085,8 @@ PHP_METHOD(Phalcon_Debug, onUncaughtException){
zend_update_static_property_bool(phalcon_debug_ce, SL("_isActive"), 1 TSRMLS_CC);

PHALCON_INIT_VAR(class_name);
phalcon_get_class(class_name, exception, 0 TSRMLS_CC);
ce = Z_OBJCE_P(exception);
ZVAL_STRINGL(class_name, ce->name, ce->name_length, !IS_INTERNED(ce->name));

PHALCON_INIT_NVAR(message);
phalcon_call_method(message, exception, "getmessage");
Expand Down
2 changes: 1 addition & 1 deletion ext/di/service.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ PHP_METHOD(Phalcon_DI_Service, resolve){
/**
* String definitions can be class names without implicit parameters
*/
if (phalcon_class_exists(definition, 1 TSRMLS_CC)) {
if (phalcon_class_exists(Z_STRVAL_P(definition), Z_STRLEN_P(definition), 1 TSRMLS_CC)) {
if (Z_TYPE_P(parameters) == IS_ARRAY) {
if (phalcon_create_instance_params(instance, definition, parameters TSRMLS_CC) == FAILURE) {
RETURN_MM();
Expand Down
3 changes: 2 additions & 1 deletion ext/dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,8 @@ PHP_METHOD(Phalcon_Dispatcher, dispatch){
* DI doesn't have a service with that name, try to load it using an autoloader
*/
PHALCON_INIT_NVAR(has_service);
ZVAL_LONG(has_service, phalcon_class_exists(handler_class, 1 TSRMLS_CC));
assert(Z_TYPE_P(handler_class) == IS_STRING);
ZVAL_LONG(has_service, phalcon_class_exists(Z_STRVAL_P(handler_class), Z_STRLEN_P(handler_class), 1 TSRMLS_CC));
}

/**
Expand Down
43 changes: 22 additions & 21 deletions ext/events/manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "events/event.h"
#include "events/exception.h"

#include <Zend/zend_closures.h>
#include <ext/spl/spl_heap.h>

#include "kernel/main.h"
Expand Down Expand Up @@ -313,22 +314,22 @@ PHP_METHOD(Phalcon_Events_Manager, fireQueue){
HashTable *ah0;
HashPosition hp0;
zval **hd;
zend_class_entry **weakref_ce;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 2, 0, &queue, &event);

if (unlikely(Z_TYPE_P(queue) != IS_ARRAY)) {
if (Z_TYPE_P(queue) != IS_OBJECT) {
PHALCON_THROW_EXCEPTION_STR(phalcon_events_exception_ce, "The SplPriorityQueue is not valid");
return;
}
PHALCON_VERIFY_CLASS_EX(event, spl_ce_SplPriorityQueue, phalcon_events_exception_ce, 1);
}
if (unlikely(Z_TYPE_P(event) != IS_OBJECT)) {
PHALCON_THROW_EXCEPTION_STR(phalcon_events_exception_ce, "The event is not valid");
return;

PHALCON_VERIFY_CLASS_EX(event, phalcon_events_event_ce, phalcon_events_exception_ce, 1);

if (FAILURE == zend_lookup_class_ex(SL("WeakRef") ZLK_NULL_CC, 0, &weakref_ce TSRMLS_CC)) {
weakref_ce = NULL;
}

PHALCON_INIT_VAR(status);

PHALCON_INIT_VAR(arguments);
Expand Down Expand Up @@ -404,7 +405,7 @@ PHP_METHOD(Phalcon_Events_Manager, fireQueue){
/**
* Check if the event is a weak reference.
*/
if (phalcon_is_instance_of(handler_embeded, SL("WeakRef") TSRMLS_CC)) {
if (weakref_ce && instanceof_function(Z_OBJCE_P(handler_embeded), *weakref_ce TSRMLS_CC)) {
/**
* Checks whether the object referenced still exists.
*/
Expand All @@ -423,24 +424,24 @@ PHP_METHOD(Phalcon_Events_Manager, fireQueue){
}

} else {
PHALCON_INIT_NVAR(handler);
PHALCON_CPY_WRT(handler, handler_embeded);
}

/**
* Check if the event is a closure
*/
if (phalcon_is_instance_of(handler, SL("Closure") TSRMLS_CC)) {
assert(Z_TYPE_P(handler) == IS_OBJECT);
if (instanceof_function(Z_OBJCE_P(handler), zend_ce_closure TSRMLS_CC)) {

/**
* Create the closure arguments
*/
if (Z_TYPE_P(arguments) == IS_NULL) {
PHALCON_INIT_NVAR(arguments);
array_init_size(arguments, 3);
phalcon_array_append(&arguments, event, PH_SEPARATE);
phalcon_array_append(&arguments, source, PH_SEPARATE);
phalcon_array_append(&arguments, data, PH_SEPARATE);
phalcon_array_append(&arguments, event, 0);
phalcon_array_append(&arguments, source, 0);
phalcon_array_append(&arguments, data, 0);
}

/**
Expand Down Expand Up @@ -522,7 +523,7 @@ PHP_METHOD(Phalcon_Events_Manager, fireQueue){
/**
* Check if the event is a weak reference.
*/
if (phalcon_is_instance_of(handler_embeded, SL("WeakRef") TSRMLS_CC)) {
if (weakref_ce && instanceof_function(Z_OBJCE_P(handler_embeded), *weakref_ce TSRMLS_CC)) {
/**
* Checks whether the object referenced still exists.
*/
Expand All @@ -538,24 +539,24 @@ PHP_METHOD(Phalcon_Events_Manager, fireQueue){
}

} else {
PHALCON_INIT_NVAR(handler);
PHALCON_CPY_WRT(handler, handler_embeded);
}

/**
* Check if the event is a closure
*/
if (phalcon_is_instance_of(handler, SL("Closure") TSRMLS_CC)) {
assert(Z_TYPE_P(handler) == IS_OBJECT);
if (instanceof_function(Z_OBJCE_P(handler), zend_ce_closure TSRMLS_CC)) {

/**
* Create the closure arguments
*/
if (Z_TYPE_P(arguments) == IS_NULL) {
PHALCON_INIT_NVAR(arguments);
array_init_size(arguments, 3);
phalcon_array_append(&arguments, event, PH_SEPARATE);
phalcon_array_append(&arguments, source, PH_SEPARATE);
phalcon_array_append(&arguments, data, PH_SEPARATE);
phalcon_array_append(&arguments, event, 0);
phalcon_array_append(&arguments, source, 0);
phalcon_array_append(&arguments, data, 0);
}

/**
Expand Down
11 changes: 5 additions & 6 deletions ext/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "filterinterface.h"
#include "filter/exception.h"

#include <Zend/zend_closures.h>

#include "kernel/main.h"
#include "kernel/memory.h"
#include "kernel/exception.h"
Expand Down Expand Up @@ -224,18 +226,15 @@ PHP_METHOD(Phalcon_Filter, _sanitize){

PHALCON_OBS_VAR(filters);
phalcon_read_property_this(&filters, this_ptr, SL("_filters"), PH_NOISY_CC);
if (phalcon_array_isset(filters, filter)) {

PHALCON_OBS_VAR(filter_object);
phalcon_array_fetch(&filter_object, filters, filter, PH_NOISY);
if (phalcon_array_isset_fetch(&filter_object, filters, filter) && Z_TYPE_P(filter_object) == IS_OBJECT) {

/**
* If the filter is a closure we call it in the PHP userland
*/
if (phalcon_is_instance_of(filter_object, SL("Closure") TSRMLS_CC)) {
if (instanceof_function(Z_OBJCE_P(filter_object), zend_ce_closure TSRMLS_CC)) {
PHALCON_INIT_VAR(arguments);
array_init_size(arguments, 1);
phalcon_array_append(&arguments, value, PH_SEPARATE);
phalcon_array_append(&arguments, value, 0);
PHALCON_CALL_USER_FUNC_ARRAY(return_value, filter_object, arguments);
RETURN_MM();
}
Expand Down
8 changes: 2 additions & 6 deletions ext/image/adapter/imagick.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,14 @@ PHALCON_INIT_CLASS(Phalcon_Image_Adapter_Imagick){
*/
PHP_METHOD(Phalcon_Image_Adapter_Imagick, check){

zval class_name;
zval *version;

INIT_ZVAL(class_name);
ZVAL_STRING(&class_name, "imagick", 0);

if (!phalcon_class_exists(&class_name, 0 TSRMLS_CC)) {
if (!phalcon_class_exists(SL("imagick"), 0 TSRMLS_CC)) {
PHALCON_THROW_EXCEPTION_STR(phalcon_image_exception_ce, "Imagick is not installed, or the extension is not loaded");
return;
}

ALLOC_INIT_ZVAL(version);
MAKE_STD_ZVAL(version);
if (!zend_get_constant_ex(SL("imagick::IMAGICK_EXTNUM"), version, NULL, ZEND_FETCH_CLASS_SILENT TSRMLS_CC)) {
zend_update_static_property_long(phalcon_image_adapter_imagick_ce, SL("_version"), 0 TSRMLS_CC);
}
Expand Down
Loading

0 comments on commit dd1e4a6

Please sign in to comment.