Skip to content

Commit

Permalink
Added a very interesting TODO in the rabbit "hole"
Browse files Browse the repository at this point in the history
  • Loading branch information
WebReflection committed Jan 5, 2024
1 parent bc0a0ee commit 5c52d6d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion esm/literals.js
Expand Up @@ -74,7 +74,7 @@ export const entry = (t, p, u, n = '') => ({ t, p, u, n });
* @param {Cache[]} s the cache stack
* @returns {Cache}
*/
export const cache = s => ({ s, t: null, n: null, d: empty});
export const cache = s => ({ s, a: false, t: null, n: null, d: empty});

/**
* @typedef {Object} Parsed
Expand Down
24 changes: 17 additions & 7 deletions esm/rabbit.js
Expand Up @@ -6,23 +6,34 @@ import parser from './parser.js';
const parseHTML = create(parser(false));
const parseSVG = create(parser(true));

// TODO: check twice for isArray or check attributes
// makes literally no sense and it's performance
// critical. The template details know all of this
// so there's gonna be a way to drill those details
// down this loop, avoiding any unnecessary check.

/**
* @param {import("./literals.js").Cache} cache
* @param {Hole} hole
* @returns {Node}
*/
export const unroll = (cache, { s: svg, t: template, v: values }) => {
if (values.length && cache.s === empty) cache.s = [];
const length = unrollValues(cache, values);
if (cache.s === empty && values.length) cache.s = [];
if (cache.t !== template) {
// this is necessary only on first time ... after that there are details
unrollValues(cache, values);
const { n: node, d: details } = (svg ? parseSVG : parseHTML)(template, values);
cache.t = template;
cache.n = node;
cache.d = details;
}
else {
// this makes no sense here ... it could be just one loop
unrollValues(cache, values);
const { d: details } = cache;
for (let i = 0; i < length; i++) {
// this loop could resolve values[i] in a go
// making the need to loop more than once unnecessary
for (let i = 0; i < values.length; i++) {
const value = values[i];
const detail = details[i];
const { v: previous } = detail;
Expand All @@ -41,8 +52,7 @@ export const unroll = (cache, { s: svg, t: template, v: values }) => {
* @returns {number}
*/
const unrollValues = ({ s: stack }, values) => {
const { length } = values;
for (let i = 0; i < length; i++) {
for (let i = 0; i < values.length; i++) {
const hole = values[i];
if (hole instanceof Hole)
values[i] = unroll(stack[i] || (stack[i] = cache(empty)), hole);
Expand All @@ -51,8 +61,8 @@ const unrollValues = ({ s: stack }, values) => {
else
stack[i] = null;
}
if (length < stack.length) stack.splice(length);
return length;
if (values.length < stack.length)
stack.splice(values.length);
};

/**
Expand Down
6 changes: 3 additions & 3 deletions signal.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5c52d6d

Please sign in to comment.