1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-28 17:52:43 +01:00
phorge-phorge/src/applications/project/storage/PhabricatorProject.php

87 lines
2.4 KiB
PHP
Raw Normal View History

<?php
/*
* Copyright 2012 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
final class PhabricatorProject extends PhabricatorProjectDAO {
protected $name;
protected $phid;
protected $status = PhabricatorProjectStatus::STATUS_ACTIVE;
protected $authorPHID;
protected $subprojectPHIDs = array();
protected $phrictionSlug;
private $subprojectsNeedUpdate;
private $memberPHIDs;
public function getConfiguration() {
return array(
self::CONFIG_AUX_PHID => true,
self::CONFIG_SERIALIZATION => array(
'subprojectPHIDs' => self::SERIALIZATION_JSON,
),
) + parent::getConfiguration();
}
public function generatePHID() {
return PhabricatorPHID::generateNewPHID(
PhabricatorPHIDConstants::PHID_TYPE_PROJ);
}
Improve performance of project list view Summary: D477 added functionality to the project list view but had a couple of performance issues that I missed in review, because it took the query count for the page from around 3 to as many as 300, including up to 100 heavyweight search index queries. This fixes the two simple N+1 query problems. This general pattern of data access often occurs: COUNTEREXAMPLE $cats = load_cats(); foreach ($cats as $cat) { $cats_hats = load_hats_for_cat($cat); // ... } But this issues "N+1" queries, i.e. if you load 100 cats you issue 101 queries. It is faster to group the queries instead: $cats = load_cats(); $hats = load_all_hats_for_these_cats($cats); foreach ($cats as $cat) { $cats_hats = $hats[$cat->getID()]; } MySQL can execute one query which returns all the results much faster than 100 queries which return one result, especially if the database is not local (i.e., over the network). However, this doesn't save a ton of time. The bigger issue is that I didn't have the right keys on the relationship tables in the search engine. This adds them, and reduces the search engine lookup cost from 25-80ms (for secure.phabricator.com) down to 1-3ms. I still probably want to get this out of the loop at some point but it's okay for now and the page loads in a few ms rather than taking more than a second. Test Plan: Used "services" tab, "xhprof" and "EXPLAIN" to analyze page performance. I measured these changes: - Query count: 1 + (3 * N projects) -> 3 + (N projects) (e.g., 301 -> 103) - Total time spent querying, ignoring search indexes: 40ms (local.aprhont.com) -> 20ms (local.aphront.com) - Cost for search index query: 25-80ms (secure.phabricator.com) -> 1-3ms Reviewed By: cadamo Reviewers: cadamo, aran, jungejason, tuomaspelkonen CC: aran, cadamo, epriestley Differential Revision: 485
2011-06-21 01:32:44 +02:00
public function loadProfile() {
$profile = id(new PhabricatorProjectProfile())->loadOneWhere(
'projectPHID = %s',
$this->getPHID());
return $profile;
}
public function attachMemberPHIDs(array $phids) {
$this->memberPHIDs = $phids;
return $this;
}
public function getMemberPHIDs() {
if ($this->memberPHIDs === null) {
throw new Exception("Call attachMemberPHIDs() first!");
}
return $this->memberPHIDs;
}
public function loadMemberPHIDs() {
if (!$this->getPHID()) {
return array();
}
return PhabricatorEdgeQuery::loadDestinationPHIDs(
$this->getPHID(),
PhabricatorEdgeConfig::TYPE_PROJ_MEMBER);
}
public function setPhrictionSlug($slug) {
// NOTE: We're doing a little magic here and stripping out '/' so that
// project pages always appear at top level under projects/ even if the
// display name is "Hack / Slash" or similar (it will become
// 'hack_slash' instead of 'hack/slash').
$slug = str_replace('/', ' ', $slug);
$slug = PhabricatorSlug::normalize($slug);
$this->phrictionSlug = $slug;
return $this;
}
}