Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

true/false inlining can waste chars #39

Open
pancelor opened this issue May 11, 2024 · 2 comments
Open

true/false inlining can waste chars #39

pancelor opened this issue May 11, 2024 · 2 comments

Comments

@pancelor
Copy link
Contributor

semi-related to #38

These are both longer (in char-count) than the input:

> echo 'a=0<1' | shrinko8 - - -m --focus-chars -f lua
r=true
> echo 'a=1<0' | shrinko8 - - -m --focus-chars -f lua
f=false

workaround: compare a builtin symbol instead, since shrinko8 isn't sure whether its a constant or not:

> echo 'a=◆<0' | shrinko8 - - -m --focus-chars -f lua
a=◆<0

Veering out of bug-report territory and into feature-request territory: shrinko8 could automatically replace "true" and "false" with "0<1" and "1<0", which saves about 2~3 chars each time (since the trailing space can often be removed when merging statements together). I don't know if this idea is in-scope for the tool or not, but it's an interesting idea

@thisismypassport
Copy link
Owner

About the bug - yeah, it's the same issue as in #38 - I'll probably fix it by avoiding doing const replacement in focus-chars mode if the result is longer than the original tokens (though there are edge cases relating to const vars and to the possibility that even though parts of an expression are longer in constant form, the whole expression is shorter in constant form)

About the feature - hmm... So far I never adding tokens as part of minification, and I'm not sure that would be expected, even though --focus-tokens isn't provided. Maybe under some kind of --ignore-tokens option?
Do you know other such save-chars-at-the-cost-of-tokens tricks?

@pancelor
Copy link
Contributor Author

pancelor commented May 18, 2024

avoiding doing const replacement in focus-chars mode if the result is longer than the original tokens

Perfect, focus-chars-only is exactly what I would expect here

Oh, true=>0<1 adds extra tokens! very true and not good; I didn't even think about that haha (I've been in character-counting mode for the last few weeks). maybe only do this replacement iff the user asked for --focus-chars but did not ask for --focus-tokens? bah, it feels like a gross mess that users wouldn't be able to predict.

An alternate idea might be a new pause/unpause directive; something like: a=--[[shrinko8-pause]]0<1--[[shrinko8-unpause]]. Some sort of fined-grain control like this, although this exact suggestion feels a bit lacking, since it would only(?) work to prevent constant inlining...

> Do you know other such save-chars-at-the-cost-of-tokens tricks?

This was a fun exercise to think through! Here's a few I came up with just now:

1

if(not x) => if(x==a) saves one character, if a is unassigned/nil and the programmer is intending to only check for nil (i.e. they know x won't be false)

2

any output of my constant companion tool, e.g. replacing 0x6000 with ◆-♥, or replacing 0x7fff.ffff with 1/0. Or replacing 0xffff.ffff with ~0

3

I just thought up this toy example, probably not useful to you tho:

--28 tokens, 55 chars
function f(x) return x*2*3*4*5*6*7 end
a=f(10)
b=f(20)
--30 tokens, 33 chars
a=10*2*3*4*5*6*7
b=20*2*3*4*5*6*7

4

I suppose the tweetcart standard of using ::_::cls() flip()goto _ instead of using function _draw() end is another example (even when using the p8scii version of flip()cls())

5

similar to 3 but maybe more useful for shrinko8: creating an alias for an often-used variable or constant: u=unpack ... x=u(c) ... y=u(c) or e=128 .... rect(0,0,e,e) ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants