 Chromium Code Reviews
 Chromium Code Reviews Issue 16108003:
  Avoid parsing path and query string more than once  (Closed) 
  Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
    
  
    Issue 16108003:
  Avoid parsing path and query string more than once  (Closed) 
  Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart| Index: sdk/lib/core/uri.dart | 
| diff --git a/sdk/lib/core/uri.dart b/sdk/lib/core/uri.dart | 
| index c8277e7fc6b4b035ad7624e195049d0817a80740..4f371394ee3bc1bcdce517394dea1f911cb65301 100644 | 
| --- a/sdk/lib/core/uri.dart | 
| +++ b/sdk/lib/core/uri.dart | 
| @@ -81,6 +81,9 @@ class Uri { | 
| */ | 
| final String fragment; | 
| + List<String> _pathSegments; | 
| 
Lasse Reichstein Nielsen
2013/05/30 11:57:50
Dartdoc saying that this caches the computed retur
 
Søren Gjesse
2013/05/30 13:38:25
Done.
 | 
| + Map<String, String> _queryParameters; | 
| 
Lasse Reichstein Nielsen
2013/05/30 11:57:50
Similar.
 
Søren Gjesse
2013/05/30 13:38:25
Done.
 | 
| + | 
| /** | 
| * Creates a new URI object by parsing a URI string. | 
| */ | 
| @@ -167,10 +170,19 @@ class Uri { | 
| * Returns the URI path split into its segments. Each of the | 
| * segments in the returned list have been decoded. If the path is | 
| * empty the empty list will be returned. | 
| + * | 
| + * The returned list should not be modified as the same object will | 
| + * be returned for each call. | 
| 
Lasse Reichstein Nielsen
2013/05/30 11:57:50
"should not" isn't very safe. Could you make it un
 
Søren Gjesse
2013/05/30 13:38:25
Created an immutable list wrapper.
 | 
| */ | 
| List<String> get pathSegments { | 
| - if (path == "") return const<String>[]; | 
| - return path.split("/").map(Uri.decodeComponent).toList(growable: false); | 
| + if (_pathSegments == null) { | 
| + _pathSegments = | 
| + path == "" ? const<String>[] | 
| + : path.split("/") | 
| + .map(Uri.decodeComponent) | 
| + .toList(growable: false); | 
| + } | 
| + return _pathSegments; | 
| } | 
| /* | 
| @@ -178,9 +190,13 @@ class Uri { | 
| * specified for FORM post in the HTML 4.01 specification. Each key | 
| * and value in the returned map have been decoded. If there is no | 
| * query the empty map will be returned. | 
| + * | 
| + * The returned map should not be modified as the same object will | 
| + * be returned for each call. | 
| */ | 
| Map<String, String> get queryParameters { | 
| - return query.split("&").fold({}, (map, element) { | 
| + if (_queryParameters == null) { | 
| + _queryParameters = query.split("&").fold({}, (map, element) { | 
| int index = element.indexOf("="); | 
| if (index == -1) { | 
| if (!element.isEmpty) map[element] = ""; | 
| @@ -190,7 +206,9 @@ class Uri { | 
| map[Uri.decodeQueryComponent(key)] = decodeQueryComponent(value); | 
| } | 
| return map; | 
| - }); | 
| + }); | 
| + } | 
| + return _queryParameters; | 
| } | 
| static String _makeScheme(String scheme) { |