Skip to content

Commit

Permalink
Merge pull request #44 from JacobBrownAustin/setRule-performance
Browse files Browse the repository at this point in the history
Speed improvements for `Acl#setRule()`, reducing complexity from `O(N^2)` to `O(N)`
  • Loading branch information
Ocramius authored Feb 1, 2023
2 parents e0332e1 + 2224633 commit 86cecb5
Show file tree
Hide file tree
Showing 6 changed files with 884 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
/phpcs.xml export-ignore
/phpunit.xml.dist export-ignore
/test/ export-ignore
/benchmarks/ export-ignore
/phpbench.json export-ignore
71 changes: 71 additions & 0 deletions benchmarks/AclBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace LaminasBench\PermissionsAcl;

use Laminas\Permissions\Acl\Acl;
use Laminas\Permissions\Acl\Role\GenericRole;

/**
* @Revs(2)
* @Iterations(2)
* @Warmup(1)
*/
class AclBench
{
private const NUM_ROLES_WITHOUT_PARENT = 135;
private const NUM_ROLES_WITH_PARENT = 70;
private const NUM_RESOURCES = 324;
private const NUM_ALLOW_CALLED = 300;
private const NUM_DENY_CALLED = 300;

private Acl $acl;

public function __construct()
{
$this->acl = new Acl();
}

/**
* Benchmarks setting up ACL with roles and resources, and then adding allow/deny rules to them
*/
public function benchAclRolesResourcesAllowDeny(): void
{
$acl = new Acl();
$role = new GenericRole('A');
$acl->addRole($role);
for ($i = 0; $i < self::NUM_ROLES_WITHOUT_PARENT; $i++) {
$acl->addRole((string) $i);
}
for ($i = 0; $i < self::NUM_ROLES_WITH_PARENT; $i++) {
$acl->addRole((string) ($i + self::NUM_ROLES_WITHOUT_PARENT), $i % self::NUM_ROLES_WITHOUT_PARENT);
}
for ($i = 0; $i < self::NUM_RESOURCES; $i++) {
if ($i === 0) {
$parent = null;
} else {
$parent = (string) (intdiv($i * 3, 2) % $i);
}
$acl->addResource((string) $i, $parent);
}
for ($i = 0; $i < self::NUM_ALLOW_CALLED; $i++) {
$role = (string) (intdiv($i * 3, 2) % (self::NUM_ROLES_WITHOUT_PARENT + self::NUM_ROLES_WITH_PARENT));
if ($i %2 ) {
$resource = (string) (intdiv($i * 7, 5) % self::NUM_RESOURCES);
} else {
$resource = null;
}
$acl->allow($role, $resource);
}
for ($i = 0; $i < self::NUM_DENY_CALLED; $i++) {
$role = (string) (intdiv($i * 13, 11) % (self::NUM_ROLES_WITHOUT_PARENT + self::NUM_ROLES_WITH_PARENT));
if ($i %2 ) {
$resource = (string) (intdiv($i * 19, 17) % self::NUM_RESOURCES);
} else {
$resource = null;
}
$acl->allow($role, $resource);
}
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"require-dev": {
"laminas/laminas-coding-standard": "~2.5.0",
"laminas/laminas-servicemanager": "^3.19",
"phpbench/phpbench": "^1.2",
"phpunit/phpunit": "^9.5.26",
"psalm/plugin-phpunit": "^0.18.0",
"vimeo/psalm": "^5.0"
Expand Down
Loading

0 comments on commit 86cecb5

Please sign in to comment.