Pushing the Java Platform Further Me Charles Oliver Nutter - - PowerPoint PPT Presentation

pushing the java platform further me
SMART_READER_LITE
LIVE PREVIEW

Pushing the Java Platform Further Me Charles Oliver Nutter - - PowerPoint PPT Presentation

Pushing the Java Platform Further Me Charles Oliver Nutter headius@headius.com @headius JRuby co-lead, fulltime since 2006 JVM enthusiast http://blog.headius.com JRuby Ruby atop the JVM "Just another Ruby


slide-1
SLIDE 1

Pushing the Java Platform Further

slide-2
SLIDE 2

Me

Charles Oliver Nutter

  • headius@headius.com
  • @headius
  • JRuby co-lead, fulltime since 2006
  • JVM enthusiast
  • http://blog.headius.com
slide-3
SLIDE 3

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

slide-4
SLIDE 4

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

slide-5
SLIDE 5

Custom Core Classes

slide-6
SLIDE 6

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
slide-7
SLIDE 7

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

slide-8
SLIDE 8

String

slide-9
SLIDE 9

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

slide-10
SLIDE 10

ByteList

http://github.com/jruby/bytelist

slide-11
SLIDE 11

ByteList

  • A list of bytes

○ Abstraction around byte[] + encoding ○ Similar operations to StringBuffer ○ CoW-capable

slide-12
SLIDE 12

ByteList Demo

slide-13
SLIDE 13

Regexp

slide-14
SLIDE 14

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

slide-15
SLIDE 15

Joni

http://github.com/jruby/joni

slide-16
SLIDE 16

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)!

slide-17
SLIDE 17

Joni Demo

slide-18
SLIDE 18

OS-level Features

slide-19
SLIDE 19

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
slide-20
SLIDE 20

Java Native Runtime

http://github.com/jnr

slide-21
SLIDE 21

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)!

slide-22
SLIDE 22

JNR-FFI (foreign function interface)

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

slide-23
SLIDE 23

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

slide-24
SLIDE 24

JNR-FFI Demo

slide-25
SLIDE 25

JNR-POSIX

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

slide-26
SLIDE 26

JNR-POSIX

  • Commonly used POSIX functions not in JDK

○ Symlinks ○ Filesystem metadata ○ Ownership, permissions ○ True "exec" ○ Process management

slide-27
SLIDE 27

JNR-POSIX Demo

slide-28
SLIDE 28

JNR-ENXIO (Extended Native X-platform IO)

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

slide-29
SLIDE 29

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

slide-30
SLIDE 30

JNR-ENXIO Demo

slide-31
SLIDE 31

JNR-UNIXSOCKET

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

slide-32
SLIDE 32

JNR-UNIXSOCKET

  • Built atop jnr-enxio
  • UNIX sockets

○ Need I say more?

slide-33
SLIDE 33

JNR-UNIXSOCKET

slide-34
SLIDE 34

JNR-X86ASM (yes, it's what you think)

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

slide-35
SLIDE 35

JNR-X86ASM

  • API for x86/x86_64 ASM generation
  • Huh?

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

slide-36
SLIDE 36

JNR-X86ASM Demo

slide-37
SLIDE 37

Ruby FFI

http://github.com/ffi/ffi

slide-38
SLIDE 38

Ruby FFI

  • Ruby DSL for binding native libraries
  • Cross-impl

○ Supported on JRuby and standard Ruby

  • Fun!
slide-39
SLIDE 39

Ruby FFI Demo

slide-40
SLIDE 40

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
slide-41
SLIDE 41

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