https://engineering.fb.com/2014/03/20/developer-tools/hack-a-new-programming-language-for-hhvm/
Hack, PHP, and the HHVM (HipHop Virtual Machine) in a Nutshell
Introduction
PHP has long been the go-to language for web development, powering platforms like WordPress, Drupal, and even Facebook in its early days. However, as applications grew in scale, PHP’s dynamic nature presented challenges in performance and maintainability. Enter Hack and HHVM (HipHop Virtual Machine), Meta’s (formerly Facebook) solution to these issues.
This article breaks down Hack, PHP, and HHVM, explaining their roles and why they matter in modern web development.
1. What is HHVM (HipHop Virtual Machine)?
HHVM is an open-source virtual machine designed to execute PHP and Hack code efficiently. Originally developed by Facebook, it replaces the traditional PHP interpreter with a Just-In-Time (JIT) compiler, significantly boosting performance.
Key Features of HHVM:
- JIT Compilation: Converts PHP/Hack code into optimized machine code at runtime, improving execution speed.
- Asynchronous Execution: Supports async/await patterns for handling concurrent tasks efficiently.
- Hack Language Support: Fully supports Hack, Facebook’s alternative to PHP.
- Performance Optimizations: Faster than traditional PHP interpreters (Zend Engine), particularly for large applications.
- Type Checking (for Hack): Helps catch errors at compile time rather than runtime.
Fun fact: Before HHVM, Facebook used a PHP-to-C++ transpiler called HipHop for PHP (HPHPc), but it was later replaced by HHVM’s JIT approach.
2. What is Hack? (PHP, But Smarter)
Hack is a statically-typed superset of PHP, meaning it adds features like static typing while still supporting existing PHP code. It was introduced by Facebook in 2014 to improve code safety and maintainability without abandoning PHP entirely.
Why Use Hack?
✅ Static Typing: Unlike traditional PHP, Hack enforces (or allows optional) type annotations, reducing runtime errors.
✅ Improved Performance: Works efficiently with HHVM, making it faster than vanilla PHP.
✅ Asynchronous Programming: Native async
/await
support for better handling of concurrent tasks, useful for web APIs.
✅ Collections & Generics: Advanced data structures (Vector
, Map
, Set
) and generics for better type safety.
✅ Gradual Adoption: Can be incrementally introduced into PHP projects without a full rewrite.
Example: Hack vs. PHP
PHP Code:
|
|
Hack Equivalent:
|
|
With Hack, passing a string instead of an integer would raise an error before execution, avoiding unexpected behavior.
3. How Does HHVM Improve PHP Performance?
Traditional PHP runs on the Zend Engine, which interprets scripts line-by-line. This is fine for small applications but slows down large-scale services.
How HHVM Boosts Performance:
- JIT Compilation: Instead of interpreting code repeatedly, HHVM translates it into machine code for faster execution.
- Opcode Caching: Stores compiled bytecode in memory, reducing the need for repeated compilation.
- Efficient Memory Management: Optimizes memory use, reducing CPU load.
- Async Execution: Handles multiple tasks concurrently, reducing waiting time for I/O operations.
Benchmark: HHVM has historically been 2x faster than PHP 5.x and provided comparable performance to PHP 7.x+ in many cases.
4. PHP vs. Hack: Should You Switch?
Feature | PHP | Hack |
---|---|---|
Typing | Dynamic | Static & Dynamic (Gradual Typing) |
Performance | Slower (Zend Engine) | Faster (HHVM) |
Async Support | Limited | Built-in async/await |
Compatibility | Universal | HHVM-only |
Used By | WordPress, Laravel, Drupal | Facebook, some enterprise apps |
When to Use Hack & HHVM:
✅ If you’re working on a large-scale application (e.g., social media, real-time services).
✅ If you need better type safety but don’t want to abandon PHP completely.
✅ If you want to leverage HHVM’s speed improvements.
When to Stick with PHP:
❌ If your project relies on WordPress, Laravel, or other frameworks that don’t support HHVM.
❌ If you need compatibility with standard hosting providers (Hack/HHVM is niche).
❌ If PHP 7.x/8.x already meets your performance needs.
5. The Future of HHVM and Hack
Initially, HHVM aimed to be a faster alternative to PHP, but as PHP improved (especially with PHP 7+), HHVM pivoted to focus exclusively on Hack. Meta has moved away from supporting HHVM for generic PHP use.
Current Status:
- HHVM no longer supports vanilla PHP (since HHVM 4.0).
- Hack is still maintained by Meta but has limited adoption outside of Facebook.
- PHP remains dominant for general web development, while Hack is mostly used in-house at Meta.
Key Ideas Summary
Feature | Summary |
---|---|
Hack | A statically-typed superset of PHP with improved performance and async support. |
HHVM | A Just-In-Time compiler for PHP/Hack, significantly improving execution speed. |
PHP | The most widely used web scripting language, improved significantly with PHP 7+ and 8+. |
Hack vs. PHP | Hack is faster and safer but limited to HHVM, while PHP is more compatible. |
HHVM’s Future | No longer supports PHP, focusing solely on Hack. |