Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: count() now costs O(1) instad of O(n) #385

Closed
wants to merge 6 commits into from

Conversation

snipershady
Copy link

Now the method count() costs O(1) instad of O(n)

Copy link
Member

@derrabus derrabus left a comment

Choose a reason for hiding this comment

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

Does this change actually solve a problem for you? Did you run any benchmarks?

The obvious blocker for me is the fact that the constructor call gets unnecessarily expensive with your change.

Also since the logic behind the count() method gets more complex with your change, we will probably need more tests around the calculation.

That being said, I'm not sure the added complexity is worth it, tbh.

.gitignore Outdated Show resolved Hide resolved
@@ -65,6 +71,7 @@ class ArrayCollection implements Collection, Selectable, Stringable
public function __construct(array $elements = [])
{
$this->elements = $elements;
$this->size = count($elements);
Copy link
Member

Choose a reason for hiding this comment

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

This increases the cost of the constructor call from O(1) to O(n).

Copy link
Author

Choose a reason for hiding this comment

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

only if you create a collection from another collection.
All new collection costs O(1)

Copy link
Member

Choose a reason for hiding this comment

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

only if you create a collection from another collection.

Which is a totally valid thing to do! This is a blocker!

@snipershady
Copy link
Author

The Static Analysis problem, from PHPstan, wasn't a problem of this PR
I hadn't modified that part of the doc

@greg0ire
Copy link
Member

greg0ire commented Dec 30, 2023

The Static Analysis problem, from PHPstan, wasn't a problem of this PR

Are you willing to bet? 😛 There's an easy way you could make sure of that, you know 😉

@@ -98,7 +104,7 @@ public function first()
* @psalm-template K of array-key
* @psalm-template V
*/
protected function createFrom(array $elements)
protected function createFrom(array $elements): static
Copy link
Member

Choose a reason for hiding this comment

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

Also, the "changes" view makes it obvious which part of your changes is causing the issue.

@@ -65,6 +70,7 @@ class ArrayCollection implements Collection, Selectable, Stringable
public function __construct(array $elements = [])
{
$this->elements = $elements;
$this->size = empty($elements) ? 0 : count($elements);
Copy link
Member

Choose a reason for hiding this comment

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

You don't need to avoid count() on an empty array. That's a cheap operation. counting non-empty arrays preemptively is what troubles me.

@@ -98,7 +104,7 @@ public function first()
* @psalm-template K of array-key
* @psalm-template V
*/
protected function createFrom(array $elements)
protected function createFrom(array $elements): static
Copy link
Member

Choose a reason for hiding this comment

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

Adding a native return type is a breaking change. We must not do that in a minor or bugfix release. Apart from that, this change seems unrelated to the goal of this PR.

@@ -3,4 +3,4 @@
/build
/phpcs.xml
/.phpcs-cache
/.phpunit.result.cache
/.phpunit.result.cache
Copy link
Member

Choose a reason for hiding this comment

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

Please revert. 🙃

@malarzm
Copy link
Member

malarzm commented Jan 1, 2024

FWIW count is O(1) when used on arrays, PHP tracks number of elements in a hash table on its own https://github.com/php/php-src/blob/ea6110f52e40c2b2112364df6d6ca99cb81ab63f/Zend/zend_types.h#L407

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

Successfully merging this pull request may close these issues.

4 participants