Skip to content

Commit 404a423

Browse files
committed
Merge pull request square#377 from square/dimitris/threadlocal
Use ThreadLocal StringBuilder, no iterator on batch delivery.
2 parents df5bdf8 + f893e98 commit 404a423

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

picasso/src/main/java/com/squareup/picasso/BitmapHunter.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ abstract class BitmapHunter implements Runnable {
4545
*/
4646
private static final Object DECODE_LOCK = new Object();
4747

48+
private static final ThreadLocal<StringBuilder> NAME_BUILDER = new ThreadLocal<StringBuilder>() {
49+
@Override protected StringBuilder initialValue() {
50+
return new StringBuilder(Utils.THREAD_PREFIX);
51+
}
52+
};
53+
4854
final Picasso picasso;
4955
final Dispatcher dispatcher;
5056
final Cache cache;
@@ -78,7 +84,7 @@ protected void setExifRotation(int exifRotation) {
7884

7985
@Override public void run() {
8086
try {
81-
Thread.currentThread().setName(Utils.THREAD_PREFIX + data.getName());
87+
updateThreadName(data);
8288

8389
result = hunt();
8490

@@ -188,6 +194,16 @@ Picasso.LoadedFrom getLoadedFrom() {
188194
return loadedFrom;
189195
}
190196

197+
static void updateThreadName(Request data) {
198+
String name = data.getName();
199+
200+
StringBuilder builder = NAME_BUILDER.get();
201+
builder.ensureCapacity(Utils.THREAD_PREFIX.length() + name.length());
202+
builder.replace(Utils.THREAD_PREFIX.length(), builder.length(), name);
203+
204+
Thread.currentThread().setName(builder.toString());
205+
}
206+
191207
static BitmapHunter forRequest(Context context, Picasso picasso, Dispatcher dispatcher,
192208
Cache cache, Stats stats, Action action, Downloader downloader) {
193209
if (action.getData().resourceId != 0) {

picasso/src/main/java/com/squareup/picasso/Picasso.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ public interface RequestTransformer {
8585
switch (msg.what) {
8686
case HUNTER_BATCH_COMPLETE: {
8787
@SuppressWarnings("unchecked") List<BitmapHunter> batch = (List<BitmapHunter>) msg.obj;
88-
for (BitmapHunter hunter : batch) {
88+
//noinspection ForLoopReplaceableByForEach
89+
for (int i = 0, n = batch.size(); i < n; i++) {
90+
BitmapHunter hunter = batch.get(i);
8991
hunter.picasso.complete(hunter);
9092
}
9193
break;

0 commit comments

Comments
 (0)