DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Leverage Lambdas for Cleaner Code
  • The First Annual Recap From JPA Buddy
  • Deadlock-Free Synchronization in Java
  • Introduction to Spring Data JPA Part 8: Many-to-Many Bidirectional

Trending

  • Enhancing Security With ZTNA in Hybrid and Multi-Cloud Deployments
  • Scaling DevOps With NGINX Caching: Reducing Latency and Backend Load
  • Medallion Architecture: Efficient Batch and Stream Processing Data Pipelines With Azure Databricks and Delta Lake
  • Teradata Performance and Skew Prevention Tips
  1. DZone
  2. Data Engineering
  3. Databases
  4. Shortest Code and Lowest Latency in Java

Shortest Code and Lowest Latency in Java

How do you write code that executes at maximum speed while still keeping coding to a minimum?

By 
Per-Åke Minborg user avatar
Per-Åke Minborg
·
Nov. 28, 18 · Analysis
Likes (16)
Comment
Save
Tweet
Share
18.4K Views

Join the DZone community and get the full member experience.

Join For Free

Shortest Code and Lowest Latency

Who can write the shortest Java code with the lowest latency, and what tools are used?

At Oracle Code One, I promoted a code challenge during my speech. The contestants were given a specific problem and the winner would be the one with lowest possible latency multiplied with the number of code lines that was used (i.e. having low latency and, at the same time, using as few lines as possible is good. I also shared the contest on social media to get as many developers involved as possible.

The input data was to be taken from the table "film" in the open-source Sakila database.

More specifically, the object was to develop a Java application that computes the sum, min, max, and average rental duration for five films out of the existing 1,000 films using a general solution. The five films should be the films around the median film length starting from the 498:th and ending with the 502:th film (inclusive) in ascending film length.

Contestants were free to use any Java library and any solution available, such as Hibernate, JDBC, or other ORM tools.

A Solution Based on SQL/JDBC

One way of solving the problem would be using SQL/JDBC directly as shown hereunder. Here is an example of SQL code that solves the computational part of the problem:

SELECT 
  sum(rental_duration),
  min(rental_duration),
  max(rental_duration),
  avg(rental_duration)
FROM 
  (select rental_duration from sakila.film 
  order by length 
  LIMIT 5 OFFSET 498) as A


If I run it on my laptop, I get a latency of circa 790 microseconds on the server side (standard MySQL 5.7.16). To use this solution, we also need to add Java code to issue the SQL statement and to read the values back from JDBC into our Java variables. This means that the code will be even larger and will take a longer time to execute as shown hereunder:

try (Connection con = DriverManager
                       .getConnection("jdbc:mysql://somehost/sakila?"
                            + "user=sakila-user&password=sakila-password")) {

        try (Statement statement = con.createStatement()) {

            ResultSet resultSet = statement
                .executeQuery(
                    "SELECT " +
                        "  sum(rental_duration)," +
                        "  min(rental_duration)," +
                        "  max(rental_duration)," +
                        "  avg(rental_duration)" +
                        "FROM " +
                        "  (select rental_duration from sakila.film " +
                        "  order by length " +
                        "limit 5 offset 498) as A");

            if (resultSet.next()) {
                int sum = resultSet.getInt(1);
                int min = resultSet.getInt(2);
                int max = resultSet.getInt(3);
                double avg = resultSet.getDouble(4);
                // Handle the result
            } else {
                // Handle error
            }
      }
}


To give this alternative a fair chance, I reused the connection between calls in the benchmark rather than re-creating it each time (recreation is shown above but was not used in benchmarks).

Result: ~1,000 us and ~25 code lines

The Winning Contribution

However, the SQL example above was without a chance compared to the winning contribution. The winner was Sergejus Sosunovas (@SergejusS) from Switzerland, who currently develops an optimization and management system. He used Speedment in-JVM-memory acceleration and states: "It took less than an hour to start and build a solution." Here is the winning code:

IntSummaryStatistics result = app.getOrThrow(FilmManager.class).stream()
     .sorted(Film.LENGTH)
     .skip(498)
     .limit(5)
     .mapToInt(GeneratedFilm::getRentalDuration)
     .summaryStatistics();


This was much faster than SQL/JDBC and completed in as little as 6 microseconds.

Result: 6 us and 6 code lines

A Solution With Only Five Lines

One of the contestants, Corrado Lombard from Italy, is worth an honorary mention since he was able to solve the problem in only five lines. Unfortunately, there was a slight error in his original contribution, but when fixed, the solution looked like this:

IntSummaryStatistics result = app.getOrThrow(FilmManager.class).stream()
    .sorted(Film.LENGTH)
    .skip(498)
    .limit(5)
    .collect(summarizingInt(Film::getRentalDuration));


This fixed solution had about the same performance as the winning solution.

Optimized Speedment Solution

As a matter of fact, there is a way of improving latency even more than the winning contribution. By applying an IntFunction that is able to do in-place-deserialization (in situ) of the int value directly from RAM, it improves performance even more. In-place-deserialization means that we do not have to deserialize the entire entity but just extract the parts of it that are needed. This saves time, especially for entities with many columns. Here is what an optimized solution could look like:

IntSummaryStatistics result = app.getOrThrow(FilmManager.class).stream()
     .sorted(Film.LENGTH)
     .skip(498)
     .limit(5)
     .mapToInt(Film.RENTAL_DURATION.asInt()) // Use in-place-deserialization
     .summaryStatistics();


This was even faster and completed in a just 3 microseconds.

Result: 3 us and 6 code lines

GraalVM and Optimized Speedment Solution

GraalVM contains an improved C2 Compiler that is known to improve stream performance for many workloads. In particular, it seems like the benefits from inlining and escape-analysis are much better under Graal than under the normal OpenJDK.

I was curious to see how the optimized solution above could benefit from GraalVM. With no code change, I run it under GraalVM (1.0.0-rc9), and now, latency was down to just 1 microsecond! This means that we could perform 1,000,000 such queries per second per thread on a laptop and presumably much more on a server grade computer.

Result: 1 us and 6 code lines

Overview

When we compare SQL/JDBC latency against the best Speedment solution, the speedup factor with Speedment was about 1,000 times. This is the same difference as comparing walking to work or taking the worlds fastest manned jet plane (SR-71 "Blackbird"). A huge improvement if you want to streamline your code.

To be able to plot the solutions in a diagram, I have removed the comparatively slow SQL/JDBC solution and only showed how the different Speedment solutions and runtimes measure up:

Comparing Latency Solutions

Try it Out

The competition is over. However, feel free to challenge the solutions above or try them out for yourself. Full details of the competition and rules can be found here. If you can beat the fastest solution, let me know in the comments below.

To complete the challenge, download the following:

  • Download the Sakila database

  • Download Speedment

Benchmark Notes

The benchmark results presented above were obtained when running on my MacBook Pro Mid 2015, 2.2 GHz Intel Core i7, 16 GB 1600 MHz DDR3, Java 8, JMH (1.21), and Speedment (3.1.8)

Conclusion

It is possible to reduce latencies by orders of magnitude and reduce code size at the same time using in-JVM-memory technology and Java streams. Additionally, GraalVM can improve your stream performance significantly under many conditions.

code style Java (programming language) Database

Published at DZone with permission of Per-Åke Minborg, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Leverage Lambdas for Cleaner Code
  • The First Annual Recap From JPA Buddy
  • Deadlock-Free Synchronization in Java
  • Introduction to Spring Data JPA Part 8: Many-to-Many Bidirectional

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!