Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Zend Db Query Builder Optimisation #6541

Closed

Conversation

RichardHeelin
Copy link
Contributor

Found that the query builder was spending a long time building queries with large arrays in the where clause. After a little bit of profiling noticed that line 111 was been called a lot. array_search is an expensive lookup, especially as the return value wasn't been used beyond checking it wasn't false, so replaced with a less expensive isset.

we only care that the name has a position, not what is is, so don't waste resources looking it up
@@ -107,8 +107,7 @@ public function offsetSet($name, $value, $errata = null)
}
} elseif (is_string($name)) {
// is a string:
$currentNames = array_keys($this->data);
$position = array_search($name, $currentNames, true);
$position = isset($this->data[$name]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isset and array_search have different return types - are you sure this is equivalent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$position is only checked to see if it is explicity false (see line 117), which isset will return if it's not set.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the null value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can only hit this code if $name is a string, at which point we are only checking if it's already been used or not. Null name values are handled differently as before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore me, I know what you mean, yeah will need to change this to array_key_exists.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null values :-)

$data = ['foo' => null];
var_dump(isset($data['foo']));

http://3v4l.org/ZdNWD

isset will return false if value is null which is not what we want. `array_key_exists` will still be faster than `array_search`
@RichardHeelin
Copy link
Contributor Author

Switched to using array_key_exists to account for null values been passed, will still be quicker than the original array_search.

@Ocramius Ocramius added this to the 2.3.2 milestone Aug 7, 2014
weierophinney added a commit that referenced this pull request Aug 7, 2014
@weierophinney
Copy link
Member

Cherry-picked to master for release to 2.3.2, and merged to both master and develop.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants