Configurable Regex Cache

41 views
Skip to first unread message

Jacob Schlather

unread,
Jun 12, 2025, 12:28:19 AMJun 12
to libphonenumber-discuss
We had an issue today with contention on the RegexCache in of our services. If you get past the 100 max size, it can become quite a bottleneck on throughput. I was wondering if a PR to make this configurable in some way would be welcome. Ideally, I would add a service loader mechanism to allow the user to pass a more robust (lock less) LRU implementation such as a caffeine cache. Environment variables or Dargs are also possible, but less appealing. Static swap out methods could also work, but are a bit messy. Here's a sample of our thread dump analysis for one of the impacted services.

⚠️ Lock Contention Detected ⚠️

Found 1 lock(s) with threads in BLOCKED state:

Lock 0x00000006c74d0480

Threads waiting for lock: 81

Lock owner: dw-529 - GET [Redacted]

Lock Owner's Stack Trace
    at java.util.LinkedHashMap.get(java...@21.0.7/LinkedHashMap.java:539)<
at com.google.i18n.phonenumbers.internal.RegexCache$LRUCache.get(RegexCache.java:67)
- locked <0x00000006c74d0480> (a com.google.i18n.phonenumbers.internal.RegexCache$LRUCache)
at com.google.i18n.phonenumbers.internal.RegexCache.getPatternForRegex(RegexCache.java:36)
at com.google.i18n.phonenumbers.internal.RegexBasedMatcher.matchNationalNumber(RegexBasedMatcher.java:46)
at com.google.i18n.phonenumbers.PhoneNumberUtil.isNumberMatchingDesc(PhoneNumberUtil.java:2361)
at com.google.i18n.phonenumbers.PhoneNumberUtil.getNumberTypeHelper(PhoneNumberUtil.java:2295)
at com.google.i18n.phonenumbers.PhoneNumberUtil.isValidNumberForRegion(PhoneNumberUtil.java:2407)
at com.hubspot.phone.number.utils.FormattedPhoneNumber.validateRegion(FormattedPhoneNumber.java:124)

Example waiting thread: dw-477 - GET [Redacted]

Waiting Thread Stack Trace

    at com.google.i18n.phonenumbers.internal.RegexCache$LRUCache.get(RegexCache.java:67)
- waiting to lock <0x00000006c74d0480> (a com.google.i18n.phonenumbers.internal.RegexCache$LRUCache)
at com.google.i18n.phonenumbers.internal.RegexCache.getPatternForRegex(RegexCache.java:36)
at com.google.i18n.phonenumbers.internal.RegexBasedMatcher.matchNationalNumber(RegexBasedMatcher.java:46)
at com.google.i18n.phonenumbers.PhoneNumberUtil.isNumberMatchingDesc(PhoneNumberUtil.java:2361)
at com.google.i18n.phonenumbers.PhoneNumberUtil.getNumberTypeHelper(PhoneNumberUtil.java:2270)
at com.google.i18n.phonenumbers.PhoneNumberUtil.getNumberType(PhoneNumberUtil.java:2262)
at com.hubspot.phone.number.utils.FormattedPhoneNumber.<init>(FormattedPhoneNumber.java:81)

Tijana

unread,
Jun 12, 2025, 4:33:39 AMJun 12
to libphonenumber-discuss
Hello, thank you for your willingness to contribute to LibPhoneNumber!

We have had this issue already brought up a while ago. Could you try and see if the solution proposed there works for you? If so, we are happy to accept such a contribution (TL;DR configurable cache size).
If you would still like to use a different cache implementation, I think the simplest way forward would be to introduce a new abstraction for the cache and add an overload of PhoneNumberUtil#createInstance which would accept any implementation of your choice.

Jacob Schlather

unread,
Jun 13, 2025, 6:33:20 AMJun 13
to libphonenumber-discuss
Hey, the linked issue is in google corp, so I can't access it. But, I'd be happy to create a PR that adds an overload of PhoneNumberUtil.createInstance which takes in a custom regex cache impl.

Tijana Vislavski Gradina

unread,
Jun 16, 2025, 8:34:18 AMJun 16
to libphonenum...@googlegroups.com
Sorry, my bad. Here is the public link. Thank you once again for willing to contribute to the library.

--
You received this message because you are subscribed to a topic in the Google Groups "libphonenumber-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/libphonenumber-discuss/5Im4ef_JyDw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to libphonenumber-di...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/libphonenumber-discuss/4f9f2ca3-3dca-4568-981d-3f852ef7c94an%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages