class Opal::Rewriters::TargetedPatches
def on_array(node)
def on_array(node) children = node.children # Optimize large arrays produced by lexer, but mainly we are interested # in improving compile times, by reducing the tree for the further # compilation efforts (also reducing the bundle size a bit) # # This particular patch reduces compile time of the following command # by 12.5%: # # OPAL_CACHE_DISABLE=true OPAL_PREFORK_DISABLE=true bin/opal \ # --no-source-map -ropal-parser -ropal/platform -ce \ # 'puts ::Opal.compile($stdin.read)' > _Cnow.js # # So, in short, an array of a kind: # # [1, 2, 3, nil, nil, :something, :abc, nil, ...] # # Becomes compiled to: # # Opal.large_array_unpack("1,2,3,,something,abc,,...") if children.length > 32 ssin_array = children.all? do |child| # Break for wrong types next false unless %i[str sym int nil].include?(child.type) # Break for strings that may conflict with our numbers, nils and separator next false if %i[str sym].include?(child.type) && child.children.first.to_s =~ /\A[0-9-]|\A\z|,/ # Break for too numbers out of range, as there may be decoding issues next false if child.type == :int && !(-1_000_000..1_000_000).cover?(child.children.first) true end if ssin_array str = children.map { |i| i.children.first.to_s }.join(',') node.updated(:jscall, [s(:js_tmp, :Opal), :large_array_unpack, s(:sym, str)]) else super end else super end end