<i18n dev> RFR: 8288723: Avoid redundant ConcurrentHashMap.get call in java.time [v2]

Attila Szegedi attila at openjdk.org
Sun Jul 3 19:51:23 UTC 2022


On Tue, 28 Jun 2022 07:27:30 GMT, Andrey Turbanov <aturbanov at openjdk.org> wrote:

>> src/java.base/share/classes/java/time/temporal/WeekFields.java line 331:
>> 
>>> 329:         String key = firstDayOfWeek.toString() + minimalDaysInFirstWeek;
>>> 330:         WeekFields rules = CACHE.get(key);
>>> 331:         if (rules == null) {
>> 
>> Perhaps you don't even need this `if` block and the preceding `CACHE.get(key)` and maybe it can be replaced with:
>> 
>> 
>> return CACHE.computeIfAbsent(key, ignore -> new WeekFields(firstDayOfWeek, minimalDaysInFirstWeek));
>
> But it will generate garbage: non-static lambda.

It already generates some garbage as it does string concatenation for the key. Here's an idea: declare a record class for the key, `record CacheKey(DayOfWeek firstDayOfWeek, int minimalDaysInFirstWeek)`. It will be more efficient than using strings for keys, and then you can have a static lambda.

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

PR: https://git.openjdk.org/jdk/pull/9208


More information about the i18n-dev mailing list