2

How costly (performance impact) is creating subcontainers in Unity 2.0? The scenario is for example web application or web service where main container is initialized on start of the application but each processed request has its own instace of subcontainer created from the main one. The configuration of the container will not change. The reason is usage of HierarchicalLifetimeManager.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670

1 Answers1

7

As always when the question is related to performance the answer is: measure your application to understand the performance implications for your specific application.

As a general observation, for mainstream applications that integrate with out-of-process resources (such as databases, web services, etc.), object composition is rarely a performance bottleneck. Compose your object graphs with confidence - odds are that the DI Container will not give you a measurable overhead.

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • I'm not asking for performance of object composition and resolving by unity. I'm only interested if there is any performance impact when I create sub containers very often. I can't measure it because I didn't write the code yet I just thinking about my options. – Ladislav Mrnka Mar 17 '11 at 10:10
  • But what is it that makes you concerned about this in the first place? Would you also be concerned about e.g. creating lots of new Uri instances in your application? – Mark Seemann Mar 17 '11 at 10:13
  • My concern is about container initialization. Is it shared with main container, clonned or initialized again? – Ladislav Mrnka Mar 17 '11 at 10:17
  • 2
    I followed your advice and made some tests on simple example instancing several thousands of sub containers without any performance issue. Also thanks for reminding that premature optimalization is a bad way. – Ladislav Mrnka Mar 17 '11 at 23:29