<div dir="auto">Hi,<div dir="auto"><br></div><div dir="auto">Thanks for the correction. I confused the implementations.</div><div dir="auto"><br></div><div dir="auto">I'm proposing to change:</div><div dir="auto"><br></div><div dir="auto">```java</div><div dir="auto">return (year & 15) == 0 ? (year & 3) == 0 : (year & 3) == 0 && year % 100 != 0;</div><div dir="auto">```</div><div dir="auto"><br></div><div dir="auto">to:</div><div dir="auto"><br></div><div dir="auto">```java</div><div dir="auto">return (year & 15) == 0 || ((year & 3) == 0 && year % 100 != 0);</div><div dir="auto">```</div><div dir="auto"><br></div><div dir="auto">At the low level, this should be slightly faster because:</div><div dir="auto"><br></div><div dir="auto">1. Fewer CPU branches - Ternary creates a true branch, while logical operators use short-circuit evaluation</div><div dir="auto">2. Better for branch prediction - Simpler control flow pattern</div><div dir="auto">3. Less operation duplication - The current code conceptually checks (year & 3) == 0 twice in the false case</div><div dir="auto"><br></div><div dir="auto">However, I understand any performance difference would likely be minimal. If the team prefers the current ternary operator for better readability, I fully respect that decision.</div></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">Raffaello Giulietti <<a href="mailto:raffaello.giulietti@oracle.com">raffaello.giulietti@oracle.com</a>> 于 2025年12月14日周日 19:45写道:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
the current logic in mainline is<br>
```<br>
return (year & 15) == 0 ? (year & 3) == 0 : (year & 3) == 0 && year % 100 != 0;<br>
```<br>
(see <a href="https://github.com/openjdk/jdk/blob/fb531cdaf3b30034e0efa86b9b20558478ce94d0/src/java.base/share/classes/java/time/Year.java#L321" rel="noreferrer noreferrer" target="_blank">https://github.com/openjdk/jdk/blob/fb531cdaf3b30034e0efa86b9b20558478ce94d0/src/java.base/share/classes/java/time/Year.java#L321</a>)<br>
<br>
<br>
________________________________________<br>
From: core-libs-dev <<a href="mailto:core-libs-dev-retn@openjdk.org" target="_blank" rel="noreferrer">core-libs-dev-retn@openjdk.org</a>> on behalf of Memory <<a href="mailto:smqy2314@gmail.com" target="_blank" rel="noreferrer">smqy2314@gmail.com</a>><br>
Sent: Sunday, December 14, 2025 09:45<br>
To: <a href="mailto:core-libs-dev@openjdk.org" target="_blank" rel="noreferrer">core-libs-dev@openjdk.org</a><br>
Subject: Question about potential optimization for Year.isLeap()<br>
<br>
Hello core-libs-dev team,<br>
<br>
My name is Memory2314, and I am a new contributor currently waiting for my Oracle Contributor Agreement (OCA) to be processed.<br>
<br>
I have been studying the `java.time.Year.isLeap()` method and would like to propose a micro-optimization:<br>
<br>
**Current logic:**<br>
```java<br>
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);<br>
```<br>
<br>
**Proposed optimization:**<br>
```java<br>
return (year & 15) == 0 || ((year & 3) == 0 && year % 100 != 0);<br>
```<br>
<br>
**Key improvements:**<br>
- Replaces `year % 4 == 0` with bitwise `(year & 3) == 0`<br>
- Uses `(year & 15) == 0` to efficiently detect years divisible by 400<br>
- Reduces modulo operations from 3 to 1 in the common case<br>
<br>
**Verification benchmark:**<br>
```java<br>
public static void main(String[] args) {<br>
int[] years = new int[1_000_000_000];<br>
Random random = new Random();<br>
for (int i = 0; i < years.length; i++) {<br>
years[i] = 1970 + random.nextInt(5000 - 1970 + 1);<br>
}<br>
<br>
long start1 = System.currentTimeMillis();<br>
for (int year : years) {<br>
boolean result = isLeapOriginal(year);<br>
}<br>
System.out.println("Original: " + (System.currentTimeMillis()-start1) + "ms");<br>
<br>
long start2 = System.currentTimeMillis();<br>
for (int year : years) {<br>
boolean result = isLeapOptimized(year);<br>
}<br>
System.out.println("Optimized: " + (System.currentTimeMillis()-start2) + "ms");<br>
}<br>
<br>
public static boolean isLeapOriginal(long year) {<br>
return (year & 15) == 0 ? (year & 3) == 0 : (year & 3) == 0 && year % 100 != 0;<br>
}<br>
<br>
public static boolean isLeapOptimized(long year) {<br>
return (year & 15) == 0 || ((year & 3) == 0 && year % 100 != 0);<br>
}<br>
```<br>
<br>
**Correctness verification:** I've tested this logic extensively, including edge cases like year 0, negative years (proleptic Gregorian), and all century boundaries from -10,000 to 10,000.<br>
<br>
I am aware that I cannot submit a formal patch until my OCA is complete. However, I would be very grateful for your initial technical feedback on this approach before I proceed to create a fully tested patch with benchmarks.<br>
<br>
Once my OCA is in place, would there be a maintainer or an experienced contributor interested in sponsoring this change if it proves worthwhile?<br>
<br>
Thank you for your time and consideration.<br>
<br>
Best regards,<br>
Memory2314<br>
</blockquote></div>