Skip to content

Commit

Permalink
Diff update performance improvement (#38)
Browse files Browse the repository at this point in the history
* Apply performance improvement even when we now hashes can't be used as unique identifiers. This will reduce the number of collisions and the refresh rate will not be perfect but good enough for the implementation we've decided to use without any interface asking for the item id

* Prepare release

* Check if the type is the same before checking the item hash. This will improve performance

* Update documentation and ask also for the object memory reference in order to know if two items are the same
  • Loading branch information
pedrovgs authored Jul 19, 2017
1 parent 7418c35 commit d8bbe7b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 31 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ If the ``RecyclerView`` performance is crucial in your application remember you
adapter.diffUpdate(newList)
```

This method provides a ready to use diff update for our adapter based on the implementation of the standard ``equals`` method from ``Object`` class.
This method provides a ready to use diff update for our adapter based on the implementation of the standard ``equals`` and ``hashCode`` methods from ``Object`` class.

Usage
-----
Expand All @@ -214,7 +214,7 @@ Or declare it into your pom.xml
<dependency>
<groupId>com.github.pedrovgs</groupId>
<artifactId>renderers</artifactId>
<version>3.3.0</version>
<version>3.3.1</version>
<type>aar</type>
</dependency>
```
Expand All @@ -223,7 +223,7 @@ Or declare it into your pom.xml
Or into your build.gradle
```groovy
dependencies{
compile 'com.github.pedrovgs:renderers:3.3.0'
compile 'com.github.pedrovgs:renderers:3.3.1'
}
```

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ POM_NAME=Renderers
POM_ARTIFACT_ID=renderers
POM_PACKAGING=aar

VERSION_NAME=3.3.1-SNAPSHOT
VERSION_NAME=3.3.1
VERSION_CODE=030301
GROUP=com.github.pedrovgs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@

import java.util.List;

class DiffCallbacks<T> extends DiffUtil.Callback {
class DiffCallback<T> extends DiffUtil.Callback {

private final AdapteeCollection<T> oldList;
private final List<T> newList;

private int oldItemPosition;

private boolean deep;

DiffCallbacks(AdapteeCollection<T> oldList, List<T> newList) {
DiffCallback(AdapteeCollection<T> oldList, List<T> newList) {
this.oldList = oldList;
this.newList = newList;
}
Expand All @@ -42,27 +38,15 @@ class DiffCallbacks<T> extends DiffUtil.Callback {
}

@Override public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
this.deep = false;
this.oldItemPosition = oldItemPosition;
return equals(newList.get(newItemPosition));
T oldItem = oldList.get(oldItemPosition);
T newItem = newList.get(newItemPosition);
boolean areTheSameInstance = oldItem == newItem;
boolean hasTheSameType = oldItem.getClass().equals(newItem);
boolean hasTheSameHash = oldItem.hashCode() == newItem.hashCode();
return areTheSameInstance && hasTheSameType && hasTheSameHash;
}

@Override public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
this.deep = true;
this.oldItemPosition = oldItemPosition;
return equals(newList.get(newItemPosition));
}

@Override public boolean equals(Object newItem) {
Object current = oldList.get(oldItemPosition);
if (deep) {
return newItem.equals(current);
} else {
return newItem.getClass().equals(current.getClass());
}
}

@Override public int hashCode() {
return super.hashCode();
return oldList.get(oldItemPosition).equals(newList.get(newItemPosition));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ public void diffUpdate(List<T> newList) {
addAll(newList);
notifyDataSetChanged();
} else {
DiffCallbacks diffCallbacks = new DiffCallbacks(collection, newList);
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallbacks);
DiffCallback diffCallback = new DiffCallback(collection, newList);
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);
clear();
addAll(newList);
diffResult.dispatchUpdatesTo(this);
Expand Down

0 comments on commit d8bbe7b

Please sign in to comment.