GithubHelp home page GithubHelp logo

isabella232 / unsafe-1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from medallia/unsafe

0.0 0.0 0.0 91 KB

Java library to compile and run C++ code in-memory

License: Apache License 2.0

Shell 1.41% C++ 37.12% Java 56.05% Makefile 5.42%

unsafe-1's Introduction

Unsafe

a Java library to compile and run C++ code in-memory based on Clang+LLVM:

final NativeModule nativeModule = Driver.compileInMemory(
		"#include<stdio.h>\n" +
		"extern \"C\" void foo() {" +
		"printf(\"hello world!\\n\");" +
		"}");

nativeModule.getFunctionByName("foo").invoke();

This library is designed for performance sensitive applications where access and generation of fast native code on the fly gives a performance edge. It is not intended for integration, mainly because the linker is not exposed in the API to keep it simple.

Features

  • Compile and run C++ code using Clang+LLVM
  • Dynamic invocation of compiled functions with basic argument marshalling from/to Java
  • Built in support for JNI types on compiled code
  • Implement native methods on the fly
  • Automatic thunk generation for fast Java to native calling

There are two main packages:

  • com.medallia.unsafe: The low-level api.
  • com.medallia.thunk: an automatic thunk builder to dynamically implement JNI methods.

The low-level API provides a simple reflective interface for compiled modules. Even though it is practical, it is awfully slow. The call cost of invoking NativeFunction.invoke() is about 1 ms. To avoid this, you should use the automatic thunk generator, which lets you implement JNI methods on the fly. A JNI method call ia about 1000x faster.

Building

Make sure that you have a properly set JAVA_HOME environment variable.

Go to the directory where you checked out the project:

cd unsafe

Then you need to checkout and build Clang (it will take some time):

./bin/checkout-clang.sh
./bin/build-clang.sh

The scripts will checkout the correct version for you. Once that's done, you can build the JNI library:

cd jni
make

Depending on how your build environment is setup, you might need to pass the JAVA_HOME variable to make.

To run the examples, you will have to pass the JNI library directory in the java.library.path property, for example:

-Djava.library.path=/Users/<your username>/projects/unsafe/jni

unsafe-1's People

Contributors

efranqueiro avatar juancn avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.