awesome-print / awesome_print

Pretty print your Ruby objects with style -- in full color and with proper indentation
http://github.com/michaeldv/awesome_print
MIT License
4.07k stars 454 forks source link

Redundant string allocation #213

Open kikonen opened 8 years ago

kikonen commented 8 years ago

For example, following piece of logic seems to trigger lots of allocations of '@awesome_methods' string. Perhaps should use '@awesome_methods'.freeze instead. Also seemingly other extra alloc happens.

This is inconvenient, since extra overhead is added into every execution of "-" and "&" operators in arrays.

class Array #:nodoc:
  [ :-, :& ].each do |operator|
    original_operator = instance_method(operator)

    define_method operator do |*args|
      arr = original_operator.bind(self).call(*args)
      if self.instance_variable_defined?('@__awesome_methods__')
        arr.instance_variable_set('@__awesome_methods__', self.instance_variable_get('@__awesome_methods__'))
        arr.sort! { |a, b| a.to_s <=> b.to_s }  # Need the block since Ruby 1.8.x can't sort arrays of symbols.
      end
      arr
    end
  end
gerrywastaken commented 8 years ago

@kikonen Any chance of some profiling on a PR. The problem you are talking about isn't immediately obvious to me, but some profiling would probably help.

kikonen commented 8 years ago

Not recalling exactly. Recalling this was causing significant overhead on "-" and "&" operations on lists; basically meaning that awesome_print gem must be ensured to be not included in Gemfile in non development setup (I've taken that approach).

gerrywastaken commented 8 years ago

Ok well I'll leave this open for now until somebody provides some benchmarks or I have time to look into it myself and test things.

dgynn commented 8 years ago

I've just added PR #264 which addresses this issue and includes benchmarks. I meant to reference this issue in the commit.