From ea15574226bee467a3ce1b92bc2240c3f4e46913 Mon Sep 17 00:00:00 2001 From: "green-ponies (jgen)" Date: Wed, 11 Jan 2012 15:08:27 -0500 Subject: [PATCH] Changes and tweaks for speed. Because every microsecond counts! :P These changes are based on information from: http://phpbench.com/ http://stackoverflow.com/questions/482202/is-there-a-performance-benefit-single-quote-vs-double-quote-in-php --- core/database.class.php | 12 ++++---- core/imageboard.pack.php | 11 ++++--- core/util.inc.php | 66 +++++++++++++++++++++------------------- 3 files changed, 47 insertions(+), 42 deletions(-) diff --git a/core/database.class.php b/core/database.class.php index 53ed4b4d..6e8a0213 100644 --- a/core/database.class.php +++ b/core/database.class.php @@ -292,17 +292,17 @@ class Database { $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db_proto = $this->db->getAttribute(PDO::ATTR_DRIVER_NAME); - if($db_proto == "mysql") { + if($db_proto === "mysql") { $this->engine = new MySQL(); } - else if($db_proto == "pgsql") { + else if($db_proto === "pgsql") { $this->engine = new PostgreSQL(); } - else if($db_proto == "sqlite") { + else if($db_proto === "sqlite") { $this->engine = new SQLite(); } else { - die("Unknown PDO driver: $db_proto"); + die('Unknown PDO driver: '.$db_proto); } if(isset($cache_dsn) && !empty($cache_dsn)) { @@ -345,8 +345,8 @@ class Database { return $stmt; } catch(PDOException $pdoe) { - print "Message: ".$pdoe->getMessage(); - print "

Error: $query"; + print 'Message: '.$pdoe->getMessage(); + print '

Error: '.$query; exit; } } diff --git a/core/imageboard.pack.php b/core/imageboard.pack.php index 5f110f07..d07a571b 100644 --- a/core/imageboard.pack.php +++ b/core/imageboard.pack.php @@ -194,12 +194,12 @@ class Image { } if(count($tags) == 0) { - $row = $database->get_row("SELECT images.* FROM images WHERE images.id $gtlt {$this->id} ORDER BY images.id $dir LIMIT 1"); + $row = $database->get_row('SELECT images.* FROM images WHERE images.id '.$gtlt.' '.$this->id.' ORDER BY images.id '.$dir.' LIMIT 1'); } else { - $tags[] = "id$gtlt{$this->id}"; + $tags[] = 'id'. $gtlt . $this->id; $querylet = Image::build_search_querylet($tags); - $querylet->append_sql(" ORDER BY images.id $dir LIMIT 1"); + $querylet->append_sql(' ORDER BY images.id '.$dir.' LIMIT 1'); $row = $database->get_row($querylet->sql, $querylet->variables); } @@ -229,14 +229,15 @@ class Image { */ public function get_tag_array() { global $database; - $cached = $database->cache->get("image-{$this->id}-tags"); + $tmp = 'image-'.$this->id.'-tags'; + $cached = $database->cache->get($tmp); if($cached) return $cached; if(!isset($this->tag_array)) { $this->tag_array = $database->get_col("SELECT tag FROM image_tags JOIN tags ON image_tags.tag_id = tags.id WHERE image_id=:id ORDER BY tag", array("id"=>$this->id)); } - $database->cache->set("image-{$this->id}-tags", $this->tag_array); + $database->cache->set($tmp, $this->tag_array); return $this->tag_array; } diff --git a/core/util.inc.php b/core/util.inc.php index 25e020f9..27ad46f2 100644 --- a/core/util.inc.php +++ b/core/util.inc.php @@ -21,6 +21,10 @@ function html_escape($input) { * @retval int */ function int_escape($input) { + /* + Side note, Casting to an integer is FASTER than using intval. + http://hakre.wordpress.com/2010/05/13/php-casting-vs-intval/ + */ return (int)$input; } @@ -56,13 +60,13 @@ function sql_escape($input) { function bool_escape($input) { $input = strtolower($input); return ( - $input == "y" || - $input == "yes" || - $input == "t" || - $input == "true" || - $input == "on" || - $input == 1 || - $input == true + $input === "y" || + $input === "yes" || + $input === "t" || + $input === "true" || + $input === "on" || + $input === 1 || + $input === true ); } @@ -86,7 +90,7 @@ function parse_shorthand_int($limit) { return (int)$limit; } - if(preg_match('/^([\d\.]+)([gmk])?b?$/i', "$limit", $m)) { + if(preg_match('/^([\d\.]+)([gmk])?b?$/i', (string)$limit, $m)) { $value = $m[1]; if (isset($m[2])) { switch(strtolower($m[2])) { @@ -118,7 +122,7 @@ function to_shorthand_int($int) { return sprintf("%.1fKB", $int / 1024); } else { - return "$int"; + return (string)$int; } } @@ -131,7 +135,7 @@ function to_shorthand_int($int) { function autodate($date, $html=true) { $cpu = date('c', strtotime($date)); $hum = date('F j, Y', strtotime($date)); - return ($html ? "" : $hum); + return ($html ? '' : $hum); } @@ -182,17 +186,17 @@ function make_link($page=null, $query=null) { } if(is_null($query)) { - return str_replace("//", "/", "$base/$page"); + return str_replace("//", "/", $base.'/'.$page ); } else { if(strpos($base, "?")) { - return "$base/$page&$query"; + return $base .'/'. $page .'&'. $query; } else if(strpos($query, "#") === 0) { - return "$base/$page$query"; + return $base .'/'. $page . $query; } else { - return "$base/$page?$query"; + return $base .'/'. $page .'?'. $query; } } } @@ -261,14 +265,14 @@ function make_http($link) { function make_form($target, $method="POST", $multipart=False, $form_id="", $onsubmit="") { global $user; $auth = $user->get_auth_html(); - $extra = empty($form_id) ? '' : " id='$form_id'"; + $extra = empty($form_id) ? '' : 'id="'. $form_id .'"'; if($multipart) { $extra .= " enctype='multipart/form-data'"; } if($onsubmit) { - $extra .= " onsubmit='$onsubmit'"; + $extra .= ' onsubmit="'.$onsubmit.'"'; } - return "

$auth"; + return ''.$auth; } /** @@ -278,7 +282,7 @@ function make_form($target, $method="POST", $multipart=False, $form_id="", $onsu function theme_file($filepath) { global $config; $theme = $config->get_string("theme","default"); - return make_link("themes/$theme/$filepath"); + return make_link('themes/'.$theme.'/'.$filepath); } @@ -411,12 +415,12 @@ function _count_execs($db, $sql, $inputarray) { */ function get_theme_object(Extension $class, $fatal=true) { $base = get_class($class); - if(class_exists("Custom{$base}Theme")) { - $class = "Custom{$base}Theme"; + if(class_exists('Custom'.$base.'Theme')) { + $class = 'Custom'.$base.'Theme'; return new $class(); } - elseif ($fatal || class_exists("{$base}Theme")) { - $class = "{$base}Theme"; + elseif ($fatal || class_exists($base.'Theme')) { + $class = $base.'Theme'; return new $class(); } else { return false; @@ -517,14 +521,14 @@ function get_base_href() { $possible_vars = array('SCRIPT_NAME', 'PHP_SELF', 'PATH_INFO', 'ORIG_PATH_INFO'); $ok_var = null; foreach($possible_vars as $var) { - if(substr($_SERVER[$var], -4) == '.php') { + if(substr($_SERVER[$var], -4) === '.php') { $ok_var = $_SERVER[$var]; break; } } assert(!empty($ok_var)); $dir = dirname($ok_var); - if($dir == "/" || $dir == "\\") $dir = ""; + if($dir === "/" || $dir === "\\") $dir = ""; return $dir; } @@ -544,10 +548,10 @@ function warehouse_path($base, $hash, $create=true) { $ab = substr($hash, 0, 2); $cd = substr($hash, 2, 2); if(WH_SPLITS == 2) { - $pa = "$base/$ab/$cd/$hash"; + $pa = $base.'/'.$ab.'/'.$cd.'/'.$hash; } else { - $pa = "$base/$ab/$hash"; + $pa = $base.'/'.$ab.'/'.$hash; } if($create && !file_exists(dirname($pa))) mkdir(dirname($pa), 0755, true); return $pa; @@ -949,7 +953,7 @@ function _get_page_request() { global $config; $args = _get_query_parts(); - if(count($args) == 0 || strlen($args[0]) == 0) { + if( empty($args) || strlen($args[0]) === 0) { $args = explode('/', $config->get_string('front_page')); } @@ -1019,7 +1023,7 @@ function _start_cache() { $_cache_hash = md5($_SERVER["QUERY_STRING"]); $ab = substr($_cache_hash, 0, 2); $cd = substr($_cache_hash, 2, 2); - $_cache_filename = "data/$ab/$cd/$_cache_hash"; + $_cache_filename = 'data/'.$ab.'/'.$cd.'/'.$_cache_hash; if(!file_exists(dirname($_cache_filename))) { mkdir(dirname($_cache_filename), 0750, true); @@ -1038,7 +1042,7 @@ function _start_cache() { } else { header("Content-type: text/html"); - header("Last-Modified: $gmdate_mod"); + header('Last-Modified: '.$gmdate_mod); $zdata = @file_get_contents($_cache_filename); if(CACHE_MEMCACHE) { $_cache_memcache->set($_cache_hash, $zdata, 0, 600); @@ -1086,8 +1090,8 @@ function _end_coverage() { if(!file_exists("data/coverage")) mkdir("data/coverage"); $n = 0; $t = time(); - while(file_exists("data/coverage/$t.$n.log")) $n++; - file_put_contents("data/coverage/$t.$n.log", serialize(xdebug_get_code_coverage())); + while(file_exists('data/coverage/'.$t.'.'.$n.'.log')) $n++; + file_put_contents('data/coverage/'.$t.'.'.$n.'.log', serialize(xdebug_get_code_coverage())); } } ?>