GeezerVison is showing github.com — Allocate less in QueryStringDecoder.addParam for typical use case (#1… · netty/netty@893ea2e
File tree
Expand file treeCollapse file tree
Open diff view settings
codec-http/src/main/java/io/netty/handler/codec/http
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/QueryStringDecoder.java +9-4Lines changed: 9 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -248,7 +248,7 @@ private Map<String, List<String>> decodeParams(String s, int from, Charset chars |
248 | 248 | if (s.charAt(from) == '?') { |
249 | 249 | from++; |
250 | 250 | } |
251 | | - Map<String, List<String>> params = new LinkedHashMap<String, List<String>>(); |
| 251 | + Map<String, List<String>> params = new LinkedHashMap<>(); |
252 | 252 | int nameStart = from; |
253 | 253 | int valueStart = -1; |
254 | 254 | int i; |
@@ -298,10 +298,15 @@ private boolean addParam(String s, int nameStart, int valueStart, int valueEnd, |
298 | 298 | String value = decodeComponent(s, valueStart, valueEnd, charset, htmlQueryDecoding); |
299 | 299 | List<String> values = params.get(name); |
300 | 300 | if (values == null) { |
301 | | - values = new ArrayList<String>(1); // Often there's only 1 value. |
302 | | - params.put(name, values); |
| 301 | + params.put(name, Collections.singletonList(value)); |
| 302 | + } else if (values instanceof ArrayList) { |
| 303 | + values.add(value); |
| 304 | + } else { |
| 305 | + List<String> newValues = new ArrayList<>(2); |
| 306 | + newValues.add(values.get(0)); |
| 307 | + newValues.add(value); |
| 308 | + params.put(name, newValues); |
303 | 309 | } |
304 | | - values.add(value); |
305 | 310 | return true; |
306 | 311 | } |
307 | 312 |
|
|