Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do lambdas resolve faster?

Tags:

autofac

The docs recommend to register frequently-used components via lambdas as ...

This can yield an improvement of up to 10x faster Resolve() calls

Now there are obviously a few questions:

  1. why? (EDIT: to clarify: I would understand if the register time goes up, because you got to use reflection now to find the right constructor and such, but why the heck the resolve time?)
  2. In which scenarios does this apply / what aspects of the registered class make this number go up/which make it go down?
  3. What resolve times are we generally talking about anyhow? Like "yeah now it takes 100 instead of 10 cpu cycles" or actually measurable numbers in "normal" use cases (web service with per-request lifetimes)?
like image 804
FrankyBoy Avatar asked Jan 31 '16 13:01

FrankyBoy


People also ask

Why are Lambda functions fast?

Being anonymous, lambda functions can be easily passed without being assigned to a variable. Lambda functions are inline functions and thus execute comparatively faster.

Does lambda function improve performance?

If a function is CPU-, network- or memory-bound, then changing the memory setting can dramatically improve its performance. Since the Lambda service charges for the total amount of gigabyte-seconds consumed by a function, increasing the memory has an impact on overall cost if the total duration stays constant.

Are Lambda layers faster?

Summary. Just by chance of inspecting how layers impact Lambda execution time it came out that the quickest execution for cold Lambda was still about 32 times slower that the quickest for hot Lambda (e.g. with layers: ~978ms/29ms).


1 Answers

As noted in the comments, the short version is that the concrete implementation is going to be faster than the reflection manner of resolving.

Diving deeper, think about the steps involved in each.

Lambda:

  1. Execute the method.
  2. There is no step two.

Reflection:

  1. Enumerate all the constructors for the type to be instantiated. This list could be cached, but is already fairly well cached by the .NET framework.
  2. Of all of the constructors that are available, figure out which one to execute based on the number of constructor parameters available and the types registered in the container. Note the types registered in the container may change based on registration sources, lifetime scope registrations, etc.
  3. Resolve the constructor parameters. If there are reflection-based registrations that make up the constructor parameters, run them through this process recursively.
  4. Invoke the selected constructor using the resolved parameters.

As you can see, there's actually a lot more work than just Activator.CreateInstance in the reflection manner of resolving, which is why it takes longer.

But, as also noted in the comments, don't worry about premature optimization. This all happens pretty darn quickly so wait to optimize until you can actually locate a bottleneck using a profiler or some similar tool.

like image 113
Travis Illig Avatar answered Oct 23 '22 04:10

Travis Illig