SlideShare a Scribd company logo
實戰 HHVM Extension 
Ricky Su 
@phpconf2014
About me 
Ricky 是我 哥 
● Symfony 愛好者 
● PHP也有Day 固定攝影師。 
● 目前在 擔任高級水電工。 
玩體驗 
● ricky@ez2.us 
● http://www.facebook.com/ricky.su.35 
● http://ricky.ez2.us/ (目前Server掛點中)
HHVM 
是什麼?
HipHop (HPHPc) 
● 由 Facebook 於 2010年2月2日發布 
● 將 PHP 轉換成 C++,再透過 g++ 編 
譯執行檔。 
● 於2013年2月19日被官方棄用。
HHVM 
● 透過 JIT 的方式加速 PHP 執行。 
● 運行速度已經超越 HPHPc。 
● 除了 PHP 外,還支援了 Hack Lang。 
● 只支援 x64 平台,未來會支援 ARM。 
● 目前 Facebook 的 PHP 程式皆運行在 
HHVM 上。
HHVM != PHP (Zend Engine)
HHVM 的執行效能?
fibonacci(40) 殘酷考驗
fibonacci(40) 
<?php 
function fibonacci($n) 
{ 
return $n<2?$n:fibonacci($n-1)+fibonacci($n-2); 
}
HHVM benchmark 
Less is better
HHVM Extension benchmark 
Less is better
但是開發 
HHVM Extension
有個東西一定要了解
Hack Lang
Hack = 強型態的 PHP
Hack Lang 
<?hh 
function fibonacci(int $n):int 
{ 
return $n<2?$n:fibonacci($n-1)+fibonacci($n-2); 
}
Hack Lang 
<?hh 
function print_nl(?string $n = null): void 
{ 
echo "$nn"; 
}
Hack Lang Reference 
http://goo.gl/PPB64m
HHVM Extension requirement 
● x64 OS 
● gcc >= 4.7.3 
● g++ >= 4.7.3 
● cmake >= 2.8.3
HHVM Extension 
寫起來很複雜嗎
只要三個檔案
就可以寫出 
fibonacci
config.cmake 
HHVM_EXTENSION( 
fibonacci fibonacci.cpp 
) 
HHVM_SYSTEMLIB( 
fibonacci ext_fibonacci.php 
)
ext_fibonacci.php 
<?hh 
<<__Native>> function fibonacci(int $n): int;
fibonacci.cpp 
#include "hphp/runtime/base/base-includes.h" 
namespace HPHP { 
static int64_t HHVM_FUNCTION(fibonacci, int64_t n) { 
return n<2?n:HHVM_FN(fibonacci)(n-2) + HHVM_FN(fibonacci)(n-1); 
} 
static class FibonacciExtension : public Extension { 
public: 
FibonacciExtension() : Extension("fibonacci") {} 
virtual void moduleInit() { 
HHVM_FE(fibonacci); 
loadSystemlib(); 
} 
} s_fibonacci_extension; 
HHVM_GET_MODULE(fibonacci) 
} // namespace HPHP
build extension 
$ hphpize 
$ cmake . 
$ make && make install
config.cmake 
HHVM_EXTENSION( 
fibonacci 
fibonacci.cpp 
foo.cpp 
bar.cpp 
other.cpp 
) 
extension 
name
config.cmake 
HHVM_EXTENSION( 
fibonacci 
fibonacci.cpp 
foo.cpp 
bar.cpp 
other.cpp 
) 
cpp files
config.cmake 
HHVM_SYSTEMLIB( 
fibonacci ext_fibonacci.php 
) 
PHP檔案只能一個 
而且命名必須是 ext_ 開頭
HNI (HHVM Native Interface) 
<?hh 
<<__Native>> function fibonacci(int $n): int; 
function echo_ fibonacci(int $n): void 
{ 
echo fibonacci($n); 
}
register extenstion 
static class FibonacciExtension : public Extension { 
public: 
FibonacciExtension() : Extension("fibonacci") {} 
virtual void moduleInit() { 
HHVM_FE(fibonacci); 
loadSystemlib(); 
} 
} s_fibonacci_extension; 
HHVM_GET_MODULE(fibonacci)
register extenstion 
static class FibonacciExtension : public Extension { 
public: 
FibonacciExtension() : Extension("fibonacci") {} 
virtual void moduleInit() { 
HHVM_FE(fibonacci); 
loadSystemlib(); 
} 
} s_fibonacci_extension; 
HHVM_GET_MODULE(fibonacci) 
定義函數
register extenstion 
static class FibonacciExtension : public Extension { 
public: 
FibonacciExtension() : Extension("fibonacci") {} 
virtual void moduleInit() { 
HHVM_FE(fibonacci); 
HHVM_FE(foo); 
loadSystemlib(); 
} 
} s_fibonacci_extension; 
HHVM_GET_MODULE(fibonacci) 
如果有還有其他 
函數接著定義
register extenstion 
static class FibonacciExtension : public Extension { 
public: 
FibonacciExtension() : Extension("fibonacci") {} 
virtual void moduleInit() { 
HHVM_FE(fibonacci); 
loadSystemlib(); 
} 
} s_fibonacci_extension; 
HHVM_GET_MODULE(fibonacci) 
讀取HNI
implement function 
static int64_t HHVM_FUNCTION(fibonacci, int64_t n) { 
return n<2?n:HHVM_FN(fibonacci)(n-2) + HHVM_FN 
(fibonacci)(n-1); 
}
implement function 
函數回傳值 
型態 
static int64_t HHVM_FUNCTION(fibonacci, int64_t n) { 
return n<2?n:HHVM_FN(fibonacci)(n-2) + HHVM_FN 
(fibonacci)(n-1); 
}
implement function 
函數名稱 
static int64_t HHVM_FUNCTION(fibonacci, int64_t n) { 
return n<2?n:HHVM_FN(fibonacci)(n-2) + HHVM_FN 
(fibonacci)(n-1); 
}
implement function 
參數型態定義 
static int64_t HHVM_FUNCTION(fibonacci, int64_t n) { 
return n<2?n:HHVM_FN(fibonacci)(n-2) + HHVM_FN 
(fibonacci)(n-1); 
}
implement function 
如果有多個參 
數接著定義 
static int64_t HHVM_FUNCTION(foo, int64_t n, const String &n2) 
{ 
// ... 
}
型態對應 
PHP Type C++ Parameter 
Type 
C++ Return 
Type 
void N/A void 
bool bool bool 
int int64_t int64_t 
float double double 
string const String & String 
array const Array & Array 
resource const Resource & Resource 
object const Object & Object 
mixed const Variant & Variant
以上官方文件 
都可以查到
但是
也只能查到這些了
如果要知道更多
RTFSC
實戰 Hhvm extension   php conf 2014
接下來要踩地雷了
Native Class
HNI 
<?hh 
class foo { 
protected ?mixed $bar; 
<<__Native>> 
public function __construct(mixed $bar): void; 
<<__Native>> 
public function getBarNative(): ?mixed; 
public function getBar():?mixed { 
return $this->bar; 
} 
}
register extenstion 
static class FooExtension : public Extension { 
public: 
FooExtension() : Extension("foo") {} 
virtual void moduleInit() { 
HHVM_ME(foo, __construct); 
HHVM_ME(foo, getBarNative); 
loadSystemlib(); 
} 
} s_foo_extension; 
HHVM_GET_MODULE(foo)
register extenstion 
static class FooExtension : public Extension { 
public: 
FooExtension() : Extension("foo") {} 
virtual void moduleInit() { 
HHVM_ME(foo, __construct); 
HHVM_ME(foo, getBarNative); 
loadSystemlib(); 
} 
} s_foo_extension; 
HHVM_GET_MODULE(foo) 
註冊 foo 
__construct
implement class method 
static void HHVM_METHOD( 
__construct, 
const Variant &bar) { 
this_->o_set("bar", bar, "foo"); 
}
implement class method 
static void HHVM_METHOD( 
__construct, 
const Variant &bar) { 
this_->o_set("bar", bar, "foo"); 
} 
對應 PHP 中 
的 $this
implement class method 
static void HHVM_METHOD( 
__construct, 
const Variant &bar) { 
this_->o_set("bar", bar, "foo"); 
} 
存入成員變數
implement class method 
static void HHVM_METHOD( 
__construct, 
const Variant &bar) { 
this_->o_set("bar", bar, "foo"); 
} 
$this->bar
implement class method 
static void HHVM_METHOD( 
__construct, 
const Variant &bar) { 
this_->o_set("bar", bar, "foo"); 
} 
$this->bar = $bar
implement class method 
static void HHVM_METHOD( 
__construct, 
const Variant &bar) { 
this_->o_set("bar", bar, "foo"); 
} 
指定目前的 
context 為 foo
implement class method 
static Variant HHVM_METHOD(getBarNative) { 
return this_->o_get("bar", false, "foo"); 
}
implement class method 
static Variant HHVM_METHOD(getBarNative) { 
return this_->o_get("bar", false, "foo"); 
} 
$this->bar
implement class method 
static Variant HHVM_METHOD(getBarNative) { 
return this_->o_get("bar", false, "foo"); 
} 
如果讀取 $this- 
>bar 
發生錯誤時是否要 
丟出 error
implement class method 
static Variant HHVM_METHOD(getBarNative) { 
return this_->o_get("bar", false, "foo"); 
} 
指定目前的 
context 為 foo
Type Casting
Type Casting 
var.toBoolean(); 
var.toDouble(); 
var.toString(); 
var.toInt64(); 
var.toArray();
Object Access
Object Access 
C++ PHP 
object.o_get(...) $object->... 
object.o_set(...) $object->xxx = xxx 
object.instanceof(class) object instanceof class
String Access
String Access 
C++ PHP 
string = “hello ” + “world” $string = “hello ” . ”world” 
string += “foo” $string .= “foo” 
string1 == string2 $string1 == $string2 
string[1] $string[1] 
string.substr(1, 2) substr($string, 1, 2) 
string.size() strlen($string)
Array Access
Array Access 
Variant var = array[1]; 
Variant var = array[String(“key”)];
Array Access 
C++ PHP 
array.set(1, var) $array[1] = $var 
array.set(String(“key”), var) $array[“key”] = $var 
array.append(var) $array[] = $var 
array = make_packed_array(1, 2, “val”) $array = [1, 2, “val”] 
array = Array::Create() $array = []
Resource
Resource data declare 
namespace HPHP { 
class InternalResourceData : public SweepableResourceData { 
public: 
virtual const String& o_getClassNameHook() const { return classnameof(); } 
DECLARE_RESOURCE_ALLOCATION(InternalResourceData) 
CLASSNAME_IS("InternalResourceData") 
InternalResourceData(FILE *file); 
virtual ~InternalResourceData(); 
FILE *getHandler(); 
private: 
FILE *file; 
}; 
}
Resource data declare 
namespace HPHP { 
IMPLEMENT_OBJECT_ALLOCATION(InternalResourceData) 
InternalResourceData::InternalResourceData(FILE *file) { 
this->file = file; 
} 
InternalResourceData::~InternalResourceData() { 
fclose(file); 
} 
FILE *InternalResourceData::getHandler(){ 
return file; 
} 
}
Resource usage 
static Resource HHVM_FUNCTION(open_file, const String &filename) { 
FILE *file = fopen(filename.c_str(), "r"); 
Resource resource(NEWOBJ(InternalResourceData(file))); 
return resource; 
} 
static String HHVM_FUNCTION(read_file, const Resource &resource) { 
FILE * file; 
InternalResourceData *resource_data = 
resource.getTyped<InternalResourceData>(); 
file = resource_data->getHandler(); 
// ... 
}
Reference Counting
Variables 
$a Value 
ref count = 1 
$a = “some value”;
Variables 
$a Value 
ref count = 2 
$b = $a; 
$b
Variables 
$a Value 
ref count = 1 
$b = null; 
$b
Variables 
$a Value 
ref count = 0 
$a = null; 
$b
Variables 
$a Value 
ref count = 0 
$a = null; 
$b 
delete value
HHVM Variables 
Variable Value 
Object ObjectData 
String StringData 
Array ArrayData 
Resource ResourceData
Increase reference 
Object var = some_object; 
Object *var = new Object(some_object); 
ObjectDara *value = some_object->get(); 
value->incRefCount();
Decrease reference 
Object *var = new Object(some_object); 
delete var; 
ObjectDara *value = some_object->get(); 
value->decRefAndRelease();
HHVM密(ㄉ一ˋ)技(ㄌㄟˊ)
HHVM 
Extension 第三方 Library set callback
HHVM 
Extension 第三方 Library call callback
接著就GG了...Crash 
HHVM 
Extension 第三方 Library call callback
都是 they 的錯 
gcc -O2
因為gcc -O2 預設開啟 
omit-frame-pointer
導致 HHVM rbp 指標出錯
解決方法
重新編譯第三方套件
gcc 加上 
-fno-omit-frame-pointer
但請放棄這個念頭
請改用神秘的
JIT::VMRegAnchor _;
VMRegAnchor 
static void HHVM_FE(someFunction) { 
JIT::VMRegAnchor _; 
//then call 3rd party library; 
} 
在呼叫第三方library之前加上這段就OK了。
取得Global Variable
取得Global Variable 
Array global(get_global_variables()->asArrayData()); 
Array _SERVER = global[StaticString("_SERVER")].toArray(); 
Array _GET = global[StaticString("_GET")].toArray(); 
Array _POST = global[StaticString("_POST")].toArray();
取得 Constant
取得 Constant 
#if HHVM_API_VERSION >= 20140829L //HHVM 3.3.0 
#include "hphp/runtime/ext/std/ext_std_misc.h" 
#else // HHVM 3.2.0 
#include "hphp/runtime/ext/ext_misc.h" 
#endif 
Variant some_conatant = 
HHVM_FN(constant)(StaticString("SOME_CONSTANT"));
呼叫 PHP 的某個函數
呼叫 PHP 的某個函數 
vm_call_user_func(StaticString("printf"), 
make_packed_array("%s %s", "hello", "world")); 
printf(“%s %s”, “hello”, “world”);
或是直接 
call native function
hphp/runtime/ext/*.h 
這裡幾乎實作了所有php的函數
Call native function 
#include "hphp/runtime/ext/std/ext_std_variable.h" 
HHVM_FN(print_r)(var); //print_r($var);
還有更多地雷由您來踩
有問題嗎?
Thanks

More Related Content

PDF
Cli the other SAPI confoo11
PDF
CLI, the other SAPI phpnw11
PPTX
PDF
Just-In-Time Compiler in PHP 8
PDF
Cli the other sapi pbc11
ODP
PHP Tips for certification - OdW13
PDF
Github.com anton terekhov-orientdb-php
PDF
Create your own PHP extension, step by step - phpDay 2012 Verona
Cli the other SAPI confoo11
CLI, the other SAPI phpnw11
Just-In-Time Compiler in PHP 8
Cli the other sapi pbc11
PHP Tips for certification - OdW13
Github.com anton terekhov-orientdb-php
Create your own PHP extension, step by step - phpDay 2012 Verona

What's hot (20)

PPTX
Introducing PHP Latest Updates
PDF
Boost.Python - domesticating the snake
PDF
What's new in PHP 8.0?
PPTX
C ISRO Debugging
PDF
Pl python python w postgre-sql
ODP
PHP5.5 is Here
PDF
PHP7 is coming
PDF
What's new in PHP 5.5
PPTX
Streams, sockets and filters oh my!
PPT
Php mysql
PDF
Quick tour of PHP from inside
PPT
Perl Intro 4 Debugger
PPT
PHP - Web Development
DOCX
Php questions and answers
PDF
Take advantage of C++ from Python
PDF
Notes about moving from python to c++ py contw 2020
ODP
The promise of asynchronous PHP
PDF
Building Custom PHP Extensions
PDF
Nikita Popov "What’s new in PHP 8.0?"
KEY
Yapcasia2011 - Hello Embed Perl
Introducing PHP Latest Updates
Boost.Python - domesticating the snake
What's new in PHP 8.0?
C ISRO Debugging
Pl python python w postgre-sql
PHP5.5 is Here
PHP7 is coming
What's new in PHP 5.5
Streams, sockets and filters oh my!
Php mysql
Quick tour of PHP from inside
Perl Intro 4 Debugger
PHP - Web Development
Php questions and answers
Take advantage of C++ from Python
Notes about moving from python to c++ py contw 2020
The promise of asynchronous PHP
Building Custom PHP Extensions
Nikita Popov "What’s new in PHP 8.0?"
Yapcasia2011 - Hello Embed Perl
Ad

Viewers also liked (20)

PPTX
Assessment 2
PDF
Who Invests in Hedge Funds in My State?
PDF
Research Paper by Dr Everett Ehrlich
PPTX
Officejet 100 mobile printer
PPTX
Security Enhancements in Windows Server 2012 Securing the Private - Cloud Inf...
PDF
Symfony簡介
DOCX
4. list of figure
PDF
2014: The Year Ahead for Hedge Funds
DOC
resume draft sheet
PPT
Sales Assist Group Presentation
PPTX
The art of seo
PPTX
FP Horak Wedding Presentation
PDF
The Game as Design Principle
PDF
Hedge Funds: Trends and Insight From the Industry and Investors
ODP
Admin generator
PDF
My Passion
PPT
T-Rex
PDF
How Passage of the JOBS Act Impacts Regulation D: Private Placement and Gene...
ODP
好康報報
PPTX
The marketing environment
Assessment 2
Who Invests in Hedge Funds in My State?
Research Paper by Dr Everett Ehrlich
Officejet 100 mobile printer
Security Enhancements in Windows Server 2012 Securing the Private - Cloud Inf...
Symfony簡介
4. list of figure
2014: The Year Ahead for Hedge Funds
resume draft sheet
Sales Assist Group Presentation
The art of seo
FP Horak Wedding Presentation
The Game as Design Principle
Hedge Funds: Trends and Insight From the Industry and Investors
Admin generator
My Passion
T-Rex
How Passage of the JOBS Act Impacts Regulation D: Private Placement and Gene...
好康報報
The marketing environment
Ad

Recently uploaded (20)

PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PPTX
E -tech empowerment technologies PowerPoint
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
PDF
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
PPTX
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
PPTX
presentation_pfe-universite-molay-seltan.pptx
DOCX
Unit-3 cyber security network security of internet system
PPTX
Funds Management Learning Material for Beg
PPTX
Power Point - Lesson 3_2.pptx grad school presentation
PPT
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PDF
An introduction to the IFRS (ISSB) Stndards.pdf
PPTX
innovation process that make everything different.pptx
PPTX
Internet___Basics___Styled_ presentation
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PDF
Paper PDF World Game (s) Great Redesign.pdf
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
E -tech empowerment technologies PowerPoint
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
presentation_pfe-universite-molay-seltan.pptx
Unit-3 cyber security network security of internet system
Funds Management Learning Material for Beg
Power Point - Lesson 3_2.pptx grad school presentation
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
PptxGenJS_Demo_Chart_20250317130215833.pptx
An introduction to the IFRS (ISSB) Stndards.pdf
innovation process that make everything different.pptx
Internet___Basics___Styled_ presentation
Introuction about WHO-FIC in ICD-10.pptx
Paper PDF World Game (s) Great Redesign.pdf
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
Slides PDF The World Game (s) Eco Economic Epochs.pdf
introduction about ICD -10 & ICD-11 ppt.pptx

實戰 Hhvm extension php conf 2014