Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Groovy 2 Cookbook

You're reading from   Groovy 2 Cookbook Java and Groovy go together like ham and eggs, and this book is a great opportunity to learn how to exploit Groovy 2 to the full. Packed with recipes, both intermediate and advanced, it's a great way to speed up and modernize your programming.

Arrow left icon
Product type Paperback
Published in Oct 2013
Publisher Packt
ISBN-13 9781849519366
Length 394 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Luciano Fiandesio Luciano Fiandesio
Author Profile Icon Luciano Fiandesio
Luciano Fiandesio
Andrey Adamovich Andrey Adamovich
Author Profile Icon Andrey Adamovich
Andrey Adamovich
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Groovy 2 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
1. Getting Started with Groovy 2. Using Groovy Ecosystem FREE CHAPTER 3. Using Groovy Language Features 4. Working with Files in Groovy 5. Working with XML in Groovy 6. Working with JSON in Groovy 7. Working with Databases in Groovy 8. Working with Web Services in Groovy 9. Metaprogramming and DSLs in Groovy 10. Concurrent Programming in Groovy 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

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime