max-key and min-key evaluate k multiple times for arguments

Description

max-key or min-key will evaluate (k value) multiple times for arguments if more than 2 arguments are passed. This is undesirable if k is expensive to calculate.

Good perf test:

Approach: Avoid repeated evaluation of k for any element.

A criterium test of the example above shows:

  • Before: 206.017411 µs

  • After: 126.532306 µs

Patch: clj-99-v3.patch

Environment

None

Activity

Show:
import
April 11, 2014, 4:48 PM

Comment made by: stevemkim

sort-by is similarly inefficient. It calls keyfn for every comparison in sort, not once for every element to be sorted.

I'm not familiar with this project's preferred workflow, so I have to ask: Do you want me to open a separate ticket for sort-by, or do you prefer to consolidate that issue into this ticket?

Alex Miller
April 11, 2014, 4:50 PM

Separate.

import
April 11, 2014, 5:47 PM

Comment made by: stevemkim

I created [CLJ-1402] for sort-by

Michael Blume
February 9, 2015, 8:50 PM

clj-99-v2 applies cleanly to master

Michael Blume
November 4, 2015, 1:52 AM

clj-99-v3.patch behaves correctly for collection elements that are nil or false.

Completed

Assignee

Unassigned

Reporter

Andy Fingerhut

Labels

Approval

Ok

Patch

Code and Test

Fix versions

Priority

Major
Configure