5

I started to read about HHVM and Zephir. I was very excited at first, that performance jumps up thanks to this technologies. But then I started to ask myself a question. If It really gives you such performance like owners of this technologies says.

Now I am going to say, how do I think it looks like. Correct me, if I am wrong.

Php Script Live Process: Run Script -> Compile -> Execute Script

The obvious problem here, is with compiling. We need to compile our script every time it has been started.

Let's pretend I have Apache2 installed and I am running my php script named "test.php" with:

APC: APC turned on and APC option to check for changes off. It should looks like that:
Run Script -> Execute Script
Because it's already compiled to our memory and ready to use.

HHVM: Hip Hop Virtual Machine, would behave similar to APC. The difference here is, virtual machine, which for standard PHP with APC is Zend Engine. So it goes like that:
Run Script -> Execute Script
Because it's already compiled (in memory?) and ready to use.

Zephir: I compiled my script to C extension. So I guess it doesn't need compiling? (I am not sure about this). So the functions inside my php script are native ones now. So, if I write another script named "test2.php", which will run my native functions which were in "test.php" (Yes it should have .zep format, but it doesn't matter for now). Now using APC I avoid compiling process.
Run Script -> Execute Script

And now I am confused. As far as you can see it's all the same now. I dont see any kind of advantages of using Zephir and HHVM over standard APC. So I would like to hear, if I am correct or totally wrong?

Dariss
  • 1,258
  • 1
  • 12
  • 27
  • 1
    Stop thinking in terms of APC for opcode caching, and think of OpCache, which also optimises – Mark Baker Dec 04 '14 at 22:33
  • 3
    PHP Code that is stored in opcache is still not executable as native machine code, it's bytecode that still needs to be executed by the PHP virtual machine; whereas Zephir and HHVM create actual machine code that will execute directly – Mark Baker Dec 04 '14 at 22:35
  • @MarkBaker So OpCode still need to be converted to bytecode and it's done by virtual machine (like Zend). Then it's executed. On the other side HHVM, keeps bytecode ready for execution. That's why it's faster. But here's one problem. Zephir should be used to write vendors, some small parts of application. So around Zephir code, there will be clean PHP code, which still need to be changed to opcode and then to bytecode. So my question is, is really Zephir worth using, seems like HHVM is much better? Or I missed some point here? – Dariss Dec 05 '14 at 09:16
  • 2
    Zephir is definitely worth it for writing small library routines that are called frequently as part of a larger PHP application, particularly when the native PHP task is process intensive.... a small routine that is called 10,000 times during the execution of a script, but that takes 100ns to execute in PHP, converted to a Zephir library may take 60ns to run each iteration.... that would be a saving in script execution of 10,000 * (100-60) = 400,000ns, or 400ms... for every execution of that script – Mark Baker Dec 05 '14 at 10:23
  • 2
    The other point you're missing is that HHVM isn't simply a plug-in replacement for PHP, it's an infrastructure component as well, replacing your webserver too.... so any configuration for your Apache (or whatever webserver) you're using, will need to be reconfigured for HHVM... that's additional learning, and (unless you manage your own servers (when you're going to need to manage the installation and configuration) you're going to have to find a hosting service that will let you run it – Mark Baker Dec 05 '14 at 10:25
  • With HHVM, you also still have a compile phase.... and it can take many minutes to compile; but you compile the application before deploying it.... effectively you're deploying a machine-language executable.... so every change you make to your script requires recompilation before you can run it and test that it works – Mark Baker Dec 05 '14 at 10:28
  • 1
    Zephir libraries also require this compilation in advance... compile then deploy and test, so developing with both HHVM and with Zephir is a lot slower process than it is with PHP where every change is immediately executable (assuming bytecode caches are configured to check for changes in your development environment) – Mark Baker Dec 05 '14 at 10:30
  • 1
    Okey, seems logic to me now :). Thank you @MarkBaker very much, for explaining it to me. One more question. Do you think, this two projects can develop parallel? And which one for now is worth to study? Yes I know it depends on your needs, but still which you suggest? – Dariss Dec 05 '14 at 12:04
  • 2
    Personally, I've played with HHVM, but not done anything serious/production quality running on it.... I have been using Zephir to convert some of my libraries to extensions, and seen some real benefits in performance as a result, and (with additional work on using native C datatypes rather than PHP zvals) some memory improvements as well.... my Zephir Tries class loads a dictionary in 42 seconds compared with 89 seconds for a native PHP version of the same code - https://github.com/MarkBaker/Tries though searching the dictionary is about the same (though a Trie is optimised for that search) – Mark Baker Dec 05 '14 at 12:18
  • 1
    I've also found that working with Zephir has given me a better understanding of PHP internals, which has led to some improvements in the way I code native PHP; so my personal recommendation would be to study Zephir – Mark Baker Dec 05 '14 at 12:22
  • Thank you very much. You were very helpfull. I am starting my adventure with Zephir ;) – Dariss Dec 05 '14 at 12:26
  • @MarkBaker I wouldn't necessarily say HHVM is a web server replacement. Using FastCGI with Apache or Nginx means you can still take advantage of your normal web server configuration for request redirection, auth/access, logging etc. In the Apache sense it is more of a mod_php replacement when used in its FastCGI daemon mode (which the HHVM devs recommend these days). – ndavison Dec 05 '14 at 21:30
  • @Zephir, What do you mean by "study Zephir"? Do you mean using it or do you mean studying it's C source code? – Pacerier Feb 12 '15 at 15:01

3 Answers3

2

Zephir can't handle high level objective oriented code. Speed of your application will be worse than clean PHP way.

Here you can find comparsion of clean PHP vs Zephir. https://github.com/dgafka/zephir-BookStore

So, if you want to rewrite your application to zephir, think twice before you will do it.

https://github.com/phalcon/zephir/issues/694#issuecomment-67987616

Dariss
  • 1,258
  • 1
  • 12
  • 27
0

Zephir is pretty fast (of course, as it is compiled to a C extension)! https://www.simonholywell.com/static/files/2014-02-28/index.html

Hack is pretty fast too, less than Zephir, but on the other hand it's far more mature/active/documented than Zephir.

The main advantage of using Zephir/Hack is that you get static typing as a bonus. It is a real relief to add that kind of security to PHP code.

Dynamic typing has its limits, and permits too much ugly/dangerous things.

Lideln Kyoku
  • 952
  • 9
  • 20
0

Use Zephir for abstract functional coding.

For example, a library of functions that do string formats specific to your project. All advance PHP OOP (business logic) and such should be done within PHP.

Simple as that!
Use the tool that matches the project.

tfont
  • 10,891
  • 7
  • 56
  • 52