From 83b7be6b15e433f8594bb62dd1dc5115300709f3 Mon Sep 17 00:00:00 2001 From: Piotr Jastrzebski Date: Wed, 4 Mar 2015 19:14:48 +0000 Subject: Optimize VPathFragment creation. When compiling Music app we create 207k instances of VPathFragment. 83k of them are created in VPath.appendPath and VPath.prependPath. Those instances are the same so this change introduces a new static field to VPath which will keep a singleton for all those invocations. The remaining 124k instances are created in VPath constructor and have the same separator (INTERNAL_SEPARATOR). This change introduces a new subclass of VPathFragment to handle separators different than INTERNAL_SEPARATOR. VPathFragment itself assumes INTERNAL_SEPARATOR is used. This way we make those 124k objects smaller and save on GC. Change-Id: Ic69c6c2bcf72473ea71a2d7d0adc1bfb5dc5bbd7 --- sched/src/com/android/sched/vfs/VPath.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'sched') diff --git a/sched/src/com/android/sched/vfs/VPath.java b/sched/src/com/android/sched/vfs/VPath.java index e18c32b..a84ab0e 100644 --- a/sched/src/com/android/sched/vfs/VPath.java +++ b/sched/src/com/android/sched/vfs/VPath.java @@ -34,6 +34,8 @@ public final class VPath implements Cloneable { public static final VPath ROOT = new VPath("", '/'); private static final char INTERNAL_SEPARATOR = '/'; + private static final VPathFragment INTERNAL_SEPARATOR_FRAGMENT = + new VPathFragment(String.valueOf(INTERNAL_SEPARATOR), INTERNAL_SEPARATOR); @Nonnull private static final Splitter splitter = Splitter.on(INTERNAL_SEPARATOR).omitEmptyStrings(); @@ -70,8 +72,7 @@ public final class VPath implements Cloneable { public VPath prependPath(@Nonnull VPath path) { assert !path.isRoot(); if (!this.isRoot()) { - pathFragments.add(0, - new VPathFragment(String.valueOf(INTERNAL_SEPARATOR), INTERNAL_SEPARATOR)); + pathFragments.add(0, INTERNAL_SEPARATOR_FRAGMENT); } pathFragments.addAll(0, path.getPathFragments()); @@ -87,7 +88,7 @@ public final class VPath implements Cloneable { public VPath appendPath(@Nonnull VPath path) { assert !path.isRoot(); if (!this.isRoot()) { - pathFragments.add(new VPathFragment(String.valueOf(INTERNAL_SEPARATOR), INTERNAL_SEPARATOR)); + pathFragments.add(INTERNAL_SEPARATOR_FRAGMENT); } pathFragments.addAll(path.getPathFragments()); @@ -179,7 +180,7 @@ public final class VPath implements Cloneable { /** * A portion of path that should be immutable. */ - static class VPathFragment { + private static class VPathFragment { @Nonnull private final CharSequence path; @@ -213,7 +214,7 @@ public final class VPath implements Cloneable { } private boolean isValidSuffix() { - return !path.toString().contains(String.valueOf(separator)); + return path.toString().indexOf(separator) == -1; } } -- cgit v1.1