Skip to content

Add a read-only visitor pattern. - #96

Merged
Pike merged 1 commit into
projectfluent:masterfrom
Pike:BaseNode.visit
Feb 7, 2019
Merged

Add a read-only visitor pattern.#96
Pike merged 1 commit into
projectfluent:masterfrom
Pike:BaseNode.visit

Conversation

@Pike

@PikePike commented Feb 7, 2019

Copy link
Copy Markdown
Contributor

This includes some performance tests to compare the visitor and a
traversal. Locally, the visitor is twice as fast.

This is the most basic Visitor pattern. I've had versions that were
more generic, and performance gains vanished quickly. In particular
optionally collecting results on exit halfed the performance benefits.

If we want to factor that in, we should have an independent base class
for that.

This should fix#68, I think.

@Pike
Pike requested a review from stasmFebruary 7, 2019 15:17
@PikePike added the fluent.syntax Issues related to the fluent.syntax module. label Feb 7, 2019

@stasmstasm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is great! Thanks so much, @Pike!

if not entering(node):
return
for propname, propvalue in vars(node).items():
self.visit(propvalue)

@stasmstasmFeb 7, 2019

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd like to suggest a slightly more verbose way of writing this method. It's a nit, so feel free to ignore. As you know, I've been trying to promote the idea of self-documenting code in my reviews :) Here, the value I think this brings is that it gives an explicit meaning to the return value of the process_node method.

defvisit_node(self, node):
nodename=type(node).__name__process_node=getattr(self, 'visit_{}'.format(nodename), self.generic_visit)
should_continue=process_node(node)
ifshould_continue:
forpropname, propvalueinvars(node).items():
self.visit(propvalue)

Comment threadfluent.syntax/tests/syntax/test_visitor.py
This includes some performance tests to compare the visitor and a
traversal. Locally, the visitor is twice as fast.
This is the most basic Visitor pattern. I've had versions that were
more generic, and performance gains vanished quickly. In particular
optionally collecting results on exit halfed the performance benefits.
If we want to factor that in, we should have an independent base class
for that.
@Pike
Pike merged commit f3f9053 into projectfluent:masterFeb 7, 2019
@Pike
Pike deleted the BaseNode.visit branch February 7, 2019 22:46
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fluent.syntaxIssues related to the fluent.syntax module.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement BaseNode.visit API

2 participants

@Pike@stasm