@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Micro-optimize 'shades' color list

Summary:
Add a new method in PHUITagView with a micro-optimization which stores
these damn ~12 colors, to avoid ~250 calls of `pht()` from the homepage
feeds, especially if there were many mentioned projects.
So this array is calculated only once per request.

P.S.

A new method was created rather than modifying the existing one, so this
is a minimal change, causing less conflicts on Phorge forks with custom
colors. Also, this new method allows to easily implement future workflows
which do not need a cache, for example, showing a demo page with colors
in each supported language, etc.

Also avoid an unnecessary call of the `idx()` function, where it was
used like a ternary operator. So, we just use that operator (T16134).
This saves resources even more, since the ternary operator is faster,
and since the underlying method may be called 20+ times / request
(at least true in my homepage).

Also do not use the method `getShades()` in the examples page
as micro-optimization: we avoid 12*2 calls to 'ucwords()', just to
obtain the same label available from this new cached method.

Also add a TODO about a duplicated color list (T16240).

Closes T16239
Ref T16134

Test Plan:
Assign the "Orange" color to a normal project: no regressions.

Visit the homepage with many projects in the feed: no regressions.

Visit the example page and see absolutely no changes, especially about 'Shades':

http://phorge.localhost/uiexample/view/PHUITagExample/

Visit the project search page and search by "Orange": no regressions.

http://phorge.localhost/project/

Introduce a silly `phlog('lol')` inside `PHUITagView#getShadeMap()` and enjoy the
new cache: you see the 'lol' in the log only once and not ~20+ times.
Therefore, you are saving planet resources. Greta Thunberg may love Phorge more.

Reviewers: O1 Blessed Committers, mainframe98

Reviewed By: O1 Blessed Committers, mainframe98

Subscribers: tobiaswiese, Matthew, Cigaryno

Maniphest Tasks: T16239, T16134

Differential Revision: https://we.phorge.it/D26292

+58 -11
+1 -5
src/applications/phid/PhabricatorObjectHandle.php
··· 60 60 } 61 61 62 62 public function setTagColor($color) { 63 - static $colors; 64 - if (!$colors) { 65 - $colors = array_fuse(array_keys(PHUITagView::getShadeMap())); 66 - } 67 - 63 + $colors = PHUITagView::getShadeMapCached(); 68 64 if (isset($colors[$color])) { 69 65 $this->tagColor = $color; 70 66 }
+10
src/applications/project/icon/PhabricatorProjectIconSet.php
··· 385 385 return idx($map, $color_key); 386 386 } 387 387 388 + /** 389 + * Get the default value for the config `project.colors`. 390 + * 391 + * The result is simple enough to be easily JSON-encoded later. 392 + * @return array Array of colors. Each color is an associative array with 393 + * these keys: 'key' (color key) and 'name' (translated label). 394 + */ 388 395 public static function getDefaultColorMap() { 396 + // In the future, generate this list using PHUITagView::getShadeMapCached(), 397 + // instead of re-defining these colors also here. 398 + // https://we.phorge.it/T16240 389 399 return array( 390 400 array( 391 401 'key' => PHUITagView::COLOR_RED,
+8 -4
src/applications/uiexample/examples/PHUITagExample.php
··· 158 158 ->appendChild($icons) 159 159 ->addPadding(PHUI::PADDING_LARGE); 160 160 161 - $shades = PHUITagView::getShades(); 161 + // Build the 'Shades' example box. 162 + // Avoid 'getShades()' since it only gets keys. 163 + // Use 'getShadeMapCached()' which also gives labels. 164 + $shades = PHUITagView::getShadeMapCached(); 162 165 $tags = array(); 163 - foreach ($shades as $shade) { 166 + foreach ($shades as $shade => $shade_label) { 164 167 $tags[] = id(new PHUITagView()) 165 168 ->setType(PHUITagView::TYPE_SHADE) 166 169 ->setColor($shade) 167 170 ->setIcon('fa-tags') 168 - ->setName(ucwords($shade)) 171 + ->setName($shade_label) 169 172 ->setHref('#'); 170 173 $tags[] = hsprintf('&nbsp;'); 171 174 $tags[] = id(new PHUITagView()) ··· 173 176 ->setColor($shade) 174 177 ->setSlimShady(true) 175 178 ->setIcon('fa-tags') 176 - ->setName(ucwords($shade)) 179 + ->setName($shade_label) 177 180 ->setHref('#'); 178 181 $tags[] = hsprintf('<br /><br />'); 179 182 } 180 183 184 + // Content for the 'Shades' example box. 181 185 $content4 = id(new PHUIBoxView()) 182 186 ->appendChild($tags) 183 187 ->addPadding(PHUI::PADDING_LARGE);
+39 -2
src/view/phui/PHUITagView.php
··· 296 296 ); 297 297 } 298 298 299 + /** 300 + * Get all the color keys used for shades. 301 + * 302 + * @return array<string> Color keys for shades. 303 + */ 299 304 public static function getShades() { 300 - return array_keys(self::getShadeMap()); 305 + return array_keys(self::getShadeMapCached()); 306 + } 307 + 308 + /** 309 + * Efficiently get all the colors used for shades. 310 + * 311 + * Generally this is used to render project colors. 312 + * The result is cached. See @{method:getShadeMap} if you don't need a cache. 313 + * @return array<string, string> Map of color keys and translated label. 314 + */ 315 + public static function getShadeMapCached(): array { 316 + static $map = null; 317 + if (!$map) { 318 + $map = self::getShadeMap(); 319 + } 320 + return $map; 301 321 } 302 322 323 + /** 324 + * Get all the colors used for shades. 325 + * 326 + * Generally this is used to render project colors. 327 + * The result is not cached. 328 + * See @{method:getShadeMapCached} if you need a cache. 329 + * @return array<string, string> Map of color keys and translated label. 330 + */ 303 331 public static function getShadeMap() { 304 332 return array( 305 333 self::COLOR_RED => pht('Red'), ··· 316 344 ); 317 345 } 318 346 347 + /** 348 + * Get the shade color name, if available, or just its key. 349 + * 350 + * @param string $shade Shade color key. 351 + * @return string Shade color label (if available), or the input key. 352 + */ 319 353 public static function getShadeName($shade) { 320 - return idx(self::getShadeMap(), $shade, $shade); 354 + $shades = self::getShadeMapCached(); 355 + // Get the color label, or the color key if missing. 356 + // Avoid idx() to reduce overhead. 357 + return $shades[$shade] ?? $shade; 321 358 } 322 359 323 360 public static function getOutlines() {