Problem/Motivation

function user_template_preprocess_default_variables_alter(&$variables) {
  $user = \Drupal::currentUser();

  if (!is_object($user)) {
  }

}

This is_object call is pointless because currentUser() would have thrown an exception otherwise

Proposed resolution

Remove that bit of code.

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#1 2541340-1.patch749 bytesdawehner

Comments

dawehner’s picture

StatusFileSize
new749 bytes
Function Name Calls Diff Calls
Diff%
Incl. Wall
Diff
(microsec)
IWall
Diff%
Incl.
MemUse
Diff
(bytes)
IMemUse
Diff%
Incl.
PeakMemUse
Diff
(bytes)
IPeakMemUse
Diff%
Current Function
user_template_preprocess_default_variables_alter 0 0.0% -357 -25.6% -33,712 -296.1% -61,848 -560.2%
Exclusive Metrics Diff for Current Function -341 -95.5% -288 -0.9% 1,664 2.7%
Parent function
Drupal\Core\Extension\ModuleHandler::alter 0 N/A% -357 -100.0% -33,712 -100.0% -61,848 -100.0%
Child functions
Drupal\Core\Session\AccountProxy::hasPermission 0 0.0% -15 -4.2% 0 0.0% 1,944 3.1%
Drupal::currentUser 0 0.0% -1 -0.3% 0 0.0% 0 0.0%
is_object -1 -100.0% 0 0.0% -33,424 -99.1% -65,840 -106.5%
Drupal\Core\Session\AccountProxy::isAuthenticated 0 0.0% 0 0.0% 0 0.0% 384 0.6%
dawehner’s picture

Status: Active » Needs review

.

dawehner’s picture

Mh some microbenchmarking doesn't show that.

<?php

$user = \Drupal::currentUser();

$a = microtime();

for ($i = 0; $i < 100000; $i++) {
  $user = \Drupal::currentUser();
  if (!is_object($user)) {
    return;
  }
}

$b = microtime();

for ($i = 0; $i < 100000; $i++) {
  $user = \Drupal::currentUser();
}

$c = microtime();

print ($b -$a) . "\n";
print ($c -$b) . "\n";

0.07211
0.064923
dawehner’s picture

Priority: Normal » Minor
Issue tags: -Performance

Ignore that profiling, it still simplifies the code.

jibran’s picture

Title: user_template_preprocess_default_variables_alter calls is_object for no reason » Remove dead code from user module
Category: Bug report » Task
Priority: Minor » Normal
Status: Needs review » Reviewed & tested by the community

Beta eval: non disruptive removing dead code.

Let's classify it as normal task.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Removing dead code is permitted in beta. Committed 6366f77 and pushed to 8.0.x. Thanks!

  • alexpott committed 6366f77 on 8.0.x
    Issue #2541340 by dawehner: Remove dead code from user module
    

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.