Skip to content

Commit

Permalink
- optimization of lexer speed #311
Browse files Browse the repository at this point in the history
  • Loading branch information
uwetews committed Nov 7, 2016
1 parent 2d2be8f commit c4746e9
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 159 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)
07.11.2016
- optimization of lexer speed https://github.com/smarty-php/smarty/issues/311

27.10.2016
- bugfix template function definitions array has not been cached between Smarty::fetch() and Smarty::display() calls
https://github.com/smarty-php/smarty/issues/301
Expand Down
20 changes: 8 additions & 12 deletions lexer/smarty_internal_configfilelexer.plex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ class Smarty_Internal_Configfilelexer
* @var string
*/
public $data;
/**
/**
* Source length
*
* @var int
*/
public $dataLenght = null;
/**
* byte counter
*
* @var int
Expand Down Expand Up @@ -120,9 +126,8 @@ class Smarty_Internal_Configfilelexer
*/
function __construct($data, Smarty_Internal_Config_File_Compiler $compiler)
{
// set instance object
self::instance($this);
$this->data = $data . "\n"; //now all lines are \n-terminated
$this->dataLenght = strlen($data);
$this->counter = 0;
if (preg_match('/^\xEF\xBB\xBF/', $this->data, $match)) {
$this->counter += strlen($match[0]);
Expand All @@ -133,15 +138,6 @@ class Smarty_Internal_Configfilelexer
$this->configBooleanize = $this->smarty->config_booleanize;
}

public static function &instance($new_instance = null)
{
static $instance = null;
if (isset($new_instance) && is_object($new_instance)) {
$instance = $new_instance;
}
return $instance;
}

public function PrintTrace()
{
$this->yyTraceFILE = fopen('php://output', 'w');
Expand Down
16 changes: 0 additions & 16 deletions lexer/smarty_internal_configfileparser.y
Original file line number Diff line number Diff line change
Expand Up @@ -91,29 +91,13 @@ class Smarty_Internal_Configfileparser
*/
function __construct(Smarty_Internal_Configfilelexer $lex, Smarty_Internal_Config_File_Compiler $compiler)
{
// set instance object
self::instance($this);
$this->lex = $lex;
$this->smarty = $compiler->smarty;
$this->compiler = $compiler;
$this->configOverwrite = $this->smarty->config_overwrite;
$this->configReadHidden = $this->smarty->config_read_hidden;
}

/**
* @param null $new_instance
*
* @return null
*/
public static function &instance($new_instance = null)
{
static $instance = null;
if (isset($new_instance) && is_object($new_instance)) {
$instance = $new_instance;
}
return $instance;
}

/**
* parse optional boolean keywords
*
Expand Down
14 changes: 11 additions & 3 deletions lexer/smarty_internal_templatelexer.plex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class Smarty_Internal_Templatelexer
*/
public $data;

/**
* Source length
*
* @var int
*/
public $dataLenght = null;

/**
* byte counter
*
Expand Down Expand Up @@ -225,6 +232,7 @@ class Smarty_Internal_Templatelexer
function __construct($data, Smarty_Internal_TemplateCompilerBase $compiler)
{
$this->data = $data;
$this->dataLenght = strlen($data);
$this->counter = 0;
if (preg_match('/^\xEF\xBB\xBF/i', $this->data, $match)) {
$this->counter += strlen($match[0]);
Expand Down Expand Up @@ -366,7 +374,7 @@ class Smarty_Internal_Templatelexer
$this->compiler->getTagCompiler('private_php')->parsePhp($this);
}
text {
$to = strlen($this->data);
$to = $this->dataLenght;
preg_match("/($this->ldel)|(<[?]((php\s+|=)|\s+))|(<[%])|(<[?]xml\s+)|(<script\s+language\s*=\s*[\"']?\s*php\s*[\"']?\s*>)|([?][>])|([%][>])/i",$this->data,$match,PREG_OFFSET_CAPTURE,$this->counter);
if (isset($match[0][1])) {
$to = $match[0][1];
Expand Down Expand Up @@ -606,7 +614,7 @@ class Smarty_Internal_Templatelexer
}
}
text {
$to = strlen($this->data);
$to = $this->dataLenght;
preg_match("/{$this->ldel}[\/]?literal{$this->rdel}/i",$this->data,$match,PREG_OFFSET_CAPTURE,$this->counter);
if (isset($match[0][1])) {
$to = $match[0][1];
Expand Down Expand Up @@ -670,7 +678,7 @@ class Smarty_Internal_Templatelexer
$this->token = Smarty_Internal_Templateparser::TP_TEXT;
}
text {
$to = strlen($this->data);
$to = $this->dataLenght;
$this->value = substr($this->data,$this->counter,$to-$this->counter);
$this->token = Smarty_Internal_Templateparser::TP_TEXT;
}
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/40';
const SMARTY_VERSION = '3.1.31-dev/41';

/**
* define variable scopes
Expand Down
20 changes: 9 additions & 11 deletions libs/sysplugins/smarty_internal_config_file_compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ public function compileTemplate(Smarty_Internal_Template $template)
$this->smarty->_debug->start_compile($this->template);
}
// init the lexer/parser to compile the config file
/* @var Smarty_Internal_ConfigFileLexer $lex */
$lex = new $this->lexer_class(str_replace(array("\r\n", "\r"), "\n", $template->source->getContent()) . "\n",
/* @var Smarty_Internal_ConfigFileLexer $this->lex */
$this->lex = new $this->lexer_class(str_replace(array("\r\n", "\r"), "\n", $template->source->getContent()) . "\n",
$this);
/* @var Smarty_Internal_ConfigFileParser $parser */
$parser = new $this->parser_class($lex, $this);
/* @var Smarty_Internal_ConfigFileParser $this->parser */
$this->parser = new $this->parser_class($this->lex, $this);

if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
$mbEncoding = mb_internal_encoding();
Expand All @@ -125,17 +125,17 @@ public function compileTemplate(Smarty_Internal_Template $template)
}

if ($this->smarty->_parserdebug) {
$parser->PrintTrace();
$this->parser->PrintTrace();
}
// get tokens from lexer and parse them
while ($lex->yylex()) {
while ($this->lex->yylex()) {
if ($this->smarty->_parserdebug) {
echo "<br>Parsing {$parser->yyTokenName[$lex->token]} Token {$lex->value} Line {$lex->line} \n";
echo "<br>Parsing {$this->parser->yyTokenName[$this->lex->token]} Token {$this->lex->value} Line {$this->lex->line} \n";
}
$parser->doParse($lex->token, $lex->value);
$this->parser->doParse($this->lex->token, $this->lex->value);
}
// finish parsing process
$parser->doParse(0, 0);
$this->parser->doParse(0, 0);

if ($mbEncoding) {
mb_internal_encoding($mbEncoding);
Expand Down Expand Up @@ -166,8 +166,6 @@ public function compileTemplate(Smarty_Internal_Template $template)
*/
public function trigger_config_file_error($args = null)
{
$this->lex = Smarty_Internal_Configfilelexer::instance();
$this->parser = Smarty_Internal_Configfileparser::instance();
// get config source line which has error
$line = $this->lex->line;
if (isset($args)) {
Expand Down
Loading

1 comment on commit c4746e9

@chilek
Copy link

@chilek chilek commented on c4746e9 Nov 10, 2016

Choose a reason for hiding this comment

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

s/lenght/length/g

Please sign in to comment.