Skip to content

Commit

Permalink
Add GraalVM Native Image and Truffle compilation sections for GraalPy…
Browse files Browse the repository at this point in the history
… guides (#1563)

* Add GraalVM Native Image and Truffle compilation sections for GraalPy guides

* Language/style improvements

Co-authored-by: Olga Gupalo <[email protected]>

* Minor fix in common-graal-with-plugins-multi.adoc

* Required Metadata -> GraalVM Native Image Metadata

* Remove left-over text

---------

Co-authored-by: Olga Gupalo <[email protected]>
  • Loading branch information
steve-s and olyagpl authored Feb 5, 2025
1 parent df7012b commit 7efcf3a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ Open `http://localhost:8080/pygal` in a web browser of your choice execute the e

image::graalpy-pygal.png[]

common:graalpy-graalvm.adoc[]

Open http://localhost:8080/pygal[http://localhost:8080/pygal] in your web browser to execute the endpoint exposed by the native executable. You should see a stacked bar chart.
endpoint exposed by the native executable. You should see a stacked bar chart.

== Next Steps

Read more about https://micronaut-projects.github.io/micronaut-test/latest/guide/[Micronaut testing].
Expand Down
14 changes: 14 additions & 0 deletions guides/micronaut-graalpy/micronaut-graalpy.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ callout:http-request[]
common:testApp.adoc[]
common:runapp.adoc[]

common:graalpy-graalvm.adoc[]

You can execute the endpoint exposed by the native executable:

[source, bash]
----
curl localhost:8080/hello
----

[source]
----
Hello World
----

== Next Steps

Read more about https://micronaut-projects.github.io/micronaut-graal-languages/latest/guide/[Micronaut Graalpy] integration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ It is possible to customize the name of the native executable or pass additional
<version>0.10.3</version>
<configuration>
<!-- <1> -->
<imageName>mn-graalvm-application</imageName> <1>
<imageName>mn-graalvm-application</imageName>
<buildArgs>
<!-- <2> -->
<buildArg>-Ob</buildArg>
Expand Down
69 changes: 69 additions & 0 deletions src/docs/common/snippets/common-graalpy-graalvm.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
:exclude-for-languages:groovy

== Python Runtime Compilation

When using the GraalVM JDK, Python code executed with the GraalPy engine is just-in-time compiled.
With other JDKs, the Python code is interpreted, resulting in reduced performance.

The easiest way to install https://www.graalvm.org[GraalVM] on Linux or Mac is to use https://sdkman.io/[SDKMan.io].

[source, bash]
.Java 23
----
sdk install java 23-graal
----

For installation on Windows, or for a manual installation on Linux or Mac, see the https://www.graalvm.org/latest/docs/getting-started/[GraalVM Getting Started] documentation.

The previous command installs Oracle GraalVM, which is free to use in production and free to redistribute, at no cost, under the https://www.oracle.com/downloads/licenses/graal-free-license.html[GraalVM Free Terms and Conditions].

Alternatively, you can use the https://github.com/graalvm/graalvm-ce-builds/releases/[GraalVM Community Edition]:

[source, bash]
.Java 23
----
sdk install java 23-graalce
----

=== GraalVM JDK Compatibility

To enable runtime compilation, you must use versions of GraalPy and the Polyglot API dependencies that are compatible with the specified GraalVM JDK version. If you see errors like the following:

[source, bash]
----
Your Java runtime '23.0.1+11-jvmci-b01' with compiler version '24.1.1' is incompatible with polyglot version '24.1.0'.
----

You need to override the versions of the following dependencies according to the error message:

[source, text]
----
org.graalvm.polyglot:polyglot
org.graalvm.python:python
org.graalvm.python:python-embedding
org.graalvm.python:graalpy-maven-plugin
----

== Generate a Micronaut Application Native Executable with GraalVM

Starting with GraalVM for JDK 23, Micronaut applications that use GraalPy or any Truffle language can be compiled into native executables using GraalVM Native Image.

Compiling Micronaut applications ahead of time with GraalVM significantly improves startup time and reduces
the memory footprint of JVM-based applications.

=== Native Image Configuration
GraalVM Native Image compilation requires metadata to correctly run code that uses https://www.graalvm.org/latest/reference-manual/native-image/metadata/#dynamic-proxy[dynamic proxies].

Create a proxy configuration file:
resource:META-INF/native-image/proxy-config.json[]

=== Native Executable Generation

To generate a native executable using Maven, run:

[source, bash]
----
./mvnw package -Dpackaging=native-image
----

The native executable is created in the _target_ directory and can be run with `./target/micronautguide`.

0 comments on commit 7efcf3a

Please sign in to comment.