Skip to content

Commit 708db56

Browse files
committed
perf(toRegExp): fewer unnecessry parenthesis
1 parent 1d77393 commit 708db56

11 files changed

+52
-45
lines changed

benchmark/aoc2023-day12-result.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

2-
Part 1: 7191 (time: 907ms)
3-
Part 2: 6512849198636 (time: 11005ms)
2+
Part 1: 7191 (time: 973ms)
3+
Part 2: 6512849198636 (time: 11618ms)

benchmark/aoc2023-day12.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'fs'
2-
import { RB } from '../dist/index.js'
3-
import { assert } from '../dist/utils.js'
2+
import { RB } from '../src/index'
3+
import { assert } from '../src/utils'
44

55
const input = fs.readFileSync('./benchmark/aoc2023-day12_input.txt', 'utf-8')
66
.trim()

benchmark/demo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RB } from '../dist/index.js'
1+
import { RB } from '../src/index'
22

33
const passwordRegex = RB(/^[a-zA-Z0-9]{12,32}$/) // 12-32 alphanumeric characters
44
.and(/[0-9]/) // at least one number

benchmark/filter-vs-intersection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fc from 'fast-check'
2-
import { RB } from '../dist/index.js'
2+
import { RB } from '../src/index'
33

44
const emailRegex = /^[\w\-\.]+@([\w-]+\.)+[\w-]{2,}$/
55

benchmark/parser-bench-result.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

22
error ratio : 17 / 749
3-
total parse time : 8848ms
4-
avg parse time : 12.087431693989071ms
5-
max parse time : 1380ms
3+
total parse time : 5908ms
4+
avg parse time : 8.07103825136612ms
5+
max parse time : 919ms

benchmark/parser-bench.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs'
2-
import { parseRegExp } from '../dist/regex-parser.js'
2+
import { parseRegExp } from '../src/regex-parser'
33

44
export function* readDataset() {
55
const jsonStr = fs.readFileSync('./benchmark/regex-dataset.json', 'utf-8')

benchmark/toStdRegex_output_length-result.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ failed instances:
77
- regexSyntaxError : 0
88

99
size multipliers:
10-
- mean : 323.9034830660257
11-
- median : 1.2692307692307692
12-
- max : 178441.7894736842
10+
- mean : 323.8333568093031
11+
- median : 1.1923076923076923
12+
- max : 178441.75438596492
13+
- min : 0.0021570319240724763
1314

1415
memory:
15-
- max heap used : 2077.4215393066406 MB
16-
- max rss : 3073.03125 MB
16+
- max heap used : 2370.280075073242 MB
17+
- max rss : 2722.53515625 MB

benchmark/toStdRegex_output_length.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ fullRegexDataset.forEach((regex, i) => {
7272
const mean = mults.reduce((a,b) => a+b, 0) / mults.length
7373
const median = mults[Math.ceil(mults.length / 2)]
7474
const worst = mults.reduce((a,b) => Math.max(a,b), -Infinity)
75+
const best = mults.reduce((a,b) => Math.min(a,b), Infinity)
7576

7677
const summary = `
7778
failed instances:
@@ -85,6 +86,7 @@ size multipliers:
8586
- mean : ${mean}
8687
- median : ${median}
8788
- max : ${worst}
89+
- min : ${best}
8890
8991
memory:
9092
- max heap used : ${maxHeapUsed/1024**2} MB

src/ast.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ const needsNoParensOnSamePrecLevel = new Set([
668668
function maybeWithParens(ast: RegExpAST, parent: RegExpAST, options: RenderOptions): string {
669669
if (precLevel(ast.type) > precLevel(parent.type))
670670
return toString(ast, options)
671-
else if (ast.type === parent.type && needsNoParensOnSamePrecLevel.has(ast.type))
671+
else if (precLevel(ast.type) === precLevel(parent.type) && needsNoParensOnSamePrecLevel.has(ast.type))
672672
return toString(ast, options)
673673
else if (options.useNonCapturingGroups)
674674
return '(?:' + toString(ast, options) + ')'

src/regex.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,8 @@ export function toString(regex: StdRegex): string {
693693
// but at large sizes like this it hardly hurts readability anymore:
694694
const useNonCapturingGroups = size > 10_000
695695

696-
return '^(' + AST.toString(toRegExpAST(regex), { useNonCapturingGroups }) + ')$'
696+
const ast = AST.startAnchor(undefined, AST.endAnchor(toRegExpAST(regex), undefined))
697+
return AST.toString(ast, { useNonCapturingGroups })
697698
}
698699

699700
// TODO:

0 commit comments

Comments
 (0)