Skip to content

Commit 941d9c7

Browse files
committed
refactor: use computeIfAbsent for pattern cache lookup
Atomic and shorter. Avoids two threads compiling the same pattern under contention.
1 parent 250b469 commit 941d9c7

6 files changed

Lines changed: 6 additions & 54 deletions

File tree

common/src/main/scala/org/apache/comet/udf/RegExpExtractAllUDF.scala

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,7 @@ class RegExpExtractAllUDF extends CometUDF {
5959
"RegExpExtractAllUDF requires a non-null scalar group index")
6060

6161
val patternStr = new String(patternVec.get(0), StandardCharsets.UTF_8)
62-
val pattern = {
63-
val cached = patternCache.get(patternStr)
64-
if (cached != null) cached
65-
else {
66-
val compiled = Pattern.compile(patternStr)
67-
patternCache.put(patternStr, compiled)
68-
compiled
69-
}
70-
}
62+
val pattern = patternCache.computeIfAbsent(patternStr, Pattern.compile)
7163
val idx = idxVec.get(0)
7264

7365
val n = subject.getValueCount

common/src/main/scala/org/apache/comet/udf/RegExpExtractUDF.scala

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,7 @@ class RegExpExtractUDF extends CometUDF {
5757
"RegExpExtractUDF requires a non-null scalar group index")
5858

5959
val patternStr = new String(patternVec.get(0), StandardCharsets.UTF_8)
60-
val pattern = {
61-
val cached = patternCache.get(patternStr)
62-
if (cached != null) cached
63-
else {
64-
val compiled = Pattern.compile(patternStr)
65-
patternCache.put(patternStr, compiled)
66-
compiled
67-
}
68-
}
60+
val pattern = patternCache.computeIfAbsent(patternStr, Pattern.compile)
6961
val idx = idxVec.get(0)
7062

7163
val n = subject.getValueCount

common/src/main/scala/org/apache/comet/udf/RegExpInStrUDF.scala

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,7 @@ class RegExpInStrUDF extends CometUDF {
5757
"RegExpInStrUDF requires a non-null scalar group index")
5858

5959
val patternStr = new String(patternVec.get(0), StandardCharsets.UTF_8)
60-
val pattern = {
61-
val cached = patternCache.get(patternStr)
62-
if (cached != null) cached
63-
else {
64-
val compiled = Pattern.compile(patternStr)
65-
patternCache.put(patternStr, compiled)
66-
compiled
67-
}
68-
}
60+
val pattern = patternCache.computeIfAbsent(patternStr, Pattern.compile)
6961
val idx = idxVec.get(0)
7062

7163
val n = subject.getValueCount

common/src/main/scala/org/apache/comet/udf/RegExpLikeUDF.scala

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,7 @@ class RegExpLikeUDF extends CometUDF {
4949
"RegExpLikeUDF requires a non-null scalar pattern")
5050

5151
val patternStr = new String(patternVec.get(0), StandardCharsets.UTF_8)
52-
val pattern = {
53-
val cached = patternCache.get(patternStr)
54-
if (cached != null) cached
55-
else {
56-
val compiled = Pattern.compile(patternStr)
57-
patternCache.put(patternStr, compiled)
58-
compiled
59-
}
60-
}
52+
val pattern = patternCache.computeIfAbsent(patternStr, Pattern.compile)
6153

6254
val n = subject.getValueCount
6355
val out = new BitVector("rlike_result", CometArrowAllocator)

common/src/main/scala/org/apache/comet/udf/RegExpReplaceUDF.scala

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,7 @@ class RegExpReplaceUDF extends CometUDF {
5656
"RegExpReplaceUDF requires a non-null scalar replacement")
5757

5858
val patternStr = new String(patternVec.get(0), StandardCharsets.UTF_8)
59-
val pattern = {
60-
val cached = patternCache.get(patternStr)
61-
if (cached != null) cached
62-
else {
63-
val compiled = Pattern.compile(patternStr)
64-
patternCache.put(patternStr, compiled)
65-
compiled
66-
}
67-
}
59+
val pattern = patternCache.computeIfAbsent(patternStr, Pattern.compile)
6860
val replacement = new String(replacementVec.get(0), StandardCharsets.UTF_8)
6961

7062
val n = subject.getValueCount

common/src/main/scala/org/apache/comet/udf/StringSplitUDF.scala

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,7 @@ class StringSplitUDF extends CometUDF {
5959
"StringSplitUDF requires a non-null scalar limit")
6060

6161
val patternStr = new String(patternVec.get(0), StandardCharsets.UTF_8)
62-
val pattern = {
63-
val cached = patternCache.get(patternStr)
64-
if (cached != null) cached
65-
else {
66-
val compiled = Pattern.compile(patternStr)
67-
patternCache.put(patternStr, compiled)
68-
compiled
69-
}
70-
}
62+
val pattern = patternCache.computeIfAbsent(patternStr, Pattern.compile)
7163
val limit = limitVec.get(0)
7264

7365
val n = subject.getValueCount

0 commit comments

Comments
 (0)