Today saw a post from Steve on his blog about ” Fast String Multiplication with Regular Expressions, Exponential Concatenation, and Binary Interpolation“. Basically, it deals with how to fast multiplicate a string n times in javascript.

When I first saw the post, only 3 algorithms were presented. Looking at the 3rd algorithm, I think it can receive some improvement if exponential concatenation is used more efficiently. By the time I implement the improved version, Steve added a 4th one to his post, which is quite similar to what I have here:

function mul5(str, n) {
 	var r=[];
 	while(n){
 		if(n%2)
 			r.push(str);
 		str+=str;
 		n >>=1;
 	}
 	return r.join('');
}

The main difference is that it is more compact. My tests show that, in Firefox and Safari 3.0.4, mul5 is as fast as mul4. However, similar to mul4, mul5 is about 30% slower than mul3 in IE (mult5 performs as good as mul4 in IE).

The reason of slowdown in IE compared to mul3 is this part: str+=str. IE does not handle that direct string concatenation well if the string is quite long. What if we use array.push there as well? After several fail-and-error, I found out one solution which will actually boost the performance for IE:

function mul5(str, n) {
 	var r=[],t=[str];
 	while(n){
 		if(n%2)
 			r.push.apply(r,t);
 		t.push.apply(t,t);
 		n >>=1;
 	}
 	return r.join('');
}

As shown above, the trick is to use array.push.apply(array,anotherArray) to add all elements in anotherArray to array. This IE version is as good as mul3, or even slightly faster than it.

However, this modification will slow down other browsers. We can use conditional compilation support in IE to work around this. The merged version is like this:

function mul5(str, n) {
	var r=[];
	/*@cc_on //for IE
	var t=[str];
	while(n){
		if(n%2)
			r.push.apply(r,t);
		t.push.apply(t,t);
		n >>=1;
	}
	return r.join('');
	@*/
	while(n){
		if(n%2)
			r.push(str);
		str+=str;
		n >>=1;
	}
	return r.join('');
}

If using dojo, the conditional compilation should be changed to just test dojo.isIE. (conditional compilation is not supported by dojo builder, so it will be lost when building dojo)

Try it yourself here. Changes compared to the original one:

  1. mul1 and mul2 are slow, so removed from the test
  2. the number of repetition and the length of the input str are slightly different from the original one
  3. A table is used to present the result, rather than a list

After 4 years of PhD study, finally I passed my viva (or defence in US) on Wednesday 14, Nov, 2007.

Thanks first go to my family, including my parents, who  made my PhD study possible finacially and they are always ready to encourage me whenever I am struggling with whatever issues. My wife, (was my girlfriend) , also support me throughout my PhD study.

This research was made possible under the excellent supervision of Prof. Andy Tyrrell and Dr. Julian Miller. I would like to thank them for their continuous support and guidance of the project as well as their suggestions and direction. In addition, I highly appreciate the efforts they exerted to find financial support for my further research.

Credits should also go to my friends, labmates, thanks for the assistance and suggestions, as well as the laughs and times shared with me.

I think this should be the last degree in my life, or anyone know is there a higher degree to pursue? (I know postdoctor is not considered as a degree, but rather a job) Although, even if there is a higher one, I doubt whether I am willing to consider it…

I got married several months ago, but did not have time to put up all the photos to share with my families and friends all over the world.

Just installed Gallery 2 and WPG2 for Wordpress and all wedding photos are all uploaded. However, you do need a login/password to access them. If you know me personally, please ask me for that.

After re-installing php 5, I got a wired error when starting apache 2:

Cannot load /.../libphp5.so into server: /.../libphp5.so: undefined symbol: _efree

Google suggested that, lots of people have the exact error, but not a single page contains what caused this error or how to solve it. In addition, this error also happens with php 4 and apache 1.

After some hard time Trial and error, I found the issue: due to some reasons, the make install to install php 5 does not actually copy over the libphp5.la files to the right place. After manually copying the la file, apache 2 can be started happily with php 5 enabled.

Edit: Ivan reported that instead of the above,

make clean

will make it work for his case. Thanks. – Dec 21 2008

Dojo project contains a lot of facilities, however not all of them are useful for a given user. We provide a build system which removes inline comments and rename local variables with shorter names (3 characters), among other optimization.

Even after this building procedure, the resulting dojo.js can still be a bit big: for a normal widget build, the size is 170k (dojo trunk).

Recently when searching for IE hover css selector support, I revisited Dean Edwards’s website. Packer project attracted my interest and I decided to give it a go with the built dojo to see whether further compression can be achieved.

The result is quite promising: after packing, the resulting widget build of dojo is almost 50% smaller. The compression ratio is 99893/170258= 58.7%.

Looking at the packed version, all the comments and source formating new lines are removed. Some other compression is happening as well, but as I did not investigate its source code, I do not know exactly what it does, but it seems to me that by storing the publich API name and all the strings in the source at the last, it achieves some extra compression ratio. (Don’t worry, the compression does not change the meaning of the javascript code it packs, and I tested that in one of the applications I am working on.)

As the source code is in javascript, it should be simple enough to incorporate this to the dojo builder. Probably this should be the last "filter", before writing the output file: for all the source js files, they should be passed through this packer, and then copied to the release dir, so does the resulting dojo.js.

Sometime ago I talked about making eZ publish 3.x happy with fastcgi. The original approach is to patch eZ Publish so that it works with fastcgi correctly. However, Xavier Dutoit pointed out that, given some special treatment in lighttpd config files, no modification is required to eZ publish.

The trick is the following block:

url.rewrite-once = (
"^/.*\.(css|html|htm|pdf|js|png|gif|jpe?g)$" => "$0",
"^/.*?(\?.*)?$" => "/index.php$1"
)

For more about lighttpd and php4/php5 config instructions, please consult this thread.

Mysql 4.1 is available in the new server from migration. The transition from 4.0 to 4.1 is quite transparent as long as I do not try to set the default encodings of the db/tables to UTF-8.

However, as the proper way is to set the encodings in mysql 4.1 correctly, I decided to do it sooner rather than later.

According to the document, if UTF-8 was saved in the mysql 4.0 db although the default encoding is not UTF-8, then:

However, you should avoid trying to convert directly from latin1 to the "real" character set. This may result in data loss. Instead, convert the column to a binary data type, and then from the binary type to a non-binary type with the desired character set. Conversion to and from binary involves no attempt at character value conversion and preserves your data intact.

As it turns out, wordpress 2.1-alpha actually just save the text as latin1 strings, so no need to convert the fields to BINARY first, setting charset to UTF-8 on those fields is sufficient.

An sql file is prepared for doing this. (Proceed at your own risk and always backup first)

If you are wondering how to add the fancy format dropdowns, fontsize dropdowns among others, to the default dojo look & feel toolbar template, look no further, here it is!

The installation of this is the same as for the FCKeditor style toolbar template. (You do need a full source tree/checkout of dojo svn)

An online demo is also available.

Thanks to Bruce Webster from Interact Learning Community Environment for contributing this full featured Editor2 toolbar template and corresponding stylesheets/images.

As this blog is hosted by SCIM sponsor AWTOHOST, with the migration of SCIM website to a faster server, this website is now also benifitial from this migration. In addition, this solved another outstanding issue: the plugin Akismet now can connect to the master server properly. It seems the old scim host is blocked by Akismet server.

Big thank you to AWTOHOST again.

Sometime ago, someone asked about smoothscroll support in dojo. While working on index list of Editor2, I found myself in the position of requiring such a mechanism, so that rather than jumping to an element, scrolling smoothly to the element gives user a better feeling of context.

Rather than implementing a widget directly, as asked for in the linked post, a dojo.lfx animation shall be created first. Although the core of the smoothScroll is simple to figure out, I had little knowledge about the dojo.lfx code base. After inspecting dojo.lfx.html.propertyAnimation and resource dojo.lfx.Animation, I got the idea of what the animation framework dojo.lfx provides.

(more…)

« Previous PageNext Page »