Saturday, January 05, 2008

"Pure" mysqlnd interface feedback

After I posted my idea about a pure PHP mysqlnd driver, I received an email from Ulf Wendel from the MySQL AB team who works on the mysqlnd driver. He told me I could post his comments since he doesn't have a Blogger account. I'll include his comments below in blockquotes and then I'll comment below that.

Ulf Wendel writes:
What is "native"?

PHP and the Borg[1] seem to be relatives. Whenever a real PHP hacker find a useful C library in the universe he speaks the famous words "resistance is futile". A little later, the C library is available to PHP has become a PHP extension. This is one of the secrets of the early success of PHP: PHP is extentable. Extensions allow all the PHP drones to use the collective power of C libraries. As of today the PHP Function Reference shows 189 extensions [2]. Guess how many of them are written in C and how many of them are based on a C library...

To make it short: with PHP you can choose between C and PHP. Same with Lua: a scripting language (Lua) and C (the language Lua is implemented in) can be mixed whenever appropriate. That's the nature and the secret of both PHP and Lua. Whenever your coding bees hit a limitation of the simple to use scripting language you use the C level. Implementing the MySQL Client/Server Protocol[3] is such an example. It would be slow.

Compare that to Java. Is it common to extend the Java programming language? No, not really, very few people start hacking a Java virtual machine. And once you have hacked one virtual machine, what about portability and the other virtual machines out there. Alternatives? Well, if you enjoy cross-compiling, maybe... That's why you would never want to write a JDBC driver in any other language but Java. And, Java is more of a compiled language than PHP is, therefore it is fast enough.

Native for PHP can mean both: C and PHP. If its C - like with mysqlnd - you have to ask what external dependencies exist. The MySQL native driver for PHP (mysqlnd) gets all the infrastructure it needs from PHP itself. The driver is part of PHP, therefore mysqlnd runs on all platforms that run PHP.

Why using C?

You list some disadvantages of choosing C:

a) Platform-dependence not given

Mysqlnd runs on all platforms that run PHP. Mysqlnd is a part of PHP, it does use in particular:

- PHP memory management (limits really work now!)
- PHP data structures (less copy operations and memory savings)
- PHP Streams

b) Communication protocol inspection not possible

The MySQL native driver for PHP does use PHP Streams. PHP Streams feature hooks, aka Stream Filters [4]. With a little hacking you could expose the internal stream to the userland (PHP scripts). However, we favour MySQL Proxy[5] and therefore we have not implemented it.

To sum up: technically its possible but we have not enabled it. Tell us why you need it inside PHP and you have a fair chance to see it implemented.

Last but not least: have you ever worked on a raw binary network stream in PHP. Do your really want to know about the details of the protocol which Andrey started to love while he was implementing it? Or do you want to use something that is already there and might be the future standard: MySQL Proxy[5].

c) Deployment problems with no access to php.ini

First, with mysqlnd foundations have been laid to enable MySQL support by default: no license issue, no version issues, no external library dependencies. Its up to php.net and its community to decide if mysqlnd should be added to the default PHP configuration.

Second, if you choose a hosting service that does not configure/compile PHP as you need it, you do something wrong - honestly.

Anyway: all the above is minor stuff. The main reason is maximum integration into PHP for the best performance and easy deployment with no license issues.

[1] http://en.wikipedia.org/wiki/Borg_%28Star_Trek%29
[2] http://www.php.net/manual/en/funcref.php
[3] http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol
[4] http://www.php.net/manual/en/ref.stream.php
[5] http://forge.mysql.com/wiki/MySQL_Proxy
Now my comments to Ulf's comments:

First, thank you very much Ulf for your reply to my blog posting. I appreciate getting information "from the source" and it's important to get more information about libmysqlnd out to the community.

I want to reiterate that I think Libmysqlnd is the right solution for the PHP community, given the requirements of providing high-performance, quality extension with a PHP-compatible license. I look forward to libmysqlnd being part of the standard PHP distribution if the PHP community approves it.

I'm not lobbying to change libmysqlnd! I'm just supposing that a MySQL connector written in PHP code might also be interesting, even if it were not the preferred connector for MySQL server. It would be useful in a few circumstances, and also could be a debugging tool.

The performance advantage of C over of PHP is important. It stands to reason that an implementation of the MySQL protocol in a scripting language would be quite a bit slower.

However, it would be interesting to try it and measure the actual difference in performance, if for no other reason than understanding exactly how much performance advantage is achieved by using C. I understand from a user's comment that libraries exist in other scripting languages that implement the MySQL Protocol. Net::MySQL for Perl, Ruby/MySQL, and an unreleased Python library.

No comments: