Skip to content

Commit

Permalink
- performance require_once should be called only once for shared plug…
Browse files Browse the repository at this point in the history
…ins #280
  • Loading branch information
uwetews committed Sep 1, 2016
1 parent 5580857 commit e1d27d6
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 41 deletions.
3 changes: 3 additions & 0 deletions change_log.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
===== 3.1.31-dev ===== (xx.xx.xx)
01.09.2016
- performance require_once should be called only once for shared plugins https://github.com/smarty-php/smarty/issues/280

26.08.2016
- bugfix change of 23.08.2016 failed on linux when use_include_path = true

Expand Down
2 changes: 1 addition & 1 deletion libs/Smarty.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = '3.1.31-dev/7';
const SMARTY_VERSION = '3.1.31-dev/8';

/**
* define variable scopes
Expand Down
4 changes: 3 additions & 1 deletion libs/plugins/block.textformat.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ function smarty_block_textformat($params, $content, $template, &$repeat)
if (is_null($content)) {
return;
}
if (Smarty::$_MBSTRING && !is_callable('smarty_mb_wordwrap')) {
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_wordwrap.php');
}

$style = null;
$indent = 0;
Expand Down Expand Up @@ -92,7 +95,6 @@ function smarty_block_textformat($params, $content, $template, &$repeat)
}
// wordwrap sentences
if (Smarty::$_MBSTRING) {
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_wordwrap.php');
$_paragraph = smarty_mb_wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
} else {
$_paragraph = wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
Expand Down
4 changes: 3 additions & 1 deletion libs/plugins/function.html_checkboxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
*/
function smarty_function_html_checkboxes($params, $template)
{
require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
if (!is_callable('smarty_function_escape_special_chars')) {
require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
}

$name = 'checkbox';
$values = null;
Expand Down
4 changes: 3 additions & 1 deletion libs/plugins/function.html_image.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
*/
function smarty_function_html_image($params, $template)
{
require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
if (!is_callable('smarty_function_escape_special_chars')) {
require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
}

$alt = '';
$file = '';
Expand Down
4 changes: 3 additions & 1 deletion libs/plugins/function.html_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
*/
function smarty_function_html_options($params)
{
require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
if (!is_callable('smarty_function_escape_special_chars')) {
require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
}

$name = null;
$values = null;
Expand Down
4 changes: 3 additions & 1 deletion libs/plugins/function.html_radios.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
*/
function smarty_function_html_radios($params, $template)
{
require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
if (!is_callable('smarty_function_escape_special_chars')) {
require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
}

$name = 'radio';
$values = null;
Expand Down
15 changes: 6 additions & 9 deletions libs/plugins/function.html_select_date.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@
* @subpackage PluginsFunction
*/

/**
* @ignore
*/
require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
/**
* @ignore
*/
require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');

/**
* Smarty {html_select_date} plugin
* Type: function<br>
Expand Down Expand Up @@ -52,6 +43,12 @@
*/
function smarty_function_html_select_date($params)
{
if (!is_callable('smarty_function_escape_special_chars')) {
require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
}
if (!is_callable('smarty_make_timestamp')) {
require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');
}
// generate timestamps used for month names only
static $_month_timestamps = null;
static $_current_year = null;
Expand Down
15 changes: 6 additions & 9 deletions libs/plugins/function.html_select_time.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@
* @subpackage PluginsFunction
*/

/**
* @ignore
*/
require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
/**
* @ignore
*/
require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');

/**
* Smarty {html_select_time} function plugin
* Type: function<br>
Expand All @@ -33,6 +24,12 @@
*/
function smarty_function_html_select_time($params)
{
if (!is_callable('smarty_function_escape_special_chars')) {
require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
}
if (!is_callable('smarty_make_timestamp')) {
require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');
}
$prefix = "Time_";
$field_array = null;
$field_separator = "\n";
Expand Down
6 changes: 1 addition & 5 deletions libs/plugins/modifier.date_format.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ function smarty_modifier_date_format($string, $format = null, $default_date = ''
if ($format === null) {
$format = Smarty::$_DATE_FORMAT;
}
/**
* require_once the {@link shared.make_timestamp.php} plugin
*/
require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');
if ($string != '' && $string != '0000-00-00' && $string != '0000-00-00 00:00:00') {
$timestamp = smarty_make_timestamp($string);
} elseif ($default_date != '') {
Expand All @@ -44,7 +40,7 @@ function smarty_modifier_date_format($string, $format = null, $default_date = ''
return;
}
if ($formatter == 'strftime' || ($formatter == 'auto' && strpos($format, '%') !== false)) {
if (DIRECTORY_SEPARATOR == '\\') {
if (Smarty::$_IS_WINDOWS) {
$_win_from = array('%D', '%h', '%n', '%r', '%R', '%t', '%T');
$_win_to = array('%m/%d/%y', '%b', "\n", '%I:%M:%S %p', '%H:%M', "\t", '%H:%M:%S');
if (strpos($format, '%e') !== false) {
Expand Down
17 changes: 12 additions & 5 deletions libs/plugins/modifier.escape.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $
case 'hexentity':
$return = '';
if (Smarty::$_MBSTRING) {
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
if (!is_callable('smarty_mb_to_unicode')) {
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
}
$return = '';
foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
$return .= '&#x' . strtoupper(dechex($unicode)) . ';';
Expand All @@ -135,7 +137,9 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $
case 'decentity':
$return = '';
if (Smarty::$_MBSTRING) {
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
if (!is_callable('smarty_mb_to_unicode')) {
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
}
$return = '';
foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
$return .= '&#' . $unicode . ';';
Expand All @@ -158,8 +162,9 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $

case 'mail':
if (Smarty::$_MBSTRING) {
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_str_replace.php');

if (!is_callable('smarty_mb_str_replace')) {
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_str_replace.php');
}
return smarty_mb_str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
}
// no MBString fallback
Expand All @@ -169,7 +174,9 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $
// escape non-standard chars, such as ms document quotes
$return = '';
if (Smarty::$_MBSTRING) {
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
if (!is_callable('smarty_mb_to_unicode')) {
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php');
}
foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
if ($unicode >= 126) {
$return .= '&#' . $unicode . ';';
Expand Down
5 changes: 3 additions & 2 deletions libs/plugins/modifier.replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
function smarty_modifier_replace($string, $search, $replace)
{
if (Smarty::$_MBSTRING) {
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_str_replace.php');

if (!is_callable('smarty_mb_str_replace')) {
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_str_replace.php');
}
return smarty_mb_str_replace($search, $replace, $string);
}

Expand Down
8 changes: 3 additions & 5 deletions libs/plugins/modifiercompiler.escape.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
* @subpackage PluginsModifierCompiler
*/

/**
* @ignore
*/
require_once(SMARTY_PLUGINS_DIR . 'shared.literal_compiler_param.php');

/**
* Smarty escape modifier plugin
* Type: modifier<br>
Expand All @@ -28,6 +23,9 @@
function smarty_modifiercompiler_escape($params, $compiler)
{
static $_double_encode = null;
if (!is_callable('smarty_literal_compiler_param')) {
require_once(SMARTY_PLUGINS_DIR . 'shared.literal_compiler_param.php');
}
if ($_double_encode === null) {
$_double_encode = version_compare(PHP_VERSION, '5.2.3', '>=');
}
Expand Down

2 comments on commit e1d27d6

@ophian
Copy link

@ophian ophian commented on e1d27d6 Sep 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @uwetews
May I ask a question, please? I have read the issue #280
This commit really makes me wonder. Isn't require_once or include_once a language constructs used to check if already bound in...? (Which may internally use something like is_callable too.) If so, is checking for foo and/or is_callable() significant faster, since it does not reduce the run call check itself?

    if (foo && !is_callable('smarty_boobar')) {
        require_once(SMARTY_PLUGINS_DIR . 'shared.foobar.php');
    }

@uwetews
Copy link
Contributor Author

@uwetews uwetews commented on e1d27d6 Sep 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require_once and include_once is much slower than a call of is_callable(). Don't ask me why.

Please sign in to comment.