From f8d6b6181eb4d7aeb4b4fc2dda82896fdc7aca3a Mon Sep 17 00:00:00 2001 From: epriestley Date: Tue, 6 Dec 2016 04:16:54 -0800 Subject: [PATCH] Use PhabricatorCachedClassMapQuery when querying object PHID types Summary: Ref T11954. When we query for Conduit tokens, we load the associated objects (users) by PHID. Currently, querying objects by PHID requires us to load every PHIDType class, when we can know which specific classes we actually need (e.g., just `UserPHIDType`, if only user PHIDs are present in the query). Use PhabricatorCachedClassMapQuery to reduce the number of classes we load on this pathway. Test Plan: - Used `ab -n100` to roughly measure a ~5% performance improvement? - This measurement feels a little flimsy but the XHProf profile is cleaner, at least. Reviewers: chad Reviewed By: chad Maniphest Tasks: T11954 Differential Revision: https://secure.phabricator.com/D16997 --- .../phid/query/PhabricatorObjectQuery.php | 24 +++++++++++++------ .../phid/type/PhabricatorPHIDType.php | 15 ++++++++++-- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/applications/phid/query/PhabricatorObjectQuery.php b/src/applications/phid/query/PhabricatorObjectQuery.php index 65e9938311..8364f0210d 100644 --- a/src/applications/phid/query/PhabricatorObjectQuery.php +++ b/src/applications/phid/query/PhabricatorObjectQuery.php @@ -29,11 +29,6 @@ final class PhabricatorObjectQuery $this->namedResults = array(); } - $types = PhabricatorPHIDType::getAllTypes(); - if ($this->types) { - $types = array_select_keys($types, $this->types); - } - $names = array_unique($this->names); $phids = $this->phids; @@ -51,15 +46,30 @@ final class PhabricatorObjectQuery } } - $phids = array_unique($phids); - if ($names) { + $types = PhabricatorPHIDType::getAllTypes(); + if ($this->types) { + $types = array_select_keys($types, $this->types); + } $name_results = $this->loadObjectsByName($types, $names); } else { $name_results = array(); } if ($phids) { + $phids = array_unique($phids); + + $phid_types = array(); + foreach ($phids as $phid) { + $phid_type = phid_get_type($phid); + $phid_types[$phid_type] = $phid_type; + } + + $types = PhabricatorPHIDType::getTypes($phid_types); + if ($this->types) { + $types = array_select_keys($types, $this->types); + } + $phid_results = $this->loadObjectsByPHID($types, $phids); } else { $phid_results = array(); diff --git a/src/applications/phid/type/PhabricatorPHIDType.php b/src/applications/phid/type/PhabricatorPHIDType.php index 99deb84763..2f82dcf169 100644 --- a/src/applications/phid/type/PhabricatorPHIDType.php +++ b/src/applications/phid/type/PhabricatorPHIDType.php @@ -144,10 +144,21 @@ abstract class PhabricatorPHIDType extends Phobject { * @return dict Map of type constants to types. */ final public static function getAllTypes() { + return self::newClassMapQuery() + ->execute(); + } + + final public static function getTypes(array $types) { + return id(new PhabricatorCachedClassMapQuery()) + ->setClassMapQuery(self::newClassMapQuery()) + ->setMapKeyMethod('getTypeConstant') + ->loadClasses($types); + } + + private static function newClassMapQuery() { return id(new PhutilClassMapQuery()) ->setAncestorClass(__CLASS__) - ->setUniqueMethod('getTypeConstant') - ->execute(); + ->setUniqueMethod('getTypeConstant'); }