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

AbstractSet::add() Performance Optimization #68

Open
jmwebservices opened this issue Sep 26, 2020 · 0 comments
Open

AbstractSet::add() Performance Optimization #68

jmwebservices opened this issue Sep 26, 2020 · 0 comments

Comments

@jmwebservices
Copy link

AbstractSet::add() is slow for large collections.

AbstractSet::add() internally invokes AbstractCollection::add() which forwards to AbstractSet::offsetSet(). This flow causes AbstractCollection::contains() to be invoked twice which, for extremely large collections, can significantly impact performance.

Background/problem

Proposal/solution

Alternatives

Additional context

$tests = 10000;

$addCollection       = new Ramsey\Collection\Set( 'int' );
$offsetSetCollection = new Ramsey\Collection\Set( 'int' );

$addTime = microtime( true );
for( $i = 0; $i < $tests; ++$i )
	$addCollection->add( $i );
$addTime = microtime( true ) - $addTime;

$offsetSetTime = microtime( true );
for( $i = 0; $i < $tests; ++$i )
	$offsetSetCollection[] = $i;
$offsetSetTime = microtime( true ) - $offsetSetTime;

// ~98% slower
var_dump( ( $addTime - $offsetSetTime ) / $offsetSetTime * 100 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant