-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
General and performance tweaks #614
General and performance tweaks #614
Conversation
We gain quite a bit of a speed boost by working with text here since this is a very common function
Also remove unused function
Parsedown.php
Outdated
$beforeTab = strstr($line, "\t", true); | ||
$beforeTab !== false; | ||
$beforeTab = strstr($line, "\t", true) | ||
) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
while (($beforeTab = strstr($line, "\t", true)) !== false) {
// ...
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks :D
Parsedown.php
Outdated
@@ -216,7 +207,7 @@ protected function linesElements(array $lines) | |||
|
|||
if (isset($CurrentBlock['continuable'])) | |||
{ | |||
$Block = $this->{'block'.$CurrentBlock['type'].'Continue'}($Line, $CurrentBlock); | |||
$Block = $this->{"block{$CurrentBlock['type']}Continue"}($Line, $CurrentBlock); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
readability:
$methodName = 'block' . $CurrentBlock['type'] . 'Continue';
$Block = $this->$methodName($Line, $CurrentBlock);
+ all other occurences below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
As mentioned in:
Adjustments to string concatenation syntax appear to offer no observable difference in Parsedown in my testing (despite historic benchmarks from the PHP community apparently indicating a difference).
I think I'll probably change this back to single quotes (as you've mentioned for readability), and since it doesn't offer any observable performance advantage in my testing. (I recall a long time ago that using .
over interpolation had an observable negative difference – I guess things have changed now.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really aim for string interpolation vs. string concatenation here, but rather for defining a complex method name separately.
I don't think that there's a "real" performance difference between string interpolation and string concatenation. I personally prefer string concatenation because I think it's way more readable, especially for complex strings. However, others might say the complete different, so this is basically just a preference.
Parsedown.php
Outdated
{ | ||
$Inline['element']['nonNestables'][] = $non_nestable; | ||
$Inline['element']['nonNestables'][] = $nonNestable; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$Inline['element']['nonNestables'] = array_merge($Inline['element']['nonNestables'], $nonNestables);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Appending to undefined key creates it so we also have to check that the key exists with this method, e.g.:
$Inline['element']['nonNestables'] = isset($Inline['element']['nonNestables'])
? array_merge($Inline['element']['nonNestables'], $nonNestables)
: $nonNestables
;
Still, I wouldn't be surprised if the behaviour currently in use gets warnings added to it at some point. And even if it doesn't, adding the check is still a good thing
"<br />\n", | ||
$safeText | ||
); | ||
$Inline['element']['allowRawHtmlInSafeMode'] = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still in two minds about this change – it's quite a bit faster because plaintext is pretty common and it saves us creating several arrays for each chunk of it, however it blinds any AST post-processor to the content structure. It also perhaps risks encouraging use of rawHtml
+ allowRawHtmlInSafeMode
over the AST, which is less than ideal where it can be trivially avoided.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not so sure whether breaks are structural elements or rather styling elements... Paragraphs are structural elements without any doubt, but single line breaks? Furthermore, isn't this a great example of AST processing adding unnecessary complexity? I don't think that we encourage people to use rawHtml
+ allowRawHtmlInSafeMode
- it's rather a great example of how and under which circumstances it can and should be used. We shouldn't condemn allowRawHtmlInSafeMode
😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not so sure whether breaks are structural elements or rather styling elements...
Yes, you're correct in that they're used for style. I was more going for "structure" here to mean that breaks represent a different kind of object in the syntax tree than plaintext does. I don't really have much concern for why the elements are used (rather that they are elements) :p
I definitely don't want to condemn allowRawHtmlInSafeMode
, but if someone needs to create something simple like <p>foobar</p>
, then I'd much rather they wrote it like:
$Element = array('name' => 'p', 'text' => 'foobar');
than
$Element = array('rawHtml' => '<p>foobar</p>', 'allowRawHtmlInSafeMode' => true);
Especially so when foobar
becomes a variable.
Even if someone does all the escaping correctly, it's much easier to not to have to if it isn't required.
Perhaps an example of how to use raw HTML in a safe way like this is valuable though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I definitely don't want to condemn allowRawHtmlInSafeMode, but if someone needs to create something simple like
<p>foobar</p>
, then I'd much rather …
Perhaps an example of how to use raw HTML in a safe way like this is valuable though.
Full ACK 👍
This might be the first PR in a while that reduces the size of the codebase very slightly ;p
As measured by a few markbench runs, this PR makes Parsedown about 15-20% faster on the github sample profile, and about 10% faster when run without a profile specified. These being run on PHP 7.2.4, and performance diffs are in comparison to 1.8.0-beta-1.
Its probably worth cherry-picking some of these commits over some others. Adjustments to the
inlineText
function have major performance improvements, v.s. things like swapping regular expressions for loops – which doesn't appear to offer much of an observable benefit on my system (latest PHP version at present).Adjustments towards regular expressions may be beneficial where Parsedown would previously encounter catastrophic backtracking with certain inputs.
Adjustments to string concatenation syntax appear to offer no observable difference in Parsedown in my testing (despite historic benchmarks from the PHP community apparently indicating a difference).