Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-1041

Avoid Module#delegate (ActiveSupport) for Pure Performance in Development?

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.2.0
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      Module#delegate (ActiveSupport) calls Kernel#caller once for each method being delegated.

      Not only does Kernel#caller have to crawl the call stack, it also has to build up an array of 60 strings (that's the stack depth for the average call to delegate in Mongoid) which doesn't even get used.

      Every call to include Mongoid::Document results in 11 calls to Module#delegate (ActiveSupport) and 17 method delegations, thus 17 calls to Kernel#caller. With 20 model classes, that becomes 340 calls to Kernel#caller, creating 340 arrays of 60 strings each.

      Timing it reveals that 340 calls to Kernel#caller with a stack depth of 60 costs on average 20ms on my machine (34,000 calls costs 1.98s). This is a cost that can easily be avoided.

      Every call to delegate :method, :to => :target can be replaced with def method; target.method end. Slightly less elegant, but also a one-liner and avoids calling Kernel#caller.

      Even though ActiveSupport's delegation is convenient, Mongoid might avoid it for pure performance in development mode.

            Assignee:
            Unassigned Unassigned
            Reporter:
            yfeldblum yfeldblum
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: