Skip to content

Commit

Permalink
[CELEBORN-1448] Use static regex Pattern instances in JavaUtils.timeS…
Browse files Browse the repository at this point in the history
…tringAs and JavaUtils.byteStringAs

### What changes were proposed in this pull request?

### Why are the changes needed?

[SPARK-48496](https://issues.apache.org/jira/browse/SPARK-48496)[CORE] Use static regex Pattern instances in JavaUtils.timeStringAs and JavaUtils.byteStringAs

### Does this PR introduce _any_ user-facing change?
No

### How was this patch tested?
GA

Closes #2541 from cxzl25/CELEBORN-1448.

Authored-by: sychen <[email protected]>
Signed-off-by: Fu Chen <[email protected]>
(cherry picked from commit dc2826a)
Signed-off-by: Fu Chen <[email protected]>
  • Loading branch information
cxzl25 authored and cfmcgrady committed Jun 3, 2024
1 parent 794ca7e commit 1a9205b
Showing 1 changed file with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ private static boolean isSymlink(File file) throws IOException {
.put("pi", ByteUnit.PiB)
.build();

private static final Pattern TIME_STRING_PATTERN = Pattern.compile("(-?[0-9]+)([a-z]+)?");

/**
* Convert a passed time string (e.g. 50s, 100ms, or 250us) to a time count in the given unit. The
* unit is also considered the default if the given string does not specify a unit.
Expand All @@ -250,7 +252,7 @@ public static long timeStringAs(String str, TimeUnit unit) {
String lower = str.toLowerCase(Locale.ROOT).trim();

try {
Matcher m = Pattern.compile("(-?[0-9]+)([a-z]+)?").matcher(lower);
Matcher m = TIME_STRING_PATTERN.matcher(lower);
if (!m.matches()) {
throw new NumberFormatException("Failed to parse time string: " + str);
}
Expand Down Expand Up @@ -291,6 +293,10 @@ public static long timeStringAsSec(String str) {
return timeStringAs(str, TimeUnit.SECONDS);
}

private static final Pattern BYTE_STRING_PATTERN = Pattern.compile("([0-9]+)([a-z]+)?");
private static final Pattern BYTE_STRING_FRACTION_PATTERN =
Pattern.compile("([0-9]+\\.[0-9]+)([a-z]+)?");

/**
* Convert a passed byte string (e.g. 50b, 100kb, or 250mb) to the given. If no suffix is
* provided, a direct conversion to the provided unit is attempted.
Expand All @@ -299,8 +305,8 @@ public static long byteStringAs(String str, ByteUnit unit) {
String lower = str.toLowerCase(Locale.ROOT).trim();

try {
Matcher m = Pattern.compile("([0-9]+)([a-z]+)?").matcher(lower);
Matcher fractionMatcher = Pattern.compile("([0-9]+\\.[0-9]+)([a-z]+)?").matcher(lower);
Matcher m = BYTE_STRING_PATTERN.matcher(lower);
Matcher fractionMatcher = BYTE_STRING_FRACTION_PATTERN.matcher(lower);

if (m.matches()) {
long val = Long.parseLong(m.group(1));
Expand Down

0 comments on commit 1a9205b

Please sign in to comment.