The TL;DR version: There was a utility function that was refactored to limit the scope of some variables and avoid the extra overhead of constructing a dictionary of post details that would otherwise have been discarded.
Backlinks are links to content that a given Steem user has posted in the past. Coffeesource lets a user generate a list of backlinks that they can put in their recent posts so that readers can easily get to previous related posts. It's a pretty cool feature. They can generate links with or without images, and then paste these links into the bottom of a post they're working on. Then readers benefit with a quick way to get to older posts. These links can be filtered on tags, too, so that links can be topically relevant.
Previously I added extra functionality to coffeesource's backlinks so they would use relative links instead of always linking to steemit.com. While I was in there, I spotted some scope creep that while it wasn't negatively impacting the performance of the app, it was performing some unnecessary work. Since I was more concerned then with submitting a new feature, I didn't update this code. Instead, I limited my contribution to just what was necessary to add the new feature.
This issue of scope is a topic that I've been wanting to create a post on, but have lacked any good examples to illustrate. The coffeesource code provides a great example because the code is very clean, it solves a very specific purpose, but it doesn't restrict scope enough.
The accounts/utils.py file provides a method (get_user_posts) used throughout the controller code of the coffeesource application for fetching Steem user posts. The code is generally used to fetch Steem user posts from a particular target user's blog and then these details are used to create links in a nicely formatted markdown syntax. The code uses Steem.get_blog function, which returns both posts by that user and posts that have been resteemed by that user.
Previously, the function would search for blog entries that Steem.get_blog method, and then build a dictionary of the details needed to create backlinks.
Once it had done this, it would then fetch the author of the post and check to make sure that it wasn't a resteem. If the post was not a resteem, then this entry would be added to the list of back links to be returned. If the post was actually by the target author, then it would add it to the backlinks list.
Granted, the creation of this dictionary was not terribly expensive, but it was extra overhead.
Here's the relevant code, before my changes, with target areas annotated:
At point 1 is the method invocation fetching blog posts.
At point 2 is the creation of a list to store relevant posts.
At point 3 the entries are being iterated over, one at a time.
At point 4 the new entry for the list of relevant posts is created.
At point 5 the decision is made to keep the post in the new list or discard it.
The decision made by point 5 is actually possible to make before doing any of this work. So this work was moved to follow point 3. The code was then refactored to take this into account.
Here's the code after my changes, but with some annotations:
Notice that points 1, 2, and 3 are the same. But notice how some of the variables that were originally under point 2 are now under point 5? Namely, the category and the entry_dict were both moved to this tighter scope because nothing about them is needed at the higher scope.
The
metadatavariable was missed during this round. Its scope doesn't need to be this wide, it will be corrected in a future pull request that I will not be submitting to utopian as I should have included it with this pull request.
Technically, the entry_dict variable could be removed and the dictionary itself could simply be added raw to the entries_list, but because the function is so short, and it helps with readability, I didn't recommend any change there.
Anyway, that's my contribution, the function for getting posts for a given user will now no longer create and discard a dictionary when the post turns out to be a resteem.
Sorry for the code blocks that are image links. Utopian didn't like any format of code fence that I tried to use.