Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8288723

Avoid redundant ConcurrentHashMap.get call in java.time

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P5 P5
    • 20
    • None
    • core-libs
    • b08

      In a few places in java.time code performs ConcurrentHashMap.putIfAbsent and then ConcurrentHashMap.get by the same key.
      For example java.time.format.DateTimeTextProvider#findStore:

          private Object findStore(TemporalField field, Locale locale) {
              Entry<TemporalField, Locale> key = createEntry(field, locale);
              Object store = CACHE.get(key);
              if (store == null) {
                  store = createStore(field, locale);
                  CACHE.putIfAbsent(key, store);
                  store = CACHE.get(key);
              }
              return store;
          }

      Instead of separate dive into ConcurrentHashMap internal, we can just reuse result of putIfAbsent call.

                  Object prev = CACHE.putIfAbsent(key, store);
                  if (prev != null) {
                      store = prev;
                  }

            aturbanov Andrey Turbanov
            aturbanov Andrey Turbanov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: