简体   繁体   English

dart 中的循环进口是否不好?

[英]Are circular imports bad in dart?

Dart allows circular import. Dart 允许循环导入。

My general understanding was that cyclical import (file a import file b, file b import file a) was a bad practice, generally to let go resources, and garbage collection.我的一般理解是循环导入(文件a导入文件b,文件b导入文件a)是一种不好的做法,一般是为了让go资源和垃圾回收。 However there is not even a warning in dart so I'm left wondering if it can lead to undesired behavior or not.然而,dart 中甚至没有警告,所以我想知道它是否会导致不良行为。

Does it have an effect on garbage collection.它对垃圾收集有影响吗? Can it lead to issues?会导致问题吗?

Circular imports are not bad.循环进口也不错。

Dart's circular imports are not related to garbage collection. Dart 的循环导入与垃圾回收无关。 It's all about making names available to other libraries at compile-time.这一切都是为了在编译时让其他库可以使用名称

Cycles in data structures at run-time may keep objects stay alive (but, unlike some other languages, Dart's garbage collection can collect entire cycles as long as there is no outside reference to any object in the cycle).运行时数据结构中的循环可以使对象保持活动状态(但是,与其他一些语言不同,只要循环中没有对任何 object 的外部引用,Dart 的垃圾收集就可以收集整个循环)。

Libraries are not data structure at run-time, they only really exist at compile-time.库在运行时不是数据结构,它们仅在编译时才真正存在。

All the imported libraries exist in the program anyway.无论如何,所有导入的库都存在于程序中。 If the program uses library A, and library A imports library B, then both libraries will be part of the program (their individual members may or may not be tree-shaken away depending on whether they are actually used or not, but that's independent of the library dependencies.)如果程序使用库 A,而库 A 导入库 B,那么这两个库都将成为程序的一部分(它们的各个成员可能会或可能不会被树摇晃,具体取决于它们是否实际使用,但这与库依赖项。)

If library B then also imports library A, that changes nothing.如果库 B 然后也导入库 A,那什么都不会改变。 Both libraries are still part of the program.这两个库仍然是该计划的一部分。 All it does is to allow members of library B to refer to member declarations of library A.它所做的只是允许库 B 的成员引用库 A 的成员声明。

Cyclic dependencies does affect efficient modular compilation.循环依赖确实会影响高效的模块化编译。 In order to compile one library, you need to have compiled all its dependencies too (at least to some extend, which includes type inference), otherwise you can't check whether the library is using its dependencies correctly.为了编译一个库,您还需要编译其所有依赖项(至少在某种程度上,包括类型推断),否则您无法检查该库是否正确使用其依赖项。

If you have a one-directional dependency, then you can (potentially) compile the dependency completely before you start looking at the library which imports it.如果您有一个单向依赖项,那么您可以(可能)在开始查看导入它的库之前完全编译该依赖项。 Sometimes build systems even allows a library to be compiled once, and then reused in multiple other programs that depend on it.有时构建系统甚至允许一个库编译一次,然后在依赖它的多个其他程序中重用。

If you have a cycle in library dependencies, then the modular compiler needs to compile all the libraries of the cycle in the same compilation step, because none of them can be compiled before the others.如果你在库依赖中有一个循环,那么模块化编译器需要在同一个编译步骤中编译该循环的所有库,因为它们中的任何一个都不能先于其他编译。 So, for a modular Ahead-of-Time compiler, cycles affects the granularity of the modular compilation.因此,对于模块化的 Ahead-of-Time 编译器,周期会影响模块化编译的粒度。 I'd recommend avoiding cycles that go across multiple packages.我建议避免跨多个包的 go 循环。 Inside a single package, it's perfectly fine.在单个 package 内部,它非常好。

It still makes no difference at runtime .运行时它仍然没有区别。

I do this quite a lot actually and it never caused any problems.我实际上做了很多,它从来没有引起任何问题。

Dart uses circular imports to support multi-pass compilers which processes source code multiple times to create intermediary code. Dart 使用循环导入来支持多次处理源代码以创建中间代码的多遍编译器。 Dart VM also supports Just In Time Compiling and Ahead of time compiling which uses this intermediate code. Dart VM 还支持使用此中间代码的即时编译和提前编译。

The JIT compiler converts the source code into native machine code before execution to improve performance speed and runtime. JIT 编译器在执行前将源代码转换为本机机器码,以提高性能速度和运行时间。 Its used in Java as well.它也用于 Java。

The AOT compiler enforces the type system Dart uses and manages memory using fast object allocation and a generational garbage collector which is what supports these circular imports. AOT 编译器强制执行类型系统 Dart 使用和管理 memory 使用快速 object 分配和支持这些循环导入的分代垃圾收集器。 It is used when your app is ready to be deployed for production.当您的应用程序准备好部署到生产环境时使用它。 It compiles the code before its delivered to whatever runtime environment will run it.它在将代码交付到将运行它的任何运行时环境之前对其进行编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM