object2array() uses a foreach loop to convert object attributes to array elements. This can be done about 2.4 times faster using a simple cast (array) operator. As an additional benefit, the type check for the argument being an object can be dropped, since casting an array to an array does no harm.

Patch forthcoming.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Chris Johnson’s picture

Version: 4.7.0-beta1 » x.y.z
Status: Active » Reviewed & tested by the community
FileSize
691 bytes

Here is a patch against HEAD, as of December 14, 2005.

Chris Johnson’s picture

Title: object2array() can be made much faster » object2array() and array2object() slower than cast
Status: Reviewed & tested by the community » Active

Ah, just discovered that array2object() was likewise slow. I have patched my 4.7 test system for both routines to just use cast operators (array) and (object) and tested without errors.

Will be rolling new patch combining fixes for both routines, for HEAD/4.7 and 4.6.

Chris Johnson’s picture

Status: Active » Reviewed & tested by the community
FileSize
989 bytes

Combined patch against Head, December 14, 2005.

Chris Johnson’s picture

FileSize
1006 bytes

Combined patch against 4.6 branch (4.6.5, December 12, 2005).

kquinn’s picture

Wouldn't it be preferable to simply remove the object2array function and change all code that calls it to simply cast instead? If the overhead of the function call is at all significant (which I believe to be the case) that seems like it would be a significant improvement.

Thanks.

kquinn’s picture

Regarding my last comment: ditto array2object.

Dries’s picture

Great catch, Chris!

  • I would prefer to see the function calls removed. Shall I wait for the next patch?
  • I don't think I'll commit this to DRUPAL-4-6. It is not a bugfix and fairly untested.
Chris Johnson’s picture

Ok, I will roll a patch for 4.7 only which removes the function calls and changes them all to casts.

Dries’s picture

Thanks!

Chris Johnson’s picture

Patch for core HEAD (as of 0523GMT on 29 December).

This will break all contrib which uses either function, since they have now been deleted.

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed. Wil you add a note to the upgrade guide?

Chris Johnson’s picture

Comment to module developer upgrade handbook posted.

Anonymous’s picture

Status: Fixed » Closed (fixed)