-
Notifications
You must be signed in to change notification settings - Fork 326
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
Optimize PackageInfoService #1136
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7cd31f2
gapidapk: Add profiling statistics.
ben-clayton 638b2b7
gapidapk: Resize exceptionally large icons.
ben-clayton 035d60f
gapidapk: Use a disk cache to speed up package lists.
ben-clayton 129be97
gapidapk: Optimize EnsureInstalled.
ben-clayton 463c5b2
gapidapk: Optimize EnsureInstalled.
ben-clayton 91ad2fb
gapidapk/android: Add .gitignore file for AS generated files
ben-clayton 691a884
gapidapk: Debug logging
ben-clayton d492248
core/os/android/adb: Try port connection more frequently.
ben-clayton 20a378f
gapidapk: Remove unused imports, disable profiling.
ben-clayton e1d4217
Address review comments
ben-clayton 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 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 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 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 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 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 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 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,5 @@ | ||
# Ignore files generated by Android Studio | ||
.gradle/** | ||
.idea/** | ||
local.properties | ||
*.iml |
65 changes: 65 additions & 0 deletions
65
gapidapk/android/app/src/main/java/com/google/android/gapid/Cache.java
This file contains 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,65 @@ | ||
/* | ||
* Copyright (C) 2017 Google Inc. | ||
* | ||
* 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.android.gapid; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Cache is an in-memory key-value store. Values are generated by the {@link Cache.Builder} when | ||
* there is a cache-miss. | ||
* @param <K> the cache key type. | ||
* @param <V> the cache value type. | ||
*/ | ||
public class Cache<K, V> { | ||
private final Map<K, V> map = new HashMap<>(); | ||
private final Builder<K, V> builder; | ||
|
||
/** | ||
* Builder generates new values when there is a cache miss. | ||
* @param <K> the cache key type. | ||
* @param <V> the cache value type. | ||
*/ | ||
interface Builder<K, V> { | ||
V build(K key); | ||
} | ||
|
||
public static <K, V> Cache<K, V> create(Builder<K, V> builder) { | ||
return new Cache<>(builder); | ||
} | ||
|
||
/** | ||
* Lookups the value from the cache using the given key. If the cache does not contain the value | ||
* then the {@link Builder} will be used to create the value. | ||
*/ | ||
public V get(K key) { | ||
synchronized (map) { | ||
if (map.containsKey(key)) { | ||
return map.get(key); | ||
} | ||
} | ||
V value = builder.build(key); | ||
synchronized (map) { | ||
map.put(key, value); | ||
} | ||
return value; | ||
} | ||
|
||
private Cache(Builder<K, V> builder) { | ||
this.builder = builder; | ||
} | ||
} |
173 changes: 173 additions & 0 deletions
173
gapidapk/android/app/src/main/java/com/google/android/gapid/Counter.java
This file contains 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,173 @@ | ||
/* | ||
* Copyright (C) 2017 Google Inc. | ||
* | ||
* 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.android.gapid; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Counter is a basic multi-threaded hierarchical scope based profiler. | ||
*/ | ||
public class Counter { | ||
private static final Counter ROOT = new Counter("<root>", null); | ||
private static final ThreadLocal<Counter> CURRENT = new ThreadLocal<>(); | ||
|
||
private final Map<String, Counter> children = new HashMap<>(); | ||
private final String name; | ||
private final Counter parent; | ||
private int count = 0; | ||
private float time = 0; | ||
|
||
/** | ||
* time is used to profile the amount of time spent in the given scope, identified by the given | ||
* name. | ||
* | ||
* It is to be used with a try-with-resources block: | ||
* <code> | ||
* try (Counter.Scope t = Counter.time("doSomething")) { | ||
* doSomething(); | ||
* } | ||
* </code> | ||
* | ||
* @param name the scope name. | ||
* @return an {@link AutoCloseable} scope. | ||
*/ | ||
public static Scope time(String name) { | ||
Counter counter = CURRENT.get(); | ||
if (counter == null) { | ||
counter = ROOT.child("Thread " + Thread.currentThread().getName()); | ||
CURRENT.set(counter); | ||
} | ||
return new Scope(counter.child(name)); | ||
} | ||
|
||
/** | ||
* Collect returns the timing information about all counters, across all threads. | ||
* If reset is true, then the counters will be all cleared back to 0. | ||
*/ | ||
public static String collect(boolean reset) { | ||
StringBuilder sb = new StringBuilder(); | ||
synchronized (ROOT) { | ||
for (Counter thread : ROOT.children.values()) { | ||
thread.write(sb, 0); | ||
} | ||
} | ||
if (reset) { | ||
ROOT.reset(); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private Counter(String name, Counter parent) { | ||
this.name = name; | ||
this.parent = parent; | ||
} | ||
|
||
private synchronized Counter child(String name) { | ||
Counter counter = children.get(name); | ||
if (counter == null) { | ||
counter = new Counter(name, this); | ||
children.put(name, counter); | ||
} | ||
return counter; | ||
} | ||
|
||
private void enter() { | ||
CURRENT.set(this); | ||
} | ||
|
||
private void exit(float duration) { | ||
synchronized (this) { | ||
time += duration; | ||
count++; | ||
} | ||
CURRENT.set(parent); | ||
} | ||
|
||
private static void indent(StringBuilder sb, int depth) { | ||
for (int i = 0; i < depth; i++) { | ||
sb.append(" "); | ||
} | ||
} | ||
|
||
private synchronized void reset() { | ||
children.clear(); | ||
count = 0; | ||
time = 0; | ||
} | ||
|
||
private synchronized void write(StringBuilder sb, int depth) { | ||
if (count > 0) { | ||
indent(sb, depth); | ||
if (parent.time != 0) { | ||
sb.append((int)(100 * time / parent.time)).append("% "); | ||
} | ||
|
||
sb.append(name).append(" ") | ||
.append("[total: ").append(time) | ||
.append(" count: ").append(count) | ||
.append(" average: ").append(time / count) | ||
.append("]\n"); | ||
} else { | ||
sb.append(name).append("\n"); | ||
} | ||
|
||
if (children.size() > 0) { | ||
List<Counter> sorted = new ArrayList<>(); | ||
sorted.addAll(children.values()); | ||
Collections.sort(sorted, new Comparator<Counter>() { | ||
@Override | ||
public int compare(Counter a, Counter b) { | ||
return Float.compare(a.time, b.time); | ||
} | ||
}); | ||
for (Counter child : sorted) { | ||
child.write(sb, depth+1); | ||
} | ||
|
||
float other = time; | ||
for (Counter child : children.values()) { | ||
other -= child.time; | ||
} | ||
if (other > 0) { | ||
indent(sb, depth+1); | ||
sb.append((int)(100 * other / time)).append("% <other>\n"); | ||
} | ||
} | ||
} | ||
|
||
public static class Scope implements AutoCloseable { | ||
private final long start; | ||
private final Counter counter; | ||
|
||
private Scope(Counter counter) { | ||
this.start = System.nanoTime(); | ||
this.counter = counter; | ||
counter.enter(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
long end = System.nanoTime(); | ||
counter.exit((end - start) / 1000000000.0f); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
Add a comment that this is not thread safe.
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.
Made thread safe instead.