forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ARROW-13847: [Java] Avoid unnecessary collection copies
There are several occurences in Arrow Java codebase where temporary collections are being created and immediately discarded like when going over a sequence of elements, or when making a copy of a collection provided as an argument to a constructor. This change adds a couple of methods to Collections2 and AutoCloseables, along with some changes to the codebase to remove those extra copies. Closes apache#11063 from laurentgo/laurentgo/avoid-copies Lead-authored-by: Laurent Goujon <laurent@apache.org> Co-authored-by: emkornfield <emkornfield@gmail.com> Signed-off-by: Micah Kornfield <emkornfield@gmail.com>
- Loading branch information
1 parent
3d96177
commit c509e93
Showing
9 changed files
with
157 additions
and
34 deletions.
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
83 changes: 83 additions & 0 deletions
83
java/memory/memory-core/src/test/java/org/apache/arrow/util/TestCollections2.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,83 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.arrow.util; | ||
|
||
import static org.junit.Assert.fail; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
/** | ||
* Tests for {@code Collections2} class. | ||
*/ | ||
public class TestCollections2 { | ||
|
||
|
||
@Test | ||
public void testToImmutableListFromIterable() { | ||
final List<String> source = new ArrayList<>(Arrays.asList("foo", "bar", "baz")); | ||
|
||
final List<String> copy = Collections2.toImmutableList(source); | ||
assertEquals(source, copy); | ||
|
||
try { | ||
copy.add("unexpected"); | ||
fail("add operation should not be supported"); | ||
} catch (UnsupportedOperationException ignored) { | ||
} | ||
|
||
try { | ||
copy.set(0, "unexpected"); | ||
fail("set operation should not be supported"); | ||
} catch (UnsupportedOperationException ignored) { | ||
} | ||
|
||
try { | ||
copy.remove(0); | ||
fail("remove operation should not be supported"); | ||
} catch (UnsupportedOperationException ignored) { | ||
} | ||
|
||
source.set(1, "newvalue"); | ||
source.add("anothervalue"); | ||
|
||
assertEquals("bar", copy.get(1)); | ||
assertEquals(3, copy.size()); | ||
} | ||
|
||
|
||
@Test | ||
public void testStringFromEmptyIterator() { | ||
assertEquals("[]", Collections2.toString(Collections.emptyIterator())); | ||
} | ||
|
||
@Test | ||
public void testStringFromIterator() { | ||
Iterator<String> iterator = Arrays.asList("foo", "bar", "baz").iterator(); | ||
iterator.next(); | ||
|
||
assertEquals("[bar, baz]", Collections2.toString(iterator)); | ||
assertEquals(false, iterator.hasNext()); | ||
} | ||
} |
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