为什么在AWS Lambda实施中使用匕首比使用Guice更好? [英] Why is dagger considered better for AWS lambda implementation than Guice?

查看:115
本文介绍了为什么在AWS Lambda实施中使用匕首比使用Guice更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道dagger在生成时通过生成代码来创建注入,因此它的性能优于Guice,后者在运行时可以做到.但是特别是对于lambda的情况,我看到它在多个地方都提到了Dagger是首选.是因为冷启动问题吗?

I know that dagger creates injection at compile time by generating code and hence its performance is better than Guice, which do it at runtime. But specifically for case of lambda, I see it mentioned at multiple places that Dagger is preferred. Is it because of the cold start problem?

由于lambda中存在冷启动问题,因此lambda会在长时间后收到请求时多次进行自举.那么,使用匕首,与Guice相比,引导程序要快得多,因为它已经具有生成的代码了?我说的是,与延迟加载相比,Guice中的所有对象是否也在引导过程中创建.

Because of the cold start problem in lambda, lambda keeps doing bootstrapping multiple times whenever it receives a request after long time. So, with dagger, bootstrapping would be much faster as compared to Guice as it already has the generated code? I am saying if all the objects in Guice are also created during bootstrap as compared to Lazy loading.

推荐答案

您已经知道,任何依赖项注入框架有时都需要为应用程序所需的对象构建某种依赖关系图.构建此图通常是DI框架中计算上最昂贵的部分.

As you already know, any dependency injection framework, at some point, needs to build some sort of dependency graph of the objects that are required by your application. Building this graph is often the most computationally expensive part of the DI framework.

Guise通过在运行时使用反射来弄清楚该图. Dagger会在编译时生成表示依赖关系图的代码.我不知道哪一个更快,但是我确实知道使用反射会带来不小的性能损失.

Guice figures it out this graph by using reflection at runtime. Dagger generates code that represents the dependency graph at compile time. I don’t know which one is faster, but I do know that using reflection incurs a non-trivial performance hit.

但是,最大的区别是Dagger在编译时完成了所有繁重的工作(这意味着您需要执行一次工作,无论您运行了多少次),而Guice每次应用程序启动时都必须执行等效的工作向上.

However, the biggest difference is that Dagger does all the heavy lifting at compile time (which means you do the work once, no matter how many times you run it), whereas Guice must do the equivalent work every time the application starts up.

现在,要回答您的问题,如果应用程序频繁启动和停止,则首选Dagger.对于诸如移动应用程序之类的东西,较慢的启动时间通常只会降低UX的性能.使用Lambda不仅会减慢冷启动的时间,而且由于您需要为代码的运行时间付费,因此不断重建依赖关系图实际上会花费您更多的钱.

Now, to answer your question, Dagger is preferred if your application frequently starts and stops. With something like an mobile app, a slower startup time mostly just degrades the UX. With Lambda, not only does it slow down the cold start time, but since you are billed for the amount of time your code is running, it’s actually going to cost you more money to be constantly rebuilding the dependency graph.

TLDR; (对于冷启动时间和成本而言)Dagger在Lambda上是首选,因为它移动了DI框架中最昂贵的部分来编译时间,而不是在运行时执行.

TLDR; Dagger is preferred on Lambda (for both the cold start time and the cost) because it moves the most expensive part of the DI framework to compile time instead of performing it at runtime.

这篇关于为什么在AWS Lambda实施中使用匕首比使用Guice更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆