RFR: 8314502: Change the comparator taking version of GrowableArray::find to be a template method [v7]

Kim Barrett kbarrett at openjdk.org
Sun Oct 29 07:54:36 UTC 2023


On Sat, 28 Oct 2023 15:27:20 GMT, Quan Anh Mai <qamai at openjdk.org> wrote:

>> Sorry I was unclear: what is the advantage of a reference here? Is it just to avoid copying
>
> @dholmes-ora Yes it helps avoid copying, especially if the copy constructor is non-trivial. And I think it is more idiomatic in C++ to use references here.

Using a reference here leads to unnecessary overhead when `E` is small and
trivially copyable, unless the predicate function is inlined. Pass by value is
better in that case. Of course, as noted above, if `E` is "expensive" to copy
or non-copyable then a reference is needed. Boost has this thing called
`call_traits<E>::param_type` for this issue, but I don't recommend we copy
that.

Idiomatic C++ makes the entire function a template parameter, as was suggested
earlier in this PR. That dodges this question entirely, leaving the parameter
passing decision to the predicate function where it belongs, rather than
having it imposed by GrowableArray::find.  The find function just imposes the
requirement that the predicate satisfies the appropriate constraints, e.g. it
is callable on an element reference and returns convertible to bool.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/15418#discussion_r1375386257


More information about the hotspot-dev mailing list