Skip to content

Commit f09f927

Browse files
committed
perf(function): cache ValueFactory regexes
1 parent a46d418 commit f09f927

File tree

1 file changed

+13
-2
lines changed
  • quarkdown-core/src/main/kotlin/com/quarkdown/core/function/value/factory

1 file changed

+13
-2
lines changed

quarkdown-core/src/main/kotlin/com/quarkdown/core/function/value/factory/ValueFactory.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,24 @@ import com.quarkdown.core.pipeline.stages.ParsingStage
6262
import com.quarkdown.core.util.iterator
6363
import com.quarkdown.core.util.node.conversion.list.MarkdownListToCollectionValue
6464
import com.quarkdown.core.util.node.conversion.list.MarkdownListToDictionaryValue
65+
import kotlin.collections.map
6566

6667
/**
6768
* Prefix that forces a generic expression to be parsed as a lambda block.
6869
* @see ValueFactory.expression
6970
*/
7071
private const val EXPRESSION_FORCE_LAMBDA_PREFIX = "@lambda "
7172

73+
/**
74+
* Pre-compiled regex for stripping comments from expressions.
75+
*/
76+
private val COMMENT_REGEX = PatternHelpers.COMMENT.toRegex()
77+
78+
/**
79+
* Pre-compiled regex for splitting whitespace-separated size values.
80+
*/
81+
private val WHITESPACE_REGEX = "\\s+".toRegex()
82+
7283
/**
7384
* Factory of [Value] wrappers from raw data.
7485
* @see com.quarkdown.core.function.reflect.FromDynamicType
@@ -213,7 +224,7 @@ object ValueFactory {
213224
fun sizes(raw: Any): ObjectValue<Sizes> {
214225
if (raw is Sizes) return ObjectValue(raw)
215226

216-
val parts = raw.toString().split("\\s+".toRegex())
227+
val parts = raw.toString().split(WHITESPACE_REGEX)
217228
val iterator = parts.iterator()
218229

219230
return ObjectValue(
@@ -541,7 +552,7 @@ object ValueFactory {
541552
}
542553

543554
// Strip comments.
544-
val rawCode = raw.toString().replace(PatternHelpers.COMMENT.toRegex(), "")
555+
val rawCode = raw.toString().replace(COMMENT_REGEX, "")
545556

546557
if (rawCode.isEmpty()) return DynamicValue("")
547558

0 commit comments

Comments
 (0)