Skip to content

Lockdown firebase arrays and improve index add performance #898

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

Merged
merged 3 commits into from
Sep 11, 2017
Merged

Lockdown firebase arrays and improve index add performance #898

merged 3 commits into from
Sep 11, 2017

Conversation

SUPERCILEX
Copy link
Collaborator

Here's my reasoning behind the change:

  1. Our classes aren't extensible anyway.
    I'm personally a culprit here, I've never really bothered thinking about how devs would extend our firebase arrays. Take the FirebaseIndexArray for example. Let's say a developer wants to override onKeyAdded:
    protected void onKeyAdded(DataSnapshot data) {
    String key = data.getKey();
    DatabaseReference ref = mDataRef.child(key);
    mKeysWithPendingUpdate.add(key);
    // Start listening
    mRefs.put(ref, ref.addValueEventListener(new DataRefListener()));
    }

    All of the fields referenced in there are private so the developer would also have to override onKey[Changed/Removed/Moved]. Since they had to reimplement those methods, they also have to reimplement DataRefListener since it accesses private fields. And then all the other methods are private so at this point the developer has just reimplemented the whole class and there's no point in extending it in the first place. As for getting the callbacks, they should just be using our ChangeEventListener api.
  2. It hinders improvements.
    As you can see in this PR, I was able to improve performance by breaking backwards compatibility.
  3. Favor composition over inheritance.
    Using inheritance (on lists especially) is poor practice and we should encourage developers to use inheritance.

Signed-off-by: Alex Saveau <[email protected]>
@SUPERCILEX
Copy link
Collaborator Author

@samtstern Could you make a 3.0 branch please?

To address your comment:

Making methods private is probably a good thing, but making the class final means that you can't test this class with mocking, which is a pain. For that reason I don't really like using final except on fields.

You can mock final symbols, it's just not a stable api yet. Does that work?

Signed-off-by: Alex Saveau <[email protected]>
@samtstern
Copy link
Contributor

The mocking of final things does not play nice with Android, I tried really hard to get it to work before giving up :-(

And yep, will make that branch. Will branch 3.0 from 2.3.1-dev to start

@samtstern
Copy link
Contributor

@SUPERCILEX
Copy link
Collaborator Author

@samtstern Oh well, that's too bad. 😢 I'll make the classes non-final and keep the other changes.

@SUPERCILEX SUPERCILEX changed the base branch from version-2.3.1-dev to version-3.0.0-dev September 8, 2017 22:53
Signed-off-by: Alex Saveau <[email protected]>
@@ -243,10 +244,14 @@ public String toString() {
/**
* A ValueEventListener attached to the joined child data.
*/
protected class DataRefListener implements ValueEventListener {
private final class DataRefListener implements ValueEventListener {
Copy link
Contributor

Choose a reason for hiding this comment

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

Missed one!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That shouldn't matter, right? We're making it private so it's not mockable anyway.

@samtstern samtstern merged commit e5fbab8 into firebase:version-3.0.0-dev Sep 11, 2017
@SUPERCILEX SUPERCILEX deleted the lockdown branch September 11, 2017 18:54
@Kurt29
Copy link

Kurt29 commented Sep 11, 2017

I dont't want to bother you, but wanted to ask why this approach is more efficient?

@SUPERCILEX
Copy link
Collaborator Author

@Eragon29 we don't have to query for the correct index on addition.

@Kurt29
Copy link

Kurt29 commented Sep 11, 2017

@SUPERCILEX Ah, now I get it.
I think I am just not used to see code like
int index = currentIndex = returnOrFindIndexForKey(currentIndex, key);
in Java. After that looking at the method returnOrFindIndexForKey(index, key) was enough.

@SUPERCILEX
Copy link
Collaborator Author

👍

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