Skip to content

Commit

Permalink
Merge pull request ReactiveX#2912 from akarnokd/FixEventLoopsPerfDegr…
Browse files Browse the repository at this point in the history
…adation

Fix the performance degradation due to different schedule execution and
  • Loading branch information
benjchristensen committed Apr 30, 2015
2 parents f1bd2a9 + 729e90e commit 2532484
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ public Subscription schedule(Action0 action) {
if (isUnsubscribed()) {
return Subscriptions.unsubscribed();
}
ScheduledAction s = poolWorker.scheduleActual(action, 0, null);

serial.add(s);
s.addParent(serial);
ScheduledAction s = poolWorker.scheduleActual(action, 0, null, serial);

return s;
}
Expand Down
33 changes: 6 additions & 27 deletions src/main/java/rx/internal/util/SubscriptionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@
*/
package rx.internal.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import java.util.*;

import rx.Subscription;
import rx.exceptions.Exceptions;
Expand All @@ -34,7 +29,6 @@ public final class SubscriptionList implements Subscription {

private LinkedList<Subscription> subscriptions;
private volatile boolean unsubscribed;
private final ReentrantLock lock = new ReentrantLock();

public SubscriptionList() {
}
Expand Down Expand Up @@ -66,8 +60,7 @@ public void add(final Subscription s) {
return;
}
if (!unsubscribed) {
lock.lock();
try {
synchronized (this) {
if (!unsubscribed) {
LinkedList<Subscription> subs = subscriptions;
if (subs == null) {
Expand All @@ -77,8 +70,6 @@ public void add(final Subscription s) {
subs.add(s);
return;
}
} finally {
lock.unlock();
}
}
// call after leaving the synchronized block so we're not holding a lock while executing this
Expand All @@ -88,15 +79,12 @@ public void add(final Subscription s) {
public void remove(final Subscription s) {
if (!unsubscribed) {
boolean unsubscribe = false;
lock.lock();
try {
synchronized (this) {
LinkedList<Subscription> subs = subscriptions;
if (unsubscribed || subs == null) {
return;
}
unsubscribe = subs.remove(s);
} finally {
lock.unlock();
}
if (unsubscribe) {
// if we removed successfully we then need to call unsubscribe on it (outside of the lock)
Expand All @@ -113,16 +101,13 @@ public void remove(final Subscription s) {
public void unsubscribe() {
if (!unsubscribed) {
List<Subscription> list;
lock.lock();
try {
synchronized (this) {
if (unsubscribed) {
return;
}
unsubscribed = true;
list = subscriptions;
subscriptions = null;
} finally {
lock.unlock();
}
// we will only get here once
unsubscribeFromAll(list);
Expand Down Expand Up @@ -150,12 +135,9 @@ private static void unsubscribeFromAll(Collection<Subscription> subscriptions) {
public void clear() {
if (!unsubscribed) {
List<Subscription> list;
lock.lock();
try {
synchronized (this) {
list = subscriptions;
subscriptions = null;
} finally {
lock.unlock();
}
unsubscribeFromAll(list);
}
Expand All @@ -166,11 +148,8 @@ public void clear() {
*/
public boolean hasSubscriptions() {
if (!unsubscribed) {
lock.lock();
try {
synchronized (this) {
return !unsubscribed && subscriptions != null && !subscriptions.isEmpty();
} finally {
lock.unlock();
}
}
return false;
Expand Down

0 comments on commit 2532484

Please sign in to comment.