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

Reduce size of compiled code skipping unused helpers #2038

Merged
merged 4 commits into from
Dec 18, 2019

Conversation

elia
Copy link
Member

@elia elia commented Dec 16, 2019

  • Use magic-comments to require helpers, skip them otherwise (break, slice)
  • Reduce the size of the constant getters Opal property name

I attempted to make nil into an optional helper, but it's so ubiquitous that I eventually gave up.

Allowing magic comments to have complex values will make the good
vehicles for method names and other stuff.
@elia elia added compiler runtime compiled-size Anything related to the size of the compiled output labels Dec 16, 2019
@elia elia requested a review from iliabylich December 16, 2019 22:58
@elia elia self-assigned this Dec 16, 2019
Copy link
Contributor

@iliabylich iliabylich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

lib/opal/nodes/top.rb Outdated Show resolved Hide resolved
@@ -1,8 +1,8 @@
# frozen_string_literal: true

module Opal::MagicComments
MAGIC_COMMENT_RE = /\A# *(\w+) *: *(\w+) *$/.freeze
EMACS_MAGIC_COMMENT_RE = /\A# *-\*- *(\w+) *: *(\w+) *-\*- *$/.freeze
MAGIC_COMMENT_RE = /\A# *(\w+) *: *(\S+.*?) *$/.freeze
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignore this comment if I'm overthinking, but the idea of using magic comments feels cryptic to me. Comments shouldn't be used to change behavior of your code. However Ruby does it, so I don't know if it's ok or not.

Also I think I'd personally expect these comments (if they are valuable at all) to be used as some kind of meta-information about the file (like by some external analyzer, rubocop is an example), and so I'd expect that I can safely remove them.

Is it that much worse writing var <helper> = Opal.<helper> at the beginning of the file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way I see these helpers is that they're mostly for core stuff and very unlikely to be used elsewhere (although it wouldn't be a problem per se). Broadening the conversation to include also the use_strict comment I expect it to be transient and probably change it's default from false to true in v1.2. I personally dislike having to mark every file with frozen string literal although I like the effect.

In summary helpers should only (mostly?) live within opal core, and use_strict is transient. Hope in this perspective it becomes less of a concern.

@@ -85,6 +85,7 @@ def compile_while
end

def compile_iter
helper :slice
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh, just got the idea that shortcut is probably a better name

add_temp '$$$ = Opal.const_get_qualified'
add_temp '$$ = Opal.const_get_relative'
add_temp '$$$ = Opal.$$$'
add_temp '$$ = Opal.$$'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will make the newly generated code incompatible with the 1.0.0 runtime. I would suggest bumping the version to like 1.0.1.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahmadine you're right, actually master is targeted toward 1.1, while the 1.0 branch is 1-0-stable and that will receive minor patches and bugfixes (actually I already released 1.0.2).

That said, it should still be compatible, as the local variable names won't change, except if someone was using stuff like Opal.$$.Regexp.$$.IGNORECASE.

I'll update master to be v1.1.0.alpha shortly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't mean "API" incompatibility, but "ABI" incompatibility. Otherwise, yeah, good to know, I missed the 1.0-stable branch.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, yet, old compiled code should keep working (except for the Opal.$$.Regexp… example, which was only meant to aid debugging). Both Opal.const_get_relative and Opal.const_get_qualified are still available. Sorry if I ask again, it's because I want to be sure to really understand the implications of these changes 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New code uses $$ = Opal.$$ which, when combined with the old runtime, would be equal to $$ = undefined. And that isn't going to work, because Opal.$$ = Opal.const_get_qualified is just defined in this changeset.

I think most people wouldn't be affected, but if someone combined the prebuilt CDN "binaries" with the master code which says "1.0.0", he could be surprised.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see now, and yes I agree it shouldn't happen and it will be even less likely when I change the dev version to 1.1.0.alpha 👍

Each file can load an internal helper without requiring it to be
available for every single compiled file.
Previously those were preloaded for every file, this saves some bytes:

BEFORE:
development:  771.05KB, minified:  473.63KB, gzipped:   91.58KB

AFTER:
development:  769.25KB, minified:  472.60KB, gzipped:   91.53KB

(via bundle exec rake dist FILES=opal)
hmdne added a commit to hmdne/opal-multishake-demo that referenced this pull request Dec 17, 2019
@elia elia merged commit 951cd0b into master Dec 18, 2019
@elia elia deleted the elia/helpers-compaction branch December 18, 2019 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiled-size Anything related to the size of the compiled output compiler runtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants