Skip to content

perf: is_followed_by take too long for the current user #571

@uukelele

Description

@uukelele

when i go to the page for a user while i'm logged in - scratch.mit.edu/users/username, it immediately shows the "Follow" / "Unfollow" button. in the HTML you can see this:

<div class="follow-button button following grey" data-control="unfollow"> <!-- THIS-->
	<span class="follow text"><span class="icon-sm follower white"></span>Follow</span>
	<span class="unfollow text"><span class="icon-sm follower black"></span>Unfollow</span>
</div>

on initial load, so i'm assuming that data-control will tell the JS which element to hide?

for a user that i'm not currently following it shows as this:

<div class="follow-button button notfollowing blue" data-control="follow">
	<span class="follow text"><span class="icon-sm follower white"></span>Follow</span>
	<span class="unfollow text"><span class="icon-sm follower black"></span>Unfollow</span>
</div>

so this confirms my suspicions, the data-control is an effective way to tell if you are following a scratcher or not.

but right now the code I use to do this:

user.is_followed_by(me.username)

for a user that i follow with only 19 followers total to iterate through, the function took 61.36 seconds to run1 (benchmarked with time.time)

edit: SEE FOOTNOTE, behaviour is inconsistent
in fact see the description of PR for timing details

i think it's better if we have a separate function (or attribute?) on the User class to tell if the connected user is being followed or not by the current user, maybe .is_followed_by_me()

Footnotes

  1. i have no idea why it took that long when i looked through the code it doesn't seem like it should take too long especially for a user with 19 followers means only one iteration ... maybe ratelimiting? i doubted myself here so after about ~10 minutes of no activity on the API i reran the code and still 52 seconds .. not sure what's going on. edit: it's been an hour now and i retried and somehow it's now 0.2 seconds for the exact same user!?!!?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions