Book Image

Groovy 2 Cookbook

Book Image

Groovy 2 Cookbook

Overview of this book

Get up to speed with Groovy, a language for the Java Virtual Machine (JVM) that integrates features of both object-oriented and functional programming. This book will show you the powerful features of Groovy 2 applied to real-world scenarios and how the dynamic nature of the language makes it very simple to tackle problems that would otherwise require hours or days of research and implementation. Groovy 2 Cookbook contains a vast number of recipes covering many facets of today's programming landscape. From language-specific topics such as closures and metaprogramming, to more advanced applications of Groovy flexibility such as DSL and testing techniques, this book gives you quick solutions to everyday problems. The recipes in this book start from the basics of installing Groovy and running your first scripts and continue with progressively more advanced examples that will help you to take advantage of the language's amazing features. Packed with hundreds of tried-and-true Groovy recipes, Groovy 2 Cookbook includes code segments covering many specialized APIs to work with files and collections, manipulate XML, work with REST services and JSON, create asynchronous tasks, and more. But Groovy does more than just ease traditional Java development: it brings modern programming features to the Java platform like closures, duck-typing, and metaprogramming. In this new book, you'll find code examples that you can use in your projects right away along with a discussion about how and why the solution works. Focusing on what's useful and tricky, Groovy 2 Cookbook offers a wealth of useful code for all Java and Groovy programmers, not just advanced practitioners.
Table of Contents (17 chapters)
Groovy 2 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Running Groovy with invokedynamic support


One of the biggest improvements introduced in Groovy 2.0 is the support for the invokedynamic instruction. The invokedynamic is a new JVM instruction available in Java 7, which allows easier implementation and promises increased speed and efficiency of dynamic languages (for example, Groovy).

Dynamic languages generate a bytecode that often necessitates a number of JVM method invocations to perform a single operation. Furthermore, reflection and dynamic proxies are used extensively, which comes with a costly performance toll. Also, the JIT (Just-In-Time) compiler that helps to improve the runtime performance of a JVM, cannot work its magic by applying optimization to the bytecode because it lacks information and patterns, which are normally possible to optimize. The new bytecode instruction, invokedynamic, is able to mitigate partially these issues, including support for better JIT optimization.

In this recipe, we will show how to run Groovy with the invokedynamic (also known as indy) support and Java 7.

Getting ready

The invokedynamic support is a compile-time and runtime feature only. In other words, a developer cannot use it from within the source code. What invokedynamic brings to Groovy 2.0 (and even more to 2.1) is basically improved runtime performance.

How to do it...

As explained in the introduction, Java 7 is the required version of the JVM to compile Groovy code that leverages the invokedynamic instruction:

  1. Make sure that Java 7 is your current JVM. Type java -version and confirm that the output mentions Version 7:

    java version "1.7.0_25"
    
  2. The following steps will let us fully enable the indy support in your Groovy distribution. First of all rename or remove all the JAR files starting with groovy- in the lib directory of your Groovy 2.x home directory.

  3. Replace them with the files in the indy directory located in the root of the Groovy distribution folder.

  4. Remove the -indy classifier from the JAR names.

  5. Finally, invoke either groovy or the groovyc compiler with the --indy flag to execute your code:

    groovy --indy my_script.groovy
    

There's more...

It is important to note that if the --indy flag is omitted, the code will be compiled without the invokedynamic support, even if Java 7 is used and the Groovy JAR files have been replaced.

The performance gain introduced by the new JVM instruction greatly varies depending on a numbers of factors, including the actual JVM version and the type of code that is optimized. JVM support for the invokedynamic instruction improves at each version. The upcoming Java 8 will use invokedynamic to support lambda functions, so it is very likely that newer JVMs will offer even greater optimization. Given the current state of things, some benchmarks that we have run have shown an improvement of around 20 percent when given the same code compiled with the invokedynamic instruction enabled.

See also