Skip to content

Commit

Permalink
optimize MAP (do_list). Speeds up compress by a couple %
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Feb 4, 2023
1 parent 627a433 commit 8dc5fc2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 43 deletions.
2 changes: 1 addition & 1 deletion lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class AST_Token {
// Return a string summary of the token for node.js console.log
[Symbol.for("nodejs.util.inspect.custom")](_depth, options) {
const special = str => options.stylize(str, "special");
const quote = this.value.includes("`") ? "'" : "`";
const quote = typeof this.value === "string" && this.value.includes("`") ? "'" : "`";
const value = `${quote}${this.value}${quote}`;
return `${special("[AST_Token")} ${value} at ${this.line}:${this.col}${special("]")}`;
}
Expand Down
8 changes: 1 addition & 7 deletions lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ import {
AST_Yield,
} from "./ast.js";
import {
MAP,
MAP as do_list,
noop,
} from "./utils/index.js";

Expand All @@ -111,12 +111,6 @@ function def_transform(node, descend) {
});
}

function do_list(list, tw) {
return MAP(list, function(node) {
return node.transform(tw, true);
});
}

def_transform(AST_Node, noop);

def_transform(AST_LabeledStatement, function(self, tw) {
Expand Down
66 changes: 31 additions & 35 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

"use strict";

import { AST_Node } from "../ast.js";

function characters(str) {
return str.split("");
}
Expand Down Expand Up @@ -96,48 +98,42 @@ function return_this() { return this; }
function return_null() { return null; }

var MAP = (function() {
function MAP(a, f, backwards) {
var ret = [], top = [], i;
function doit() {
var val = f(a[i], i);
var is_last = val instanceof Last;
if (is_last) val = val.v;
if (val instanceof AtTop) {
val = val.v;
if (val instanceof Splice) {
top.push.apply(top, backwards ? val.v.slice().reverse() : val.v);
} else {
top.push(val);
function MAP(a, tw) {
// Loop but try not to build a new array
for (let i = 0; i < a.length; ++i) {
let item = a[i];
let ret = item.transform(tw, true);

if (ret !== item) {
const a1 = a.slice(0, i);

// Looks like something was transformed. Change the loop.
if (ret instanceof AST_Node) {
a1.push(ret);
} else if (ret instanceof Splice) {
a1.push(...ret.v);
}
} else if (val !== skip) {
if (val instanceof Splice) {
ret.push.apply(ret, backwards ? val.v.slice().reverse() : val.v);
} else {
ret.push(val);

while ((item = a[++i])) {
const ret = item.transform(tw, true);

if (ret instanceof AST_Node) {
a1.push(ret);
} else if (ret instanceof Splice) {
a1.push(...ret.v);
}
}

return a1;
}
return is_last;
}
if (Array.isArray(a)) {
if (backwards) {
for (i = a.length; --i >= 0;) if (doit()) break;
ret.reverse();
top.reverse();
} else {
for (i = 0; i < a.length; ++i) if (doit()) break;
}
} else {
for (i in a) if (HOP(a, i)) if (doit()) break;
}
return top.concat(ret);

return a;
}
MAP.at_top = function(val) { return new AtTop(val); };

MAP.splice = function(val) { return new Splice(val); };
MAP.last = function(val) { return new Last(val); };
var skip = MAP.skip = {};
function AtTop(val) { this.v = val; }
MAP.skip = {};
function Splice(val) { this.v = val; }
function Last(val) { this.v = val; }
return MAP;
})();

Expand Down

0 comments on commit 8dc5fc2

Please sign in to comment.