jruby: pushing the java platform further

41
Pushing the Java Platform Further

Upload: charles-nutter

Post on 15-May-2015

2.672 views

Category:

Technology


6 download

TRANSCRIPT

Page 1: JRuby: Pushing the Java Platform Further

Pushing the Java Platform Further

Page 2: JRuby: Pushing the Java Platform Further

Me

Charles Oliver Nutter● [email protected]● @headius● JRuby co-lead, fulltime since 2006● JVM enthusiast● http://blog.headius.com

Page 3: JRuby: Pushing the Java Platform Further

JRuby

● Ruby atop the JVM○ "Just another Ruby implementation"○ "Just another JVM language"

● A challenge for the JVM○ Dynamic typing/dispatch○Own set of core classes○ Heavy use of OS-level features

Page 4: JRuby: Pushing the Java Platform Further

JRuby

● Ruby atop the JVM○ "Just another Ruby implementation"○ "Just another JVM language"

● A challenge for the JVM○ Dynamic typing/dispatch○Own set of core classes○Heavy use of OS-level features

Page 5: JRuby: Pushing the Java Platform Further

Custom Core Classes

 

Page 6: JRuby: Pushing the Java Platform Further

What Classes?

● Array: a mutable, growable list of objects● Hash: an ordered associative collection● String: a collection of bytes (w/ an encoding in 1.9)● Symbol: a named identifier● IO: a stream of bytes (w/ an encoding in 1.9)● File: a stream of bytes from filesystem● Range: a range of values

Page 7: JRuby: Pushing the Java Platform Further

Why Custom?

● Array, Hash○ Behavior must match Ruby exactly○Many operations must redispatch into Ruby

● String○ Java's String is immutable○ Java's String is UTF-16 char[] based

● Regexp○ byte[] based String necessitates it○ Ruby Regexp is rich in features

● IO, File○ Blocking IO simulated atop non-blocking○ No good abstractions over NIO

Page 8: JRuby: Pushing the Java Platform Further

String

 

Page 9: JRuby: Pushing the Java Platform Further

String

● Used for both binary and character data○ "It's just bytes"○ IO operations receive, return String

● Arbitrary encoding○ Implicit encoding in 1.8 (set globally)○ Explicit encoding in 1.9 (String aggregates Encoding)

● Mutable○More efficient to mutate in-place○ Copy-on-write to reduce object churn

Page 10: JRuby: Pushing the Java Platform Further

ByteList

http://github.com/jruby/bytelist

Page 11: JRuby: Pushing the Java Platform Further

ByteList

● A list of bytes○ Abstraction around byte[] + encoding○ Similar operations to StringBuffer○ CoW-capable

Page 12: JRuby: Pushing the Java Platform Further

ByteList Demo

 

Page 13: JRuby: Pushing the Java Platform Further

Regexp

 

Page 14: JRuby: Pushing the Java Platform Further

Regexp

● Operates against byte[] in String○ All other engines operate against char[]○ Transcoding cost == death

● Supports arbitrary encoding○ Not all encodings transcode losslessly○Mismatched but compatible encodings?

● Bytecode engine○ Avoids StackOverflow bugs in java.util.regex○Optimization potential

● Advanced regex features○ Encoding-agnostic○ Named groups

Page 15: JRuby: Pushing the Java Platform Further

Joni

http://github.com/jruby/joni

Page 16: JRuby: Pushing the Java Platform Further

Joni

● Java port of Oniguruma● Ruby-friendly

○ byte[] based○ Arbitrary encodings○ Bytecoded engine

● Fast○ Avoids transcoding costs○ Sometimes faster than j.u.regex

Thanks to Marcin Mielzynski (github.com/lopex)!

Page 17: JRuby: Pushing the Java Platform Further

Joni Demo

 

Page 18: JRuby: Pushing the Java Platform Further

OS-level Features

 

Page 19: JRuby: Pushing the Java Platform Further

OS-level Features

● Process management○ getpid, waitpid, kill, signal, ...

● Filesystem operations○ symlink, ownership, permissions, ...

● User-friendly IO○ Interruptible, inherit TTY, "select" function, ...

● Binding native libs○ Avoid hand-written native shim (JNI)○ Route around JVM APIs

● UNIX sockets

Page 20: JRuby: Pushing the Java Platform Further

Java Native Runtime

http://github.com/jnr

Page 21: JRuby: Pushing the Java Platform Further

Java Native Runtime

A collection of libraries to make interfacing with OS-level features easier.

● Easy binding of native libs● NIO-like IO atop file descriptors● UNIX sockets● POSIX filesystem operations

Big thanks to Wayne Meissner (@wmeissner)!

Page 22: JRuby: Pushing the Java Platform Further

JNR-FFI(foreign function interface)

http://github.com/jnr/jnr-ffi

Page 23: JRuby: Pushing the Java Platform Further

JNR-FFI

● Base of the JNR libraries● Easy binding of native functions● Struct mapping● Memory/pointer management● As fast as JNI?

○Within 10-20% (comparing invocation overhead)○  "a viable alternative, even for reasonably

performance-sensitive libraries" - @wmeissner○ "A hell of a lot nicer than using JNI!" - @headius

Page 24: JRuby: Pushing the Java Platform Further

JNR-FFI Demo

 

Page 25: JRuby: Pushing the Java Platform Further

JNR-POSIX

http://github.com/jnr/jnr-posix

Page 26: JRuby: Pushing the Java Platform Further

JNR-POSIX

● Commonly used POSIX functions not in JDK○ Symlinks○ Filesystem metadata○Ownership, permissions○ True "exec"○ Process management

Page 27: JRuby: Pushing the Java Platform Further

JNR-POSIX Demo

 

Page 28: JRuby: Pushing the Java Platform Further

JNR-ENXIO(Extended Native X-platform IO)

http://github.com/jnr/jnr-enxio

Page 29: JRuby: Pushing the Java Platform Further

JNR-ENXIO

● Mimics NIO○ Same or similar API○ Channels, Selectors, etc

● Implemented atop JNR-FFI○ 100% Java (other than JNR-FFI)

● Multiple backends○ poll, kqueue currently○ epoll, win32 needed...volunteers?

● Full set of operations for *any* IO type○ NIO can't select on stdio, files

Page 30: JRuby: Pushing the Java Platform Further

JNR-ENXIO Demo

 

Page 31: JRuby: Pushing the Java Platform Further

JNR-UNIXSOCKET

http://github.com/jnr/jnr-unixsocket

Page 32: JRuby: Pushing the Java Platform Further

JNR-UNIXSOCKET

● Built atop jnr-enxio● UNIX sockets

○ Need I say more?

Page 33: JRuby: Pushing the Java Platform Further

JNR-UNIXSOCKET

 

Page 34: JRuby: Pushing the Java Platform Further

JNR-X86ASM(yes, it's what you think)http://github.com/jnr/jnr-x86asm

Page 35: JRuby: Pushing the Java Platform Further

JNR-X86ASM

● API for x86/x86_64 ASM generation● Huh?

○ Down to the metal, baybee○ Faster bindings to native functions○Other weird stuff...

Page 36: JRuby: Pushing the Java Platform Further

JNR-X86ASM Demo

 

Page 37: JRuby: Pushing the Java Platform Further

Ruby FFI

http://github.com/ffi/ffi

Page 38: JRuby: Pushing the Java Platform Further

Ruby FFI

● Ruby DSL for binding native libraries● Cross-impl

○ Supported on JRuby and standard Ruby● Fun!

Page 39: JRuby: Pushing the Java Platform Further

Ruby FFI Demo

 

Page 40: JRuby: Pushing the Java Platform Further

What Have We Learned?

● The JVM can be a great native platform● No problem is impossible to solve ;-)● JRuby gives you all this (and more) for free

Page 41: JRuby: Pushing the Java Platform Further

Links and Q/A

(All on Github)http://github/

● jruby - JRuby organization○ /jruby - JRuby itself○ /bytelist - byte[] container○ /jcodings - byte[] encoding support○ /joni - byte[] regex

● /jnr - Java Runtime○ /jnr-ffi - foreign function interface○ /jnr-posix - POSIX features○ /jnr-enxio - native IO○ /jnr-unixsocket - UNIX sockets○ /jnr-x86asm - x86 assembly

● /ffi/ffi - Ruby FFI