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

Refactor part processing (remove PartProcessor) #31

Merged
merged 4 commits into from
Aug 10, 2020
Merged

Refactor part processing (remove PartProcessor) #31

merged 4 commits into from
Aug 10, 2020

Conversation

matthewmcgarvey
Copy link
Member

Summary

This change removes the PartProcessor class completely and moves the logic into the Fragment class. I believe this brings clarity to the codebase as the logic using the processor required passing in the fragment and all of the passed in information anyways and it was calling methods on the fragment. Along with the move of logic, two changes were made:

  1. Instead of using recursion, I am using a .reduce call on the array of parts. Besides being easier to understand (I hope), it is responsible for the majority of the speed up below.
  2. The processor separated all of its logic between dynamic and regular parts. I encapsulated that into the add_part method so that at the higher level of process_parts there is no difference.

Benchmark (with --release)

  • Before: 205 ms
  • After: 194 ms
  • Difference: 11 ms
  • Improvement: 5%

@matthewmcgarvey matthewmcgarvey changed the title Update part processing (remove PartProcessor) Refactor part processing (remove PartProcessor) Aug 5, 2020
Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

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

I'm loving these small improvements! 🚀 I just left a small question on one part otherwise looks good.

def process_parts(parts : Array(String), method : String, payload : T)
PartProcessor(T).new(self, parts: parts, method: method, payload: payload).run
self
leaf_fragment = parts.reduce(self) { |fragment, part| fragment.add_part(part) }
Copy link
Member

Choose a reason for hiding this comment

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

Nice!

src/lucky_router/fragment.cr Show resolved Hide resolved
Copy link
Member

@paulcsmith paulcsmith left a comment

Choose a reason for hiding this comment

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

I really like this and I love the idea of wrapping the part in a class. That should make it a lot easier to further clarify the code. Nice!

Just some small comments on an explaining variable and removing an early return

name: part[1...],
fragment: Fragment(T).new
)
return dynamic_fragment.fragment
Copy link
Member

Choose a reason for hiding this comment

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

For some reason my brain has a really hard time following early returns. Could this be refactored to use conditionals and implicit returns instead?

Comment on lines 73 to 76
dynamic_fragment = self.dynamic_part ||= DynamicFragment(T).new(
name: part[1...],
fragment: Fragment(T).new
)
Copy link
Member

Choose a reason for hiding this comment

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

It took me awhile to figure out what what this was doing and why and then realized it was removing the first character which is the :. Thoughts on adding this explaining variable to clarify what it is doing?

Suggested change
dynamic_fragment = self.dynamic_part ||= DynamicFragment(T).new(
name: part[1...],
fragment: Fragment(T).new
)
part_without_colon = part[1..]
dynamic_fragment = self.dynamic_part ||= DynamicFragment(T).new(
name: part_without_colon,
fragment: Fragment(T).new
)

Copy link
Member Author

Choose a reason for hiding this comment

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

This kind of thing is why I want to wrap the part strings in a part class.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I was thinking that when I read your comment. That will be a really awesome refactor 👍 Nice idea

src/lucky_router/fragment.cr Show resolved Hide resolved
@matthewmcgarvey
Copy link
Member Author

@paulcsmith I have implemented your changes

Copy link
Member

@paulcsmith paulcsmith left a comment

Choose a reason for hiding this comment

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

Lovely! Thanks for the awesome refactor (and speed boost)

@paulcsmith paulcsmith merged commit c5bba74 into luckyframework:master Aug 10, 2020
@matthewmcgarvey matthewmcgarvey deleted the process-parts-update branch August 10, 2020 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants