简体   繁体   中英

Are circular imports bad in dart?

Dart allows circular import.

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. However there is not even a warning in dart so I'm left wondering if it can lead to undesired behavior or not.

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. 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).

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.)

If library B then also imports library A, that changes nothing. 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.

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. I'd recommend avoiding cycles that go across multiple packages. Inside a single package, it's perfectly fine.

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 VM also supports Just In Time Compiling and Ahead of time compiling which uses this intermediate code.

The JIT compiler converts the source code into native machine code before execution to improve performance speed and runtime. Its used in Java as well.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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