Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
naotoj committed Dec 20, 2024
1 parent c75b1d4 commit 3df7e9f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 9 deletions.
14 changes: 9 additions & 5 deletions src/java.base/share/classes/java/time/ZoneOffset.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -424,11 +424,15 @@ public static ZoneOffset ofTotalSeconds(int totalSeconds) {
throw new DateTimeException("Zone offset not in valid range: -18:00 to +18:00");
}
if (totalSeconds % (15 * SECONDS_PER_MINUTE) == 0) {
return SECONDS_CACHE.computeIfAbsent(totalSeconds, totalSecs -> {
ZoneOffset result = new ZoneOffset(totalSecs);
Integer totalSecs = totalSeconds;
ZoneOffset result = SECONDS_CACHE.get(totalSecs);
if (result == null) {
result = new ZoneOffset(totalSeconds);
SECONDS_CACHE.putIfAbsent(totalSecs, result);
result = SECONDS_CACHE.get(totalSecs);
ID_CACHE.putIfAbsent(result.getId(), result);
return result;
});
}
return result;
} else {
return new ZoneOffset(totalSeconds);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -309,7 +309,13 @@ public Iterator<Entry<String, Long>> getTextIterator(Chronology chrono, Temporal

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

private static int toWeekDay(int calWeekDay) {
Expand Down
10 changes: 8 additions & 2 deletions src/java.base/share/classes/java/time/format/DecimalStyle.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -160,7 +160,13 @@ public static DecimalStyle ofDefaultLocale() {
*/
public static DecimalStyle of(Locale locale) {
Objects.requireNonNull(locale, "locale");
return CACHE.computeIfAbsent(locale, DecimalStyle::create);
DecimalStyle info = CACHE.get(locale);
if (info == null) {
info = create(locale);
CACHE.putIfAbsent(locale, info);
info = CACHE.get(locale);
}
return info;
}

private static DecimalStyle create(Locale locale) {
Expand Down
53 changes: 53 additions & 0 deletions test/micro/org/openjdk/bench/java/time/ZoneOffsetBench.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.java.time;

import java.time.ZoneOffset;
import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

@BenchmarkMode(Mode.AverageTime)
@State(Scope.Benchmark)
@Fork(1)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 2)
@Measurement(iterations = 5)
public class ZoneOffsetBench {

@Benchmark
public void ofTotalSeconds() {
for (int i = 0; i < 1_000; i++) {
ZoneOffset.ofTotalSeconds(0);
}
}
}

0 comments on commit 3df7e9f

Please sign in to comment.