-
Notifications
You must be signed in to change notification settings - Fork 617
Performance: Decode documents in background thread #559
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
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
4da49cf
Decode documents in background thread
schmidt-sebastian 629cd45
Merge branch 'master' into mrschmidt/parallel
schmidt-sebastian f9c9c1f
Use Task API
schmidt-sebastian 00717dc
Realize Android Tasks are slow
schmidt-sebastian c5afc57
RFC: ThrottledForwardingExecutor
schmidt-sebastian d93feaf
Add IN and ARRAY_CONTAINS_ANY (not publicly exposed) (#519)
95de378
Add Remote Config smoke tests. (#543)
allisonbm92 d9ef372
Generalize parent repository logic. (#548)
allisonbm92 37d4803
Open source firebase remote config and firebase abt
miraziz f2e25f0
Backporting documentation changes (#550)
rsgowman 45ca056
Update Remote Config and AB Testing versions (#553)
allisonbm92 1799a29
Disable FTL for AB Testing (#554)
allisonbm92 94b5b94
Move RC to firebase-config. (#555)
allisonbm92 1e99b39
Fix FirebaseFirestore formatting. (#556)
allisonbm92 6904cc9
Remove move product flavors from smoke tests. (#557)
allisonbm92 6c663cb
Add option to run tests against emulator (#540)
4fa2fef
Use Task API
schmidt-sebastian e4a3644
Realize Android Tasks are slow
schmidt-sebastian 5237be0
RFC: ThrottledForwardingExecutor
schmidt-sebastian 06173ad
Update documentation to reflect emulator is default for integration t…
afc4382
Allow IN queries with arrays of documentIds (#560)
2075fc7
Migrate ExperimentPayload to AB Testing (#563)
allisonbm92 21c8562
Run IN query integration tests only when running against emulator (#567)
adda141
Add Inapp Messaging smoke test. (#569)
allisonbm92 0afb5d7
Add Proguard Files to Smoke Tests (#572)
allisonbm92 5eca22c
Move DocumentId to public space (#571)
b9e15bb
fdsdfs
schmidt-sebastian a2fba58
Finishing touches
schmidt-sebastian bd24bfe
Merge
schmidt-sebastian 0b2d9ad
Merge
schmidt-sebastian 7fabd7f
Fix merge
schmidt-sebastian 6fb8e9f
Cleanup
schmidt-sebastian 1c8c04c
Make BackgroundQueue a top-level class
schmidt-sebastian 28bda9e
Merge branch 'master' into mrschmidt/parallel
schmidt-sebastian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
firebase-firestore/src/main/java/com/google/firebase/firestore/util/BackgroundQueue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2019 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.firebase.firestore.util; | ||
|
||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.Semaphore; | ||
|
||
/** | ||
* A simple queue that executes tasks in parallel on the Android's AsyncTask.THREAD_POOL_EXECUTOR | ||
* and supports blocking on their completion. | ||
* | ||
* <p>This class is not thread-safe. In particular, `execute()` and `drain()` should not be called | ||
* from parallel threads. | ||
*/ | ||
public class BackgroundQueue implements Executor { | ||
private Semaphore completedTasks = new Semaphore(0); | ||
private int pendingTaskCount = 0; | ||
|
||
/** Enqueue a task on Android's THREAD_POOL_EXECUTOR. */ | ||
@Override | ||
public void execute(Runnable task) { | ||
++pendingTaskCount; | ||
Executors.BACKGROUND_EXECUTOR.execute( | ||
() -> { | ||
task.run(); | ||
completedTasks.release(); | ||
}); | ||
} | ||
|
||
/** Wait for all currently scheduled tasks to complete. */ | ||
public void drain() throws InterruptedException { | ||
completedTasks.acquire(pendingTaskCount); | ||
pendingTaskCount = 0; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...restore/src/main/java/com/google/firebase/firestore/util/ThrottledForwardingExecutor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2019 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.firebase.firestore.util; | ||
|
||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.RejectedExecutionException; | ||
import java.util.concurrent.Semaphore; | ||
|
||
/** | ||
* An executor that forwards executions to another executor, but caps the number of pending | ||
* operations. Tasks scheduled past the specified limit are directly invoked on the calling thread, | ||
* reducing the total memory consumed by pending tasks. | ||
*/ | ||
class ThrottledForwardingExecutor implements Executor { | ||
private final Executor executor; | ||
private final Semaphore availableSlots; | ||
|
||
/** | ||
* Instantiates a new ThrottledForwardingExecutor. | ||
* | ||
* @param maximumConcurrency The maximum number of pending tasks to schedule on the provided | ||
* executor. | ||
* @param executor The executor to forward tasks to. | ||
*/ | ||
ThrottledForwardingExecutor(int maximumConcurrency, Executor executor) { | ||
this.availableSlots = new Semaphore(maximumConcurrency); | ||
this.executor = executor; | ||
} | ||
|
||
/** | ||
* Forwards a task to the provided executor if the current number of pending tasks is less than | ||
* the configured limit. Otherwise, executes the task directly. | ||
* | ||
* @param command The task to run. | ||
*/ | ||
@Override | ||
public void execute(Runnable command) { | ||
if (availableSlots.tryAcquire()) { | ||
try { | ||
executor.execute( | ||
() -> { | ||
command.run(); | ||
availableSlots.release(); | ||
}); | ||
} catch (RejectedExecutionException e) { | ||
command.run(); | ||
} | ||
} else { | ||
command.run(); | ||
} | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
...ore/src/test/java/com/google/firebase/firestore/util/ThrottledForwardingExecutorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2019 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.firebase.firestore.util; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.RejectedExecutionException; | ||
import java.util.concurrent.Semaphore; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.junit.Test; | ||
|
||
public class ThrottledForwardingExecutorTest { | ||
@Test | ||
public void limitsNumberOfForwardedTasks() throws InterruptedException { | ||
Semaphore completedTasks = new Semaphore(0); | ||
int maximumConcurrency = 10; | ||
|
||
CountingExecutor countingExecutor = new CountingExecutor(); | ||
ThrottledForwardingExecutor throttledExecutor = | ||
new ThrottledForwardingExecutor(maximumConcurrency, countingExecutor); | ||
|
||
// Schedule more than `maximumConcurrency` parallel tasks and wait until all scheduling has | ||
// finished. | ||
int numTasks = maximumConcurrency + 1; | ||
CountDownLatch schedulingLatch = new CountDownLatch(1); | ||
for (int i = 0; i < numTasks; ++i) { | ||
int currentTask = i; | ||
throttledExecutor.execute( | ||
() -> { | ||
try { | ||
if (currentTask < maximumConcurrency) { | ||
// Block if we are running on the forwarded executor. We can't block the thread that | ||
// is running this test. | ||
schedulingLatch.await(); | ||
} | ||
completedTasks.release(); | ||
} catch (InterruptedException e) { | ||
fail("Unexpected InterruptedException: " + e); | ||
} | ||
}); | ||
} | ||
schedulingLatch.countDown(); | ||
|
||
// Verify that only `maximumConcurrency` tasks were forwarded to the executor. | ||
completedTasks.acquire(numTasks); | ||
assertEquals(maximumConcurrency, countingExecutor.getNumTasks()); | ||
} | ||
|
||
@Test | ||
public void handlesRejectedExecutionException() { | ||
AtomicInteger result = new AtomicInteger(0); | ||
|
||
ThrottledForwardingExecutor executor = | ||
new ThrottledForwardingExecutor( | ||
10, | ||
command -> { | ||
throw new RejectedExecutionException(); | ||
}); | ||
|
||
executor.execute(result::incrementAndGet); | ||
|
||
assertEquals(1, result.get()); | ||
} | ||
|
||
/** An executor that counts the number of tasks submitted. */ | ||
private static class CountingExecutor implements Executor { | ||
int numTasks = 0; | ||
|
||
@Override | ||
public void execute(Runnable command) { | ||
++numTasks; | ||
new Thread() { | ||
@Override | ||
public void run() { | ||
command.run(); | ||
} | ||
}.start(); | ||
} | ||
|
||
int getNumTasks() { | ||
return numTasks; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't seem like there's any difference between
currentTask
andi
. Maybe just make the loop usecurrentTask
?Similarly, numTasks isn't used except as the loop bound, maybe just use
maximumConcurrency + 1
as the loop condition? (I bet if you shortened it tomaxConcurrency
it might even all still fit in one line.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
currentTask
is the "effectively final" version ofi
and used in the inner class created below.Furthermore,
numTask
is actually used twice - once in the loop and then below in line 60. For that reason, I left it as is.