Skip to content

Commit

Permalink
Speedup
Browse files Browse the repository at this point in the history
  • Loading branch information
eoftedal committed Feb 5, 2024
1 parent 10c57b7 commit 077b787
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [1.0.0-beta.2] - 2024-02-05

### Fixes

* Performance update

## [1.0.0-beta.1] - 2024-02-01

Fixing build
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"repository": {
"type": "git",
"url": "https://github.com/eoftedal/babel-q.git"
"url": "https://github.com/RetireJS/babel-q.git"
},
"author": "Erlend Oftedal <[email protected]>",
"license": "Apache-2.0",
Expand Down
25 changes: 13 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type State = {
child: FNode[][];
descendant: FNode[][];
filters: FilterResult[][];
matches: [FNode, NodePath<Babel.Node>][][];
}
type FilterCondition = {
type: "and" | "or" | "equals";
Expand Down Expand Up @@ -129,6 +130,7 @@ function isMatch(fnode: FNode, path: NodePath<Babel.Node>) : boolean {

function addIfTokenMatch(fnode: FNode, path: NodePath<Babel.Node>, state: State) {
if (!isMatch(fnode, path)) return;
state.matches[state.depth].push([fnode, path]);
if (fnode.node.filter) {
const filter = createFilter(fnode.node.filter, []);
const filteredResult: Array<Result> = [];
Expand All @@ -149,10 +151,9 @@ function isPrimitive(value: unknown) : boolean {
}

function addPrimitiveAttributeIfMatch(fnode: FNode, path: NodePath<Babel.Node>) {
if (!fnode.node.attribute) return;
if (!fnode.node.value) return;
if (fnode.node.child) return;
if (fnode.node.filter) return;
if (!fnode.node.attribute || !fnode.node.value) return;
if (fnode.node.child || fnode.node.filter) return;
if (!Object.hasOwn(path.node, fnode.node.value)) return;
const lookup = path.get(fnode.node.value);
const nodes = (Array.isArray(lookup) ? lookup : [lookup])
.filter(n => n.node != undefined)
Expand Down Expand Up @@ -245,11 +246,9 @@ function resolveDirectly(node: QNode, path: NodePath<Babel.Node>) : Result[] {
}

function addResultIfTokenMatch(fnode: FNode, path: NodePath<Babel.Node>, state: State) {
if (!isMatch(fnode, path)) return;
const filters = state.filters[state.depth].filter(f => f.node == path.node && f.qNode == fnode.node);
const matchingFilters = filters.filter(f => evaluateFilter(f.filter, path).length > 0);
log.debug("RESULT MATCH", fnode.node.value, breadCrumb(path), filters.length, matchingFilters.length);
if (filters.length > 0) log.debug("HOLA FILTROS", filters, matchingFilters);
if (fnode.node.child) {
if (fnode.node.binding && (filters.length == 0 || matchingFilters.length > 0)) {
const binding = resolveBinding(path);
Expand Down Expand Up @@ -281,7 +280,8 @@ function travHandle<T extends Record<string, QNode>>(queries: T, root: NodePath<
depth: 0,
child: [[],[]],
descendant: [[],[]],
filters: [[],[]]
filters: [[],[]],
matches: [[]]
};
Object.entries(queries).forEach(([name, node]) => {
createFNodeAndAddToState(node, results[name], state);
Expand All @@ -296,25 +296,26 @@ function travHandle<T extends Record<string, QNode>>(queries: T, root: NodePath<
state.child.push([]);
state.descendant.push([]);
state.filters.push([]);
state.matches.push([]);
state.child[state.depth].forEach(fnode => addIfTokenMatch(fnode, path, state));
state.descendant.slice(0, state.depth+1).forEach(fnodes =>
fnodes.forEach(fnode => addIfTokenMatch(fnode, path, state))
);
},
exit(path, state) {
log.debug("EXIT", breadCrumb(path));
// Check for attributes as not all attributes are visited
state.child[state.depth +1].forEach(fnode => addPrimitiveAttributeIfMatch(fnode, path));
state.descendant.slice(0, state.depth+2).forEach(fnodes =>
state.descendant.forEach(fnodes =>
fnodes.forEach(fnode => addPrimitiveAttributeIfMatch(fnode, path))
);
state.child[state.depth].forEach(fnode => addResultIfTokenMatch(fnode, path, state));
state.descendant.slice(0, state.depth+1).forEach(fnodes =>
fnodes.forEach(fnode => addResultIfTokenMatch(fnode, path, state))
);

state.matches[state.depth].forEach(([fNode, path]) => addResultIfTokenMatch(fNode, path, state));
state.depth--;
state.child.pop();
state.descendant.pop();
state.filters.pop();
state.matches.pop();
}
}, root.scope, state, root);
return results;
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"target": "ES2016",
"lib" : [ "ES2022" ],
"target": "ES2022",
"module": "commonjs",
"declaration": true,
"outDir": "./lib",
Expand Down

0 comments on commit 077b787

Please sign in to comment.