Archive for February, 2008

MySQL+PHP vs XSLT mini rumble

Friday, February 29th, 2008

I use XML+XSLT instead of MySQL+PHP on 2 of my websites. For some reason I always imagined XSLT is fast. Yesterday I added something made with X+X combination to QUBIDRAW also so I decided to do a small comparison because my “is fast” assumption was based on.. well, nothing.

I made two scripts called tablepush that pushes some tabular data and shape it into html table. One script uses basic mysql functions and then php to do the html shaping:

<?php
$link = mysql_connect('localhost:3307', 'root', 'rootp');

mysql_select_db('mlvsx');

$query = 'SELECT * FROM '.$_GET['data'];
$result = mysql_query($query);

echo "<table>";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "<tr>";
    foreach ($line as $k => $col_value) {
		$tag = '';
		if ($k == 1) $tag = 'strong';
		else if ($k == 2) $tag = 'em';

		$t1 = ''; $t2 = '';
		if ($tag) {
			$t1 = "<$tag>";
			$t2 = "</$tag>";
		}

        echo "<td>{$t1}{$col_value}{$t2}</td>";
    }
    echo "</tr>";
}
echo "</table>";

mysql_free_result($result);
mysql_close($link);
?>

The other loads XML and uses XSLT to turn it to html table:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" encoding="iso-8859-1" indent="no"/>

	<xsl:template match="things">
		<table>
		<xsl:apply-templates />
		</table>
	</xsl:template> 	

	<xsl:template match="thing">
		<tr>
			<td><xsl:value-of select="@id" /></td>
			<xsl:apply-templates />
		</tr>
	</xsl:template> 	

	<xsl:template match="aaa"><td><strong><xsl:value-of select="." /></strong></td></xsl:template>
	<xsl:template match="bbb"><td><em><xsl:value-of select="." /></em></td></xsl:template>
	<xsl:template match="ccc"><td><xsl:value-of select="." /></td></xsl:template> 	

</xsl:stylesheet>

Results using http_load to do the load test on my local (older) computer are :

Table with 3 rows

my-php : 25 requests / sec
X-X : 49 requests / sec

Table with 50 rows

my-php : 21 requests / sec
X-X : 39 requests / sec

Table with 350 rows

my-php : 11 requests / sec
X-X : 13 requests / sec

So it seems XSLT is quite fast. I will keep using it for the cases where it makes sense (like a lot of discrete data with known and non-frequent relations). It’s funny to me that XSLT is in a way quirky and hackish and yet elegant. you can find complete sources and data to do the test here: LAMP XSLT .

It would be interesting to test some other platforms too…

Functional programming & immutable objects explained — IRC style

Saturday, February 23rd, 2008

I was on #ocaml channel on irc.freenode.org. (btw - freenode hosts channels for a lot of popular languages and technologies). This was a while back when I started making OCaml SDL examples that you can find on this blog. I think this short IRC chat told me more about FP than anything I read before.

<middayc-> when I read about FP I read that things should be immutable and that there should be little state or side effects… but if I am making a game I need states all around the place … a trivial example, I need an x,y and velocity vector of every bullet, particle effect, main character, enemies with more states regarding to their behaviour .. etc… is this ok then or I don’t get something

<hcarty> For the velocity vector example - one way to think about it would be to have your update function return a new “bullet” rather than modifying the old one.

<hcarty> But it’s a style choice, and OCaml lets you have it both ways

<middayc-> :) that is an interesting view (new bullet every time)

<Yoric[DT]> But OCaml is often able to optimize this “create a new bullet each time” to “reuse the same bullet”.

<RobertFischer> middayc- Whenever possible, use many small, rapidly-GC’ed data elements.

<RobertFischer> Ocaml is highly optimized for that kind of behavior.

<RobertFischer> All of your ex-Java (or whatever) training which says that “object creation is expensive” needs to go out the window.

<RobertFischer> You’re generally best off using records or tuples (as appropriate) and minimizing the amount of mutable data you have. This will give you the best performance in Ocaml.

<middayc-> aha … yes I was thinking about GC in that way

<RobertFischer> Thinking in terms of list/graph processing instead of message passing is also a major performance improvement.

<RobertFischer> So, when you have a bunch of sprites and a change in the moment in time, don’t think of telling each and every sprite to update itself — that’s the OO way to approach the problem.

<ita> does the use of closures instead of classes change anything speed-wise?

<middayc-> huh I have no idea what is list/graph processing …

<RobertFischer> Do you know what a graph is?

<middayc-> no

<RobertFischer> A graph is a data structure of data structures.

<middayc-> aha .. like a tree

<middayc-> like list is flat — a graph is structured?

<RobertFischer> Yes, where each node is a data structure. So a list of lists, a tree of lists, a tree of maps of lists of map-list tuples.

<RobertFischer> Whatever.

<ita> middayc-: {vertex, arcs}

<middayc-> aha … I begin to understand what you mean by list/graph processing vs object.update()

<RobertFischer> The more you can just tell OCaml (or any FP language), “Here’s how to process each element of that graph, and here’s the graph to process”, the better off you’ll be. See fold and iter of examples of that.

<middayc-> cool … you should write a book … you explain it in a way I can quickly understand

<RobertFischer> If you think of your state as a graph of elements, and can define a function which changes your state and returns the new graph of elements, then you’re going to be digging around at the best performance in Ocaml.

<RobertFischer> I’m doing a podcast and writing a blog.

<RobertFischer> I’ll work on a book later. :-D

<middayc-> aha … a new graph each time?

<middayc-> I have to invert my brain from stuff I am used to and then it all makes sense

<RobertFischer> Pretty much.

Thanks guys for explaining it to me so well! Since then I wrote 3 examples of using SDL with OCaml and I used no mutable state and felt no need to use it. I like the code I wrote with this paradigm a lot. I will continue building a game in OCaml and see what happens next.

I just got smarter by a Factor!

Wednesday, February 13th, 2008

I finally took some time to play with Factor. I approached it with some scepticism because after looking at various examples I wasn’t sure if I really want to shuffle the stack instead of use the ( old boring ) variables.

This is an much repeated example of sq (square) word (function) in Factor.

: sq dup * ;

Well, I came with scepticism but left enthusiastic. When I was thinking out cutoff word I thought to myself that factor is cool as a brain exercise, like tetris, but that it’s probably more practical to just use something less exciting where you don’t have to think so much to write a simple function. At 3-rd word getto code sort of fell together in few seconds and I tried it and it even worked. From then on things just got nicer and nicer (see the source file below).

: cutto ( sub str1 -- str2 ) dup >r dup length >r start r> r> subseq ;
: cutoff ( sub str1 -- str2 ) dupd cutto dup >r length swap length swap r> subseq ;
: getto ( sub str1 -- str2 ) tuck start 0 swap rot subseq ;
: cut-to swap cutto ;
: cut-off swap cutoff ;
: get-to swap getto ;
: snatch-to dupd get-to swap ;

What I want to do here? I want to prepare few words that will help me extract stuff out of a string in a reasonably elegant way.

( scratchpad ) "<A c=123&..><B c=234&..><C c=345&..><D c=456&..><E c=567&..>"
 			"<B" cut-to "c=" cut-off "&" snatch-to
 			"<D" cut-to "c=" cut-off "&" get-to .s
"234"
"456"

I don’t know, but this seems very elegant to me.

click to see the full progress, comments and usage examples of these words

After I posted it on IRC channel #concatenative elasticdog quickly posted a lot more elegant variations of my tree base words:

: cutto tuck start tail ;
: cutoff swap split1 nip ;
: getto swap split1 drop ;

Beauty!!!

No Rails for me

Sunday, February 3rd, 2008

Source: FlickrI believe there is a lot of Slick in Ruby on Rails. People that are smarter than me like it so there must be. So I finally looked at doing a project in it, but decided I won’t use it after all. I like library approach more than framework one. That is the core problem why I kept away from all the Rails-like systems for so long.

So what don’t I like? I don’t want to learn or use some framework specific abstractions for things that stand on their own. I want to see XHTML as XHTML, client side JS as JS and work with DB via SQL or in a SQL like manner. Second… I looked at example code and it was nice but I like how I code right now more.

//RoR example from onlamp.com
def update
@recipe = Recipe.find(params[:id])
@recipe.date = Time.now
if @recipe.update_attributes(params[:recipe])
flash[:notice] = 'Recipe was updated.'
redirect_to :action => 'show', :id => @recipe
else
render :action => 'edit'
end
end

//this is how I do something like it in php
function update($d)
{
if ($this->db->update(array(
'set' => array_merge($d, array('date' => '#noQuote#NOW()')),
'where' => "id = {$d['id']}"
)))
$this->addMsg('Recipe was updated.');
}

I like Ruby’s syntax 10x more than PHP’s and Ruby is much more advanced (in ways that I care) as a language. But there are several problems why I personally don’t want to code as it is coded above.

  • Why do I have to find the record, change it and store it back? Databases have UPDATE right?
  • Why use a external language’s API method for creating current date to store in a DB (and think about formats and all sorts of stuff) if DBs have NOW() and other functions for this?
  • Can one action create only one flash notice?
  • Why does a method that makes modification to the database (a backend method) handle where user will get redirected after the action??? This is total crap, this method should only update the DB and not have any effect on the frontend, so it can be called from anywhere you need it (admin panel, some ajax call, RPC, frontend..) … I didn’t even bother digging into MVC because I found it too extreme and impractical from what I saw on the surface and my code never does that.
  • How can it be that DSL-s (domain specific languages) are oh-so-cool and at the same time ORM’s, Active Records and similar stuff that puts wrappers around them is also oh-so-cool?
  • Isn’t Active Record so MS Access 98?

I am not saying RoR is crap. First, there were hundreds of MVC+templates+ORM+… frameworks out-there before RoR and RoR obviously did many things better because now there are thousands of (RoR-ishy) frameworks there. People do report massive productivity boosts in using it. And productivity is the whole point of improving various development routines and methodologies. So, live and let live…

(edit: zemantified also)

PHEW to PHP !

Friday, February 1st, 2008

I did all web-dev in PHP last years. Because PHP deployment is “there”. It proved itself as a practical solution from the smallest shared hosting websites to the huge ones like Flickr.

But PHP as a language is not such pearl and built-in API is very inconsistent. I keep checking out a lot of non-typical PLs and see a lot of good concepts. I would like to find a real web-project to use them on, and make a step forward in my PL experience but I have a tons of PHP code to write right here.

I got depressed about this yesterday, and then I started doing something that I will probably find is a waste of time, but anyway… I started hacking a PHP script that “compiles” some made up language to PHP. I named it PHEW and it even somewhat works… Basically it’s a PHP-ish “language” with modifications where things itched me when working with it..

source PHEW code:

class Users extends Base

	fun getFullName(u)
		return u\:name . " " . u\:surname;

	fun show_hi()
		var usr = #getUserById(@@:auth.getUserId());
		return "Hi {{#getFullName(usr)}}!";

	fun getUserById(id)
		return DBs::selectRow(@@:dbc, [
			:from = @@:t\:users,
			:where = "id = {id} " ,
		]);

generated PHP code:

classUsers  extends Base {

	function getFullName($u){
		return $u['name'] . " " . $u['surname'];

		}
	function show_hi(){
		$usr = Users::getUserById($_GLOBALS['auth'].getUserId());
		return "Hi " . (Users::getFullName($usr)) . "!";

		}
	function getUserById($id){
		return DBs::selectRow($_GLOBALS['dbc'],  array(
			'from' => $_GLOBALS['t']['users'],
			'where' => "id = {$id} " ,
		));

Click to see sample, generated code and a list of changes

Well it’s all in flux, I will keep playing with it probably and see if anything useful comes out.