August 17, 2020

The Lambda Function In Java

Source of Information: Java Classes In Pune

Overview

The main issue around the use of Lambdas in Java and Low Latency is; Does they produce crap and is there anything you can do about it?

Background

I am working on a library which supports different wire protocols. The thought being that you can describe the information you want to write/read and the cable protocol determines if it uses text with fields such as JSon or YAML, text with discipline numbers such as FIX, binary together with field names like BSON or a Binary form of YAML, binary with fields name, subject numbers or no discipline meta at all. The values could be fixed length, factors length and/or self describing data types.

The notion being that it can handle a variety of schema changes or in the event that it's possible to determine the schema is the same e.g. over a TCP session, then it is possible to skip all that and only send the information.

Another big idea is using lambdas to encourage this.

What's the problem with Lambdas

The main issue is the need to avoid substantial garbage in non latencies applications. Notionally, every single time you see lambda code that is a new Object.

Fortunately, Java 8 has considerably enhanced Escape Analysis. Escape Analysis allows the JVM to substitute new Object by unpacking them onto the stack, effectively providing you stack allocation. This feature was available in Java 7 but it rarely eliminated objects. Note: when you use a profiler it tends to prevent Escape Analysis from functioning so that you can not trust profilers that use code injection as the profiler might say an object is being creation when minus the profiler it doesn't produce an object. Flight Recorder does appear to wreck with Escape Analysis.

Escape Evaluation has consistently had quirks and it appears that it still does. For example, if you've got an IntConsumer or any other primitive user, the allocation of the lambda could be eliminated in Java 8 upgrade 20 - update 40. However, the exclusion being boolean where this does not seem to happen. Hopefully this will be fixed in a future edition.

Another quirk is the size (after inlining) of the procedure where the thing elimination happens things and in relatively modest procedures, escape investigation may give up. Other notes:

When I used a profiler, YourKit in this scenario, code that produced no garbage started generating garbage since the Escape Analysis failed.

I printed the method inlining and discovered assert statements in some key methods prevented them from being inlined as it created the methods bigger. I fixed this by developing a sub-class of by main class with assertions on to be created by a factory method when assertions are enabled. The default class has no assertions and no performance impact.

Before I moved these assertions I can just deserialize 7 areas without triggering garbage.

Once I altered the lambdas with anonymous inner classes, I saw comparable thing elimination though in most cases if you can use lambda that is preferred.

Conclusion

Java 8 seems to be considerably smarter at removing garbage produce by very short lived objects. This means that methods such as passing lambdas may be an alternative in Low Latency software.

Learn Java Programming In Pune | Java Training In Pune | Java Course In Pune