Skip to content
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

Streamline implementation details for performance and readability #8

Merged
merged 1 commit into from
May 15, 2018

Conversation

thiemowmde
Copy link
Contributor

I did not actually benchmarked these. The actual performance gain might be negligible. However, I still believe it is worth applying these optimizations. The motivation is not only to make this code (potentially) faster, but also simpler and more easy to read. For example, the goal of the comparison count() === 0 is not to actually count the number of elements, but to check if it's empty. This becomes much more obvious with empty() or === [].

@manicki manicki requested a review from bekh6ex May 14, 2018 07:32
@manicki
Copy link
Member

manicki commented May 14, 2018

On random note then: If you say this is not about performance, why not say in the commit message/whatnot this is about readability etc?

@thiemowmde thiemowmde changed the title Apply some minor performance optimizations Streamline implementation details for performance and readability May 14, 2018
@thiemowmde
Copy link
Contributor Author

Better?

@@ -11,10 +11,10 @@ class BasicJsExpressionParser implements JsExpressionParser {
*/
public function parse( $expression ) {
$expression = $this->normalizeExpression( $expression );
if ( strpos( $expression, '!' ) === 0 ) { // ! operator application
if ( strncmp( $expression, '!', 1 ) === 0 ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, by the way, an other perfectly valid and equally fast possibility is substr( $expression, 0, 1 ) === '!'. Just recently we discussed this in the MediaWiki CodeSniffer project, and preferred substr over strncmp, which might be a little obscure.

@manicki
Copy link
Member

manicki commented May 14, 2018

Thanks for the contribution. I am not going to have time to review this PR this week (most likely neither will I the week after this one), as there are more time-pressing things on my table now. Same most likely applies to other Wikidata developers currently.
As the change does not seem of that high priority, I believe you understand the delayed review.

@thiemowmde
Copy link
Contributor Author

Sure.

Since this is technically part of the Lexeme project, I would love to see this reviewed and merged before the project ends. But since what is done in this patch is more code cleanup than anything, there is no reason to rush.

@@ -171,7 +171,7 @@ private function replaceMustacheVariables( DOMNode $node, array $data ) {
private function handleAttributeBinding( DOMElement $node, array $data ) {
/** @var DOMAttr $attribute */
foreach ( iterator_to_array( $node->attributes ) as $attribute ) {
if ( !preg_match( '/^:[\-\_\w]+$/', $attribute->name ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain why you removed _ here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The \w character class already contains the underscore.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, indeed :)

@mariushoch
Copy link
Member

Looks fine, but one thing I don't understand (see inline comments).

@mariushoch mariushoch merged commit 0a6c949 into master May 15, 2018
@mariushoch mariushoch deleted the minorPerf branch May 15, 2018 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants