Permalink
Browse files

Reduce extra object creations in TaggedLogging

tags_text method creates 3 Ruby objects per each logger call when no custom tags are given
(which is the default setting, and so presumably the majority use case).
This patch reduces two temporary object creations in this case.

    require 'allocation_tracer'
    ObjectSpace::AllocationTracer.setup(%i{type})

    tags = ['a']

    pp before: ObjectSpace::AllocationTracer.trace {
      tags.collect { |tag| "[#{tag}] " }.join
    }

    pp after: ObjectSpace::AllocationTracer.trace {
      "[#{tags[0]}] "
    }

    {:before=>{[:T_ARRAY]=>[1, 0, 0, 0, 0, 0], [:T_STRING]=>[2, 0, 0, 0, 0, 0]}}
    {:after=>{[:T_STRING]=>[1, 0, 0, 0, 0, 0]}}
  • Loading branch information...
amatsuda committed Apr 26, 2018
1 parent 2929d16 commit ac93e7b5c192c39cfc224a1259a68c3b1e7bf0aa
Showing with 3 additions and 1 deletion.
  1. +3 −1 activesupport/lib/active_support/tagged_logging.rb
@@ -52,7 +52,9 @@ def current_tags
def tags_text
tags = current_tags
if tags.any?
if tags.one?
"[#{tags[0]}] "
elsif tags.any?
tags.collect { |tag| "[#{tag}] " }.join
end
end

0 comments on commit ac93e7b

Please sign in to comment.