When using multiple objects or large data structures, one way to enhance their performance is to use deferred loading or assignment (to implement). The idea that the time-consuming operation will be shortened as much as possible has never been realized. I use some examples to illustrate how lazy loading techniques can be used in Java, giving some guidance to make it easier to use in other languages and as part of the core language.
One use scenario is to show hundreds of of metadata and measure worthy domain objects through jtable. Initializing our domain objects can hold two types of objects in memory, even if only part of the metadata is displayed in the table. It will take a few seconds to build the table (but) we can accept another hundreds of items (data). After doing some analysis, we improved our implementation and looked like this:
public class DomainObject {
private final dataparser parser;
Private final map<string, string> header = new hashmap<> ();
Private final list<data> Data = new arraylist<> ();
Public DomainObject (Dataparser aparser) {
parser = Aparser;
}
Public String Getheaderfield (string name) {
//Here we lazily parse and fill the header map
if (Header.isempty ()) {
Header.addall (Parser.header ());
}
return Header.get (name);
Public iterable<data> getmeasurementvalues () {
//again Lazy-load and parse the Data
if (Data.isempty ()) {
Data.addall (parser.measurements ());
}
return data;
}
This improvement improves the display of time entries and significantly improves our ability to handle large data items. All data loading occurs only when someone wants to see the specific details of the measurement and double-click an entry.
A Java unboxing gets deferred assignment in the case of a conditional statement
Lazy and fast because the expensive operation'll only execute when needed
if (acondition () && Expensiveop Eration ()) {...}
Slow order (still lazy evaluated!)
if (Expensiveoperation () && acondition ()) {...}
Persistent layer frames often default to delay loading like hibernate because database access and data transfer costs are very high in general.
Most language features are based on a wrapping delay assignment, which is the theoretical function that minimizes the effect of side effects on latency as all members of the first class are most isolated. Scala mixes object-oriented language features and introduces deferred keywords to simplify Java styling, delaying the initial code as follows
public class DomainObject (Parser:dataparser) {//evaluated on the ' on the ' of the ' the ' on ' a ',
private lazy val Header = {parser.he Ader ()}
def Getheaderfield (name:string): String = {
header.get (name). Getorelse ("")
}
//evaluated On the
lazy val Measurementvalues:iterable[data] = {
parser.measurements ()
}
}
Conclusion
Deferred loading is not a new revolutionary technique but is a useful tool when dealing with large data or slowing down resources. In many cases you can use it to improve performance or to use it for personal experience.
It lowers the cost of implementing the code if the language support is not very good (like Java) and some other situations, the application will feel more responsive in the pre-compile preprocessing scenario.