Add transient keyword to cached toString() value in _str

Description

_str field in Keyword and Symbol classes lazily caches result of toString(). Because this field is not transient, serializing (using Java serialization) any keyword or symbol before and after calling toString() for the first time yields different results:

(import (java.io ByteArrayOutputStream ObjectOutputStream ByteArrayInputStream ObjectInputStream)) (defn- serialize [obj] (with-open [bos (ByteArrayOutputStream.) stream (ObjectOutputStream. bos)] (.writeObject stream obj) (-> bos .toByteArray seq))) ;; keyword example (def k1 (serialize :k)) (println :k) (def k2 (serialize :k)) (= k1 k2) ;;=> false ;; symbol example (def sym 'a) (def s1 (serialize sym)) (println sym) (def s2 (serialize sym)) (= s1 s2) ;;=> false

This issue came up when I was trying to use keywords as key in [Hazelcast](https://github.com/hazelcast/hazelcast) map. Hazelcast uses serialized keys in various scenarios, thus if I first put something to map under key :k and then print :k, I can no longer find such key.

Approach: Add transient keyword to _str field in Keyword and Symbol classes

Patch: CLJ-1408-3.patch

Screened by: Brenton Ashworth

Environment

None

Attachments

3
  • 23 Aug 2014, 05:00 AM
  • 09 May 2014, 09:55 PM
  • 19 Apr 2014, 10:53 AM

Activity

Show:

Alex Miller August 23, 2014 at 5:00 AM

Updated the patch for latest master and added the obvious test.

Andy Fingerhut August 2, 2014 at 3:29 AM

Tomasz, in addition to Alex's previous comment, it appears that a commit made to Clojure master earlier today causes your patches to no longer apply cleanly. I haven't looked to see whether updating the patches would be easy, but likely it is.

Alex Miller June 20, 2014 at 3:22 PM

On second thought, it looks like we have most of the infrastructure for serialization testing anyways, so would appreciate an updated patch with the example turned into a serialization test. Please see test/clojure/test_clojure/serialization.clj for a place to put this (using existing roundtrip function).

Tomasz Nurkiewicz May 9, 2014 at 9:55 PM

Removed `private` keyword

Alex Miller May 9, 2014 at 4:16 AM

Hi Tomasz, I noticed you added the private keyword - please remove that and update the patch.

Completed

Details

Assignee

Reporter

Labels

Approval

Ok

Patch

Code and Test

Priority

Affects versions

Fix versions

Created April 19, 2014 at 10:52 AM
Updated October 7, 2014 at 6:32 PM
Resolved October 7, 2014 at 6:32 PM