Skip to content

Commit 3cd24e7

Browse files
committed
remove pointless optimization
1 parent 2623532 commit 3cd24e7

4 files changed

Lines changed: 23 additions & 66 deletions

File tree

src/match.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Router } from './index.ts';
2-
import { HANDLERS, node_compile_to_regexp, reset, type Handlers } from './tree/regex.ts';
2+
import { HANDLERS, node_compile_root_to_regexp, reset, type Handlers } from './tree/regex.ts';
33
import { linear_map_index, linear_map_swap } from './linear-map.ts';
44

55
interface Matcher0<in out T> {
@@ -93,7 +93,7 @@ export const router_compile_to_matcher = <T>(router: Router<T>): Matcher<T> => {
9393

9494
if (tree[2] !== null) {
9595
reset();
96-
regexps.push(new RegExp('^' + node_compile_to_regexp(tree[2], true)));
96+
regexps.push(new RegExp(node_compile_root_to_regexp(tree[2])));
9797
handlers.push(HANDLERS);
9898
} else {
9999
regexps.push(null);
@@ -141,7 +141,7 @@ export const router_compile_to_matcher = <T>(router: Router<T>): Matcher<T> => {
141141
regexps,
142142
handlers,
143143
matchAllStaticMap,
144-
matchAllRegExp: new RegExp('^' + node_compile_to_regexp(matchAllTree![2], true)),
144+
matchAllRegExp: new RegExp(node_compile_root_to_regexp(matchAllTree![2])),
145145
matchAllHandler: HANDLERS,
146146
};
147147
};

src/tree/jit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Tree } from './index.ts';
2-
import { node_compile_to_regexp, HANDLERS, reset } from './regex.ts';
2+
import { node_compile_root_to_regexp, HANDLERS, reset } from './regex.ts';
33

44
export const tree_compile_to_code = (
55
tree: Tree<string>,
@@ -19,7 +19,7 @@ export const tree_compile_to_code = (
1919
if (tree[2] !== null) {
2020
// console.log(JSON.stringify(tree[2]!, null, 2));
2121
reset();
22-
str += `{let ${resultId}=/^${node_compile_to_regexp(tree[2], true)}/.exec(${pathId});if(${resultId}!==null){`;
22+
str += `{let ${resultId}=/${node_compile_root_to_regexp(tree[2])}/.exec(${pathId});if(${resultId}!==null){`;
2323
for (let i = 1, hasHandler = false, startIf = `if(${resultId}[`; i < HANDLERS.length; i++)
2424
if (HANDLERS[i] !== null) {
2525
str += startIf + i + `]===""){${HANDLERS[i]}}`;

src/tree/regex.ts

Lines changed: 17 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,16 @@ export const reset = (): void => {
6262
/**
6363
* @returns pattern with additional |
6464
*/
65-
export const connect_node_compile_to_regexp = <T>(
66-
connectNode: ConnectNode<T>,
67-
earlyTerminate: boolean,
68-
): string => {
65+
export const connect_node_compile_to_regexp = (connectNode: ConnectNode<unknown>): string => {
6966
if (connectNode[0] !== null) {
7067
HANDLERS.push(connectNode[0]);
71-
return connectNode[1] === null
72-
? '()$|'
73-
: `(?:()$|${node_compile_to_regexp(connectNode[1], earlyTerminate)})|`;
68+
return connectNode[1] === null ? '()$|' : `(?:()$|${node_compile_to_regexp(connectNode[1])})|`;
7469
}
7570

76-
return node_compile_to_regexp(connectNode[1]!, earlyTerminate) + '|';
71+
return node_compile_to_regexp(connectNode[1]!) + '|';
7772
};
7873

79-
export const node_compile_to_regexp = <T>(node: Node<T>, earlyTerminate: boolean): string => {
74+
export const node_compile_to_regexp = (node: Node<unknown>): string => {
8075
let parts = '',
8176
partsCnt = 0;
8277

@@ -88,32 +83,12 @@ export const node_compile_to_regexp = <T>(node: Node<T>, earlyTerminate: boolean
8883
}
8984

9085
if (node[2] !== null)
91-
for (
92-
let i = 0,
93-
staticChildren = node[2][1],
94-
childCanEarlyTerminate =
95-
earlyTerminate &&
96-
// Ensure there's no fallback part available
97-
node[3] === null &&
98-
node[4] === null &&
99-
node[5] === null &&
100-
node[6] === null;
101-
i < staticChildren.length;
102-
i++, partsCnt++
103-
)
104-
parts += node_compile_to_regexp(staticChildren[i], childCanEarlyTerminate) + '|';
86+
for (let i = 0, staticChildren = node[2][1]; i < staticChildren.length; i++, partsCnt++)
87+
parts += node_compile_to_regexp(staticChildren[i]) + '|';
10588

10689
if (node[3] !== null) {
10790
for (
108-
let i = 0,
109-
patterns = node[3][0],
110-
connectNodes = node[3][1],
111-
childCanEarlyTerminate =
112-
earlyTerminate &&
113-
// Ensure there's no fallback part available
114-
node[4] === null &&
115-
node[5] === null &&
116-
node[6] === null;
91+
let i = 0, patterns = node[3][0], connectNodes = node[3][1];
11792
i < connectNodes.length;
11893
i++, partsCnt++
11994
) {
@@ -156,56 +131,38 @@ export const node_compile_to_regexp = <T>(node: Node<T>, earlyTerminate: boolean
156131
parts +=
157132
escapeStaticPart(pattern.slice(patternPrevIdx, patternLen)) +
158133
(hasModifier ? ')' + modifier : '') +
159-
connect_node_compile_to_regexp(connectNodes[i], childCanEarlyTerminate);
134+
connect_node_compile_to_regexp(connectNodes[i]);
160135
}
161136
}
162137

163138
if (node[4] !== null)
164139
for (
165-
let i = 0,
166-
regexps = node[4][0],
167-
connectNodes = node[4][1],
168-
childCanEarlyTerminate =
169-
earlyTerminate &&
170-
// Ensure there's no fallback part available
171-
node[5] === null &&
172-
node[6] === null;
140+
let i = 0, regexps = node[4][0], connectNodes = node[4][1];
173141
i < regexps.length;
174142
i++, partsCnt++
175143
)
176-
parts +=
177-
'(?:' +
178-
regexps[i].slice(1) +
179-
connect_node_compile_to_regexp(connectNodes[i], childCanEarlyTerminate);
144+
parts += '(?:' + regexps[i].slice(1) + connect_node_compile_to_regexp(connectNodes[i]);
180145

181146
if (node[5] !== null)
182147
for (
183-
let i = 0,
184-
keys = node[5][0],
185-
connectNodes = node[5][1],
186-
childCanEarlyTerminate =
187-
earlyTerminate &&
188-
// Ensure there's no fallback part available
189-
node[6] === null;
148+
let i = 0, keys = node[5][0], connectNodes = node[5][1];
190149
i < keys.length;
191150
i++, partsCnt++
192151
) {
193152
HANDLERS.push(null);
194153
parts +=
195154
parseNamedGroup(keys[i], 0, keys[i].length) +
196-
connect_node_compile_to_regexp(connectNodes[i], childCanEarlyTerminate);
155+
connect_node_compile_to_regexp(connectNodes[i]);
197156
}
198157

199158
if (node[6] !== null) {
200159
partsCnt++;
201-
parts += '.*' + connect_node_compile_to_regexp(node[6], earlyTerminate);
160+
parts += '.*' + connect_node_compile_to_regexp(node[6]);
202161
}
203162

204-
parts = earlyTerminate
205-
? // early terminate the pattern by having an impossible pattern
206-
'(?:' + parts + '$.)'
207-
: partsCnt > 1
208-
? '(?:' + parts.slice(0, -1) + ')'
209-
: parts.slice(0, -1);
163+
parts = partsCnt > 1 ? `(?:${parts.slice(0, -1)})` : parts.slice(0, -1);
210164
return node[0].length > 0 ? escapeStaticPart(node[0]) + parts : parts;
211165
};
166+
167+
export const node_compile_root_to_regexp = (root: Node<unknown>): string =>
168+
`^(?:${node_compile_to_regexp(root)}|$.)`;

tests/node/tree/jit.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const run = (name: string, suite: Suite) => {
2525

2626
const fn: (path: string) => { id: string; params: Record<string, string> } | undefined =
2727
Function('p', tree_compile_to_code(tree, 'r', 'p')) as any;
28-
// console.log(fn.toString());
28+
//console.log(fn.toString());
2929

3030
for (const pattern in suite) {
3131
describe(pattern, () => {

0 commit comments

Comments
 (0)