Skip to content

Conversation

@HannesWell
Copy link
Contributor

This PR contains the internal code simplifications and modernizations that happened as side effect of the following PRs, in order to ease their review:

else {
result.append("{\n ");
result.append(Joiner.on("\n ").join(uris));
result.append(uris.stream().map(URI::toString).collect(Collectors.joining("\n ")));
Copy link
Contributor

Choose a reason for hiding this comment

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

Quite a mouthful compared to Joiner.on. Both in terms of characters, cognitive load and object allocations. I'd keep this as a Joiner.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Quite a mouthful compared to Joiner.on. Both in terms of characters, cognitive load and object allocations.

Yes it has more characters, but personally I would say it is simple since you can read the pipeline from left to right, but I'm more used to Streams than Guava's Joiner so it is a matter of taste.
I haven't counted all object creations, but Joiner also creates objects and the JVM+GCs are usually good in handling short living objects respectively even avoiding their creation on the heap.
But the biggest impact is probably due to the creation of the String result of the join operation and if you are really concerned about object creation here, I would try to avoid that by using something like the following instead:

for (Iterator<String> containers = invisibleContainers.iterator(); containers.hasNext();) {
	result.append(containers.next());
	if(containers.hasNext()) {
		result.append("\n    ");
	}
}

That being said, I reverted that change for now.

@HannesWell HannesWell force-pushed the internal-cleanups branch 2 times, most recently from e43173b to 8af825e Compare April 20, 2024 17:10
@HannesWell
Copy link
Contributor Author

Just rebased this on the latest master, is there anything else I should adjust?

*/
public static <T> Multimap<T,IEObjectDescription> index(Iterable<IEObjectDescription> descriptions, Function<IEObjectDescription,T> indexer) {
ArrayList<IEObjectDescription> list = Lists.newArrayList(descriptions);
List<IEObjectDescription> list = 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.

It's ok here, but keep in mind that Lists.newArrayList(Iterable) has a special casing for Collections and will call the more optimal ArrayList(Collection) constructor, if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's right. But since the purpose of the entire creation of the ArrayList seems to be being able to determine the size of the Iterable and at the same time avoiding the second pass over the Iterable for the subsequent iteration, the best would be to to test if the Iterable is collection and act accordingly or provide an overload for collections.
The latter could then be called by the current method after the iterable content was copied into a List.

@github-actions
Copy link

Test Results

  6 460 files  ± 0    6 460 suites  ±0   3h 4m 27s ⏱️ - 7m 48s
 43 240 tests ± 0   42 657 ✅ ±0    583 💤 ±0   0 ❌ ± 0 
170 113 runs  +15  167 736 ✅ ±0  2 354 💤 ±0  22 ❌ +14  1 🔥 +1 

Results for commit 0b4bcac. ± Comparison against base commit 6c31185.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants