• Stars
    star
    191
  • Rank 196,798 (Top 4 %)
  • Language
    C
  • Created about 10 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Creating native instructions in PHP since 2014

JIT-Fu

Build Status

A lesson in the art of JoeJITFu!

JIT-Fu is a PHP extension that exposes an OO API for the creation of native instructions to PHP userland, using libjit.

A JoeJITFu elephpant

Disclaimer: the elephpant above is an artists rendition, the actual animal may or may not exist ...

Fibonacci

Because apparently, that's super important ...

<?php
use JITFU\Context;
use JITFU\Type;
use JITFU\Signature;
use JITFU\Func;
use JITFU\Value;

$context = new Context();

$integer   = Type::of(Type::int);

$function = new Func($context, new Signature($integer, [$integer]), function($args) use($integer) {
	$zero     = new Value($this, 0, $integer);
	$one      = new Value($this, 1, $integer);
	$two      = new Value($this, 2, $integer);

	/* if ($arg == 0) return 0; */
	$this->doIf(
		$this->doEq($args[0], $zero),
		function() use ($zero) {
			$this->doReturn($zero);
		}
	);

	/* if ($arg == 1) return 1; */
	$this->doIf(
		$this->doEq($args[0], $one),
		function() use($one) {
			$this->doReturn($one);
		}
	);

	/* return $function($arg-1) + $function($arg-2); */
	$this->doReturn(
		$this->doAdd(
			$this->doCall($this, [$this->doSub($args[0], $one)]),
			$this->doCall($this, [$this->doSub($args[0], $two)])));	
});

if (stripos('win', PHP_OS) !== 0) {
	$function->dump("Fibonacci");
}

var_dump($function(40)); /* __invoke with magicalness */
?>

The code above will yield something like the following output on Linux:

[joe@localhost jit]$ time php -dextension=jitfu.so objects.php 
function Fibonacci(i1 : int) : int
        incoming_reg(i1, rdi)
.L:
        i6 = i1 == 0
        if i1 != 0 then goto .L0
.L:
        return_int(0)
        ends_in_dead
.L0:
        i7 = i1 == 1
        if i1 != 1 then goto .L1
.L:
        return_int(1)
        ends_in_dead
.L1:
        i8 = i1 - 1
        outgoing_reg(i8, rdi)
        call 0x08215dfd0
.L:
        return_reg(i10, rax)
        i12 = i1 - 2
        outgoing_reg(i12, rdi)
        call 0x08215dfd0
.L:
        return_reg(i14, rax)
        i15 = i10 + i14
        return_int(i15)
        ends_in_dead
.L:
.L:
end

int(102334155)


real    0m1.001s
user    0m0.997s
sys     0m0.003s

On my machine, this is ~60 times faster than PHP.

This library is not useful for compiling Zend opcodes

Installation Instructions

Linux

This extension is being developed against the latest libjit sources, it is advisable to install a fresh copy of libjit even if the system has one present:

git clone git://git.sv.gnu.org/libjit.git
cd libjit
./auto_gen.sh
./configure --prefix=/opt
make
sudo make install

Now you can build the PHP extension:

git clone https://github.com/krakjoe/jitfu
cd jitfu
phpize
./configure --with-jitfu=/opt
make
sudo make install

The example sessions above will result in a build of libjit and JIT-Fu

Windows

Snapshots of the master branch are built at 1-2 AM UTC on Mondays. Currently only VC11 x86 builds against PHP 5.5 and 5.6 are available, these should work with the packages that can be downloaded from windows.php.net. The bundled libjit is built as an self-contained binary with MinGW from a patched version of the master branch.

  • Download the appropriate package for your PHP installation from here.
  • Place php_jitfu.dll in your configured extension_dir.
  • Place libjit.dll in a directory that appears in your %PATH%.

Note: The dump() methods are currently unavailable on Windows. Calling these methods on Windows will result in a JITFU\Exception being thrown.

TODO

  • integration tests for as much as possible
  • other cool things, probably
  • pecl & phpdoc

More Repositories

1

pthreads

Threading for PHP - Share Nothing, Do Everything :)
C
3,473
star
2

parallel

A succinct parallel concurrency API for PHP8
C
1,383
star
3

apcu

APCu - APC User Cache
C
955
star
4

phpdbg

The Interactive PHP Debugger
C
838
star
5

pcov

PCOV - CodeCoverage compatible driver for PHP
C
684
star
6

ui

Cross platform UI development in PHP
C
516
star
7

tombs

Detect unused code in production
C
428
star
8

uopz

User Operations for Zend
C
350
star
9

stat

A super modern high performance profiler for production
C
209
star
10

explain

Explain
JavaScript
108
star
11

pthreads-polyfill

A polyfill for pthreads
PHP
80
star
12

ilimit

Limit time and memory consumption of individual calls
C
70
star
13

idbg

Inspector Debugger
PHP
70
star
14

wkhtmltox

Converting HTML to X since 2017
C
66
star
15

ustring

UnicodeString for PHP7
C++
65
star
16

promises

Promises in PHP
PHP
64
star
17

autostrict

Automatic strict types in PHP7
C
64
star
18

inspector

Disassembler and Debug Kit for PHP 7
C
56
star
19

sandbox

A sandbox environment for PHP7.1+
C
54
star
20

cmark

CommonMark for PHP
C
46
star
21

mimus

mocking framework as light as a bird ...
PHP
46
star
22

pcov-clobber

Run PCOV in versions of PHPUnit before 8, if you must ...
PHP
40
star
23

componere

Complex Type Composition and Manipulation
C
38
star
24

apcu-bc

APCu Backwards Compatiblity Module
C
33
star
25

SIMD

Single Instruction, Multiple Data
C
30
star
26

pthreads-autoloading-composer

An example of how to use composers autoloader in conjunction with Pools
PHP
29
star
27

memoize

Caching the result of your expensive function calls since 2016
C
23
star
28

profiler

An extension to profile PHP
C
19
star
29

trace

Tracing for PHP7 Processes
C
18
star
30

php-jansson

jannson based json encoder and decoder for PHP
C
18
star
31

snappy

Snappy Compression for PHP
PHP
15
star
32

indexed

Educational Extension for PHP7
C
13
star
33

router

This extension serves to provide a sane, easy router for modern PHP applications:
C
13
star
34

uref

weak refs, for the brave ...
C++
13
star
35

phpdbg-ui

phpdbg remote console client
Java
13
star
36

ponion

onion server for php
C
10
star
37

operators

Override operators in userland
C
9
star
38

HandlerSocket

HandlerSocket PHP 7 Extension
C
9
star
39

apcup

APCu Pooling
C
9
star
40

utypes

User verified types
C
9
star
41

overload

Overloading Zend (RESEARCH)
C
9
star
42

bundle

An experimental method of bundling PHP code with extensions
C
7
star
43

nocheq

Sick of pesky type checking making your code too slow (and correct) ?
C
7
star
44

kore

kore php functions
C
6
star
45

perf

Sampling Profiler for PHP 7 (Unfinished)
C
6
star
46

Object

Multiple Inheritance at Runtime
C
5
star
47

cmark-visitors

Visitors for CommonMark implementing some useful AST transformations
PHP
4
star
48

instrumental

Componere Instrumental
PHP
4
star
49

u2fh

PHP7 u2fh
C
4
star
50

u2fs

PHP7 u2fs
C
3
star
51

sysconf

Get configuration information at run time
C
3
star
52

jitfi

PHP FFI header for libjit
C
3
star
53

cloname

Demonstrate how to give closures long names
C
2
star
54

dec64

dec64
Assembly
2
star
55

transformer

C
2
star
56

zi

Zend Instruments
C
2
star
57

ohash

spl_object_hash alternative playground
C
1
star
58

ui-doc

Documentation Resources for UI
PHP
1
star
59

apcu-ps

APCu PS Module
C
1
star