Records do not cache hash like normal maps

Description

Records do not cache their hash codes like normal Clojure maps, which affects their performance. This problem has been fixed in CLJS, but still affects JVM CLJ.

Approach: Cache hash values in record definitions, similar to maps.

Timings:

coll

1.9.0-master

1.9.0-master+patch

small record

90 ns

7 ns

big record

446 ns

8 ns

Patch: clj-1224-3.patch

Screened by: Alex Miller

Also see: http://dev.clojure.org/jira/browse/CLJS-281

Environment

None

Activity

Show:
Alex Miller
July 18, 2015, 4:30 AM

Moving back to vetted for screening

Alex Miller
July 29, 2015, 12:17 AM

Hey Nicola, two comments on the hasheq/hashcode impl:

1) I don't think there's any reason to use == in the check instead of =, and = seems better (I think the resulting bytecode is same either way though).

2) The generated bytecode in these cases will call getfield twice in the cached case (once for the check, and once for the return):

Letting a local will avoid that:

Output bytecode:

For me, this was about 2% faster in bench too.

Alex Miller
July 29, 2015, 12:18 AM

Equivalent change in hashCode too.

Nicola Mometto
July 29, 2015, 10:06 AM

Updated patch takes into account Alex's last notes

Alex Miller
September 6, 2016, 8:10 PM

Added clj-1224-3.patch - identical to prior but uses zero? rather than = 0 at Rich's request. Attribution retained.

Completed

Assignee

Unassigned

Reporter

Brandon Bloom

Approval

Ok

Patch

Code and Test

Fix versions

Priority

Critical
Configure