DaveAKing / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

In Splitter.splitToList, presize result array when limit is set #1689

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Optimize the result array, by presizing when a column limit has been set.
Change from:
  public List<String> splitToList(CharSequence sequence) {
    checkNotNull(sequence);

    Iterator<String> iterator = splittingIterator(sequence);
    List<String> result = new ArrayList<String>();

    while (iterator.hasNext()) {
      result.add(iterator.next());
    }

    return Collections.unmodifiableList(result);
  }

To:
  public List<String> splitToList(CharSequence sequence) {
    checkNotNull(sequence);

    Iterator<String> iterator = splittingIterator(sequence);
    List<String> result = Integer.MAX_VALUE == limit ? new ArrayList<String>() : new ArrayList<String>(limit);

    while (iterator.hasNext()) {
      result.add(iterator.next());
    }

    return Collections.unmodifiableList(result);
  }

Original issue reported on code.google.com by rhvar...@gmail.com on 3 Mar 2014 at 6:21

GoogleCodeExporter commented 9 years ago
Might want to have a sanity check also:
private static final int MAX_PRESIZE_ARRAY_SIZE = 200;

List<String> result = Integer.MAX_VALUE == limit ? new ArrayList<String>() : 
new ArrayList<String>(Math.min(limit, MAX_PRESIZE_ARRAY_SIZE));

Original comment by rhvar...@gmail.com on 3 Mar 2014 at 6:23

GoogleCodeExporter commented 9 years ago
Sounds reasonable.

Not quite sure I understand why you'd want that "sanity check" that you talk 
about in update #1 ?

200 is arbitrary (and so would any value we'd place there). Are you just trying 
to protect against someone passing an insanely large number to limit()?

Original comment by kak@google.com on 3 Mar 2014 at 10:57

GoogleCodeExporter commented 9 years ago
Yes, to protect against someone making the limit one billion, for example.

Original comment by rhvar...@gmail.com on 4 Mar 2014 at 12:37

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:09

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:17

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:07