Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MINOR: support ImplicitLinkedHashCollection#sort #10456

Merged
merged 1 commit into from
Apr 2, 2021

Conversation

cmccabe
Copy link
Contributor

@cmccabe cmccabe commented Mar 31, 2021

Implement an O(N log N) merge sort in ImplicitLinkedHashCollection.
This is useful sometimes in unit tests for comparing collections.

@ijuma
Copy link
Contributor

ijuma commented Apr 1, 2021

Does this perform better than copying it to an array and using the standard library sort methods (that have been optimized over the years)?

@cmccabe
Copy link
Contributor Author

cmccabe commented Apr 1, 2021

Does this perform better than copying it to an array and using the standard library sort methods (that have been optimized over the years)?

Good question. I wrote a JMH benchmark, and it looks like the standard array sort is 4x faster. I could try to optimize this, but since it's for a unit test, let's just use the array version as you suggested. I updated the PR.

@@ -676,4 +678,18 @@ final int numSlots() {
public Set<E> valuesSet() {
return new ImplicitLinkedHashCollectionSetView();
}

public void sort(Comparator<E> comparator) {
ArrayList<E> array = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we allocate with the right size?

Copy link
Contributor

@ijuma ijuma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. One comment regarding allocating the array list with the right size.

}
array.sort(comparator);
for (E e : array) {
add(e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume we don't have an addAll method that has inherent perf advantages, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The addAll for AbstractCollection is just a simple for loop.

Support sorting the elements in ImplicitLinkedHashCollection.
This is useful sometimes in unit tests for comparing collections.
@cmccabe cmccabe merged commit ec38dcb into apache:trunk Apr 2, 2021
@cmccabe cmccabe deleted the sort_ilhc branch April 2, 2021 01:31
ijuma added a commit to ijuma/kafka that referenced this pull request Apr 4, 2021
…e-allocations-lz4

* apache-github/trunk: (243 commits)
  KAFKA-12590: Remove deprecated kafka.security.auth.Authorizer, SimpleAclAuthorizer and related classes in 3.0 (apache#10450)
  KAFKA-3968: fsync the parent directory of a segment file when the file is created (apache#10405)
  KAFKA-12283: disable flaky testMultipleWorkersRejoining to stabilize build (apache#10408)
  MINOR: remove KTable.to from the docs (apache#10464)
  MONOR: Remove redudant LocalLogManager (apache#10325)
  MINOR: support ImplicitLinkedHashCollection#sort (apache#10456)
  KAFKA-12587 Remove KafkaPrincipal#fromString for 3.0 (apache#10447)
  KAFKA-12426: Missing logic to create partition.metadata files in RaftReplicaManager (apache#10282)
  MINOR: Improve reproducability of raft simulation tests (apache#10422)
  KAFKA-12474: Handle failure to write new session keys gracefully (apache#10396)
  KAFKA-12593: Fix Apache License headers (apache#10452)
  MINOR: Fix typo in MirrorMaker v2 documentation (apache#10433)
  KAFKA-12600: Remove deprecated config value `default` for client config `client.dns.lookup` (apache#10458)
  KAFKA-12952: Remove deprecated LogConfig.Compact (apache#10451)
  Initial commit (apache#10454)
  KAFKA-12575: Eliminate Log.isLogDirOffline boolean attribute (apache#10430)
  KAFKA-8405; Remove deprecated `kafka-preferred-replica-election` command (apache#10443)
  MINOR: Fix docs for end-to-end record latency metrics (apache#10449)
  MINOR Replaced File with Path in LogSegmentData. (apache#10424)
  KAFKA-12583: Upgrade netty to 4.1.62.Final
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants