Skip to content

Conversation

@baolongnt
Copy link

@baolongnt baolongnt commented Nov 3, 2025

Use the fact that the default initial boolean value is false
and only explicitly init the values between the extreme HEX values
This minimized impact during process startup esp. on mobile
as init of a 64k boolean array take a long time (>30ms P90) during Android app launch.

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Nov 3, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@codecov
Copy link

codecov bot commented Nov 3, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.11%. Comparing base (36ca9b8) to head (4e6d274).
⚠️ Report is 73 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #7809      +/-   ##
============================================
- Coverage     90.18%   90.11%   -0.07%     
- Complexity     7226     7324      +98     
============================================
  Files           820      825       +5     
  Lines         21790    22051     +261     
  Branches       2135     2179      +44     
============================================
+ Hits          19651    19871     +220     
- Misses         1468     1502      +34     
- Partials        671      678       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

/** Returns whether the given {@code char} is a valid hex character. */
public static boolean isValidBase16Character(char b) {
return VALID_HEX[b];
if (validHex == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reading the volatile is going to make things slower

if we accept that multiple threads initialize the field, we can avoid this, see https://stackoverflow.com/questions/11051863/lazy-initialization-without-synchronization-or-volatile-keyword

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reading the volatile is going to make things slower

if we accept that multiple threads initialize the field, we can avoid this, see https://stackoverflow.com/questions/11051863/lazy-initialization-without-synchronization-or-volatile-keyword

OK. Let me look into that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you found a good solution that doesn't need locking 😄

@jkwatson
Copy link
Contributor

jkwatson commented Nov 4, 2025

@baolongnt we can't accept this unless you sign the CLA. Are you able to do so?

@baolongnt
Copy link
Author

@baolongnt we can't accept this unless you sign the CLA. Are you able to do so?

Thanks for looking at the PR. I did not think it will get any eyes till I mark as ready. I will have the CLA signed by then.

Init of a 64k boolean array take a long time (>30ms  P90) during Android app launch.
Instead of lazy init the whole array, we lazy init each values.
@baolongnt baolongnt marked this pull request as ready for review December 13, 2025 03:09
@baolongnt baolongnt requested a review from a team as a code owner December 13, 2025 03:09
@baolongnt
Copy link
Author

@jkwatson @zeitlinger PR is ready. CLA signed. Thanks in advance for the review. @

@baolongnt baolongnt requested a review from zeitlinger December 13, 2025 03:09
@jkwatson
Copy link
Contributor

Have you thought about just initializing the true values explicitly, which is far fewer values than the full set of all characters. Maybe that would be a simpler way than doing things lazily like this?

Something like this, since the array will have false as the non-explicitly initialized values?

 private static boolean[] buildValidHexArray() {
    boolean[] validHex = new boolean[Character.MAX_VALUE];
    for (int i = 48; i < 103; i++) {
      validHex[i] = (48 <= i && i <= 57) || (97 <= i && i <= 102);
    }
    return validHex;
  }

@baolongnt
Copy link
Author

Have you thought about just initializing the true values explicitly, which is far fewer values than the full set of all characters. Maybe that would be a simpler way than doing things lazily like this?

I did not think about it but I am happy to switch to that. It's def a simpler change so more practical.
In theory, the proposed implementation results in a faster start up but initializing 50ish booleans is much better than 64K ones.

@baolongnt baolongnt changed the title Lazy init valid hex array Limit init valid hex array to range that can be true Dec 13, 2025
@jkwatson
Copy link
Contributor

Have you thought about just initializing the true values explicitly, which is far fewer values than the full set of all characters. Maybe that would be a simpler way than doing things lazily like this?

I did not think about it but I am happy to switch to that. It's def a simpler change so more practical. In theory, the proposed implementation results in a faster start up but initializing 50ish booleans is much better than 64K ones.

If you could give it a try and see how the startup time is on Android, I think I'd prefer the smaller, simpler change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants