I had a application using wxWidgets 2.8.0 and then 2.8.8 in production. There were some bugs in earlier wxWidgets versions on Linux, so printing was not working properly. I decided to upgrade wx and that fixed it. Now I wanted to use the same version for Windows version of my application. I originally used some (now old) MinGW version and just wanted to rebuild and be done. But, I got build errors instead. I don’t really last time wxWidgets failed to build so I asked at mailing list and finally dug into the source code myself.

It looks like wx code is all fine, but there are problems in MinGW headers. I particular, you need to edit the file C:\MinGW\include\winspool.h and change DocumentPropertiesW function’s signature from:

LONG WINAPI DocumentPropertiesW(HWND,HANDLE,LPWSTR,PDEVMODEA,PDEVMODEA,DWORD);

to:

LONG WINAPI  DocumentPropertiesW(HWND,HANDLE,LPWSTR,PDEVMODEW,PDEVMODEW,DWORD);

It seems to be already fixed in newer MinGW versions.

I have a server where nginx is used as frontend for Apache. nginx serves static content and Apache serves PHP pages. This is a common setup.

Today I migrated stuff to a new server and needed to copy a 7GB database file to another server. I figured HTTP would be fastest way to do it. Unfortunatelly, DNS change already went thought so I could not serve the file on the static domain nginx was configured for.

I thought “nevermind”, placed file under one of those domains handled by Apache and started the download. It was going fine at 11MB/s for some time. However, soon it started to crawl at 850KB/s. I suspected network problems, but everything else was running fine. I looked at process list and whoa, nginx using 99% of CPU. Because of this single download, the server was brought to its knees and no other client could even get a simple “Hello World” page.

I stopped the download at the client side and nginx soon recovered (not restart needed). Then I edited /etc/hosts and place the old IP address of static domain and continued the download (wget -c). It finished few minutes later with 11MB/s average.

Both me and my colleague work separately working on same git tree while being offline for a couple of days. Result: following “git pull” I got a huge conflict spanning about 100 of files.

This meant that manual resolution is out of question. Enter “git mergetool” and “kdiff3”. I installed kdiff3 from linuxpackages.net (version is for old Slackware 11.0, and I had to symlink /opt/kde/kdiff3 to /usr/bin/kdiff3 so that git finds it).

git-mergetool calls kdiff3 for each file, you merge and save. Job done very quickly.

Seems to be a fine day at Google today, perhaps engineers are pulling hair.

This morning, I was looking at a spreadsheet in Google Docs and suddenly some 20 values simply vanished right before my eyes. I wasn’t even working anywhere near that part of the sheet. I was inserting new values at bottom and somewhere in top-right corner the values were gone. I tried undo and to scroll around (big sheet) and only when I switched to another sheet and came back the values showed up again. Phew. From now on I’m doing export and download to my computer every time I finish editing.

Few hours later, a new issue. Looking at a spreadsheet I selected Save from the menu. It said that it’s ok. I did some changes, clicked Save, got no error but the screen read “Last saved 2 minutes ago”. Ok, maybe it’s just a minor glitch. 15 minutes later I tried to save again. Once again, no errors, but it still says “saved 17 minutes ago”. At this point I was confused whether save is not possible or the message is simply wrong. I exported the document to xls format, checked in OpenOffice and then closed the browser tab.

Three strikes and blog post is out. I just had another issue, now with GMail, so I guess it’s time to make all this public. I wrote an e-mail message and it said “Your connection to GMail has expired. Please log in again.” Ok, it’s not like I haven’t seen that one before, but it’s been almost a month. I though they had it fixed. I logged out, logged back in and… it still does not allow me to send an e-mail. I can read messages fine, but as soon as I try to post, I get a warning that “Your connection to GMail has expired. Please log in again.”.

Oh well, I guess we get as much as we payed for it ;)

I have been stackoverflow user almost from the very start of the website. I recall reading some Jeff Atwood’s blog posts and thinking how naive he is. He has a classic case of Microsoft fanboy-ism. He swears by .net and MSSQL server and spits on Linux, PHP and… well… entire LAMP stack.

When stack became popular the website started to get a lot of traffic and Jeff was all like “Oh we don’t need all the scaling technology that all the web companies have developed since web 1.0 till today. We’re smarter, we use the all-powerful Microsoft stack, we’ll just buy more RAM, more CPU and keep it all on single machine. Machines are so powerful these days and cost almost nothing”. How little did he know.

As more and more people use the website it seems that they reached the limit of what is possible. Stack website is now inaccessible for days. By inaccessible, I don’t mean that the site does not open. It just open waaaay too slow to be usable. I sometimes wait 5 minutes to get the home page.

What I really regret is all those dumb readers on Jeff’s codinghorror blog, and all those fanboys on stack website. Some people tried to tell Jeff that this would happen, but he would not listen. He was very arrogant and dismissed all that as LAMP-crap. All his followers blindly followed his thoughts as if they really wanted that to be true. Psychology of a herd, I’d say.

Oh well, too bad that public access to such valuable resource is now limited because of stubborn owners. Maybe it’s time for a real competitor to step up, with a simple slogan: “just like stackoverflow, except that it really works”.

For the game I’m making I have a bunch of array which represent a puzzle player needs to solve. Ensuring puzzle is solvable is CPU intensive, so I pre-calculated a couple of thousands of puzzles and select one randomly. Since puzzles are static data which is not going to change, I decided not to burden the database with this because DBMS is always overloaded with other stuff anyway.

My first thought was to build a big array and fetch a random element. I found some benchmark showing that this is faster than “if” or “switch”, however benchmarks excluded time needed to parse/create the array itself. Since every player is a new HTTP request, this huge array would have to be constructed each time. I am using APC, but I failed to find if arrays in precompiled PHP source file are stored “structured” as well.

Dropping the array idea, I though about “switch”, foolishly thinking that it would use some kind of jump-table and run the desired return statement. Something like this:

function get_puzzle($id)
{
    switch ($id)
    {
        case 0: return array (...first puzzle...);
        case 1: return array (...second puzzle...);
        case 2: return array (...etc.

However, researching this I find out that switch performs similar to series of “if” statements… variable is compared with each “case”.

So I decided to roll my own solution using “if” statements, but not linear. I used a B-tree approach. Split by two until only one is left. This means it would take only 11 comparisons to reach a puzzle from a set of 2048. Here’s an example with set of 256 puzzles.

function get_puzzle_btree($id)
{
  if ($id < 128)
    if ($id < 64)
      if ($id < 32)
        if ($id < 16)
          if ($id < 8)
            if ($id < 4)
              if ($id < 2)
                if ($id < 1)
                  return array (...first puzzle...);
                else
                  return array (...second puzzle...); 
...etc.

Of course, I did not write this “if” behemoth by hand. A simple 20-line recursive function spits out the PHP code.

In the end, I wrote a simple comparison loop that tries to get all the puzzles and compares whether the old “switch” and new “btree” function return the same values.

I had a client’s machine installed with Windows 7 and some free hard disk space for Linux. I decided not to install the Linux boot loader because:

  • I did not have Windows install/rescue CD at hand
  • in case something goes wrong I could not boot into Windows
  • I had some experience in the past with Windows XP where it simply did not work

Since re-installing Windows or even fixing Windows if it became unbootable was not an option, I decided to play safe: use Windows’ boot loader to boot up Linux.

I did this in past with Windows XP. Basically, you save Linux boot loader into some file (it’s only 512 bytes) and then tell Windows’ boot loader to load it. On WindowsXP this means editing boot.ini file in C:. To create the linux boot loader file, install linux boot loader into root partition (for example, with LILO, if you installed Linux in /dev/sda4, then lilo.conf should read boot=/dev/sda4) and then read the first sector into a file:

dd if=/dev/sda4 of=linux.boot bs=512 count=1

This will create file named linux.boot which you need to copy to C:\ disk of your Windows machine (use the USB stick or network for this).

On Windows7 there is no boot.ini, you have to use Microsoft’s tool, named BCDEdit. BCD stands for Boot Configuration Data. You need to run BCDedit as administrator. Hit the Start button, then go to All programs and then to Accessories. Right-click the Command prompt and “Run as administrator”.

Now, we need to enter a couple of commands:

bcdedit /create /d "Linux" /application BOOTSECTOR

If will show something like

The entry {12345678-0000-1111-9999-112233445566} was successfully created.

That number is a unique identifier for boot menu entry. You need to use it in subsequent commands:

bcdedit /set {12345678-0000-1111-9999-112233445566} device boot
bcdedit /set {12345678-0000-1111-9999-112233445566} device partition=c:
bcdedit /set {12345678-0000-1111-9999-112233445566} PATH \linux.boot
bcdedit /displayorder {12345678-0000-1111-9999-112233445566} /addlast

You might need to prepend C: in the second line if it does not work this way.

Reboot and enjoy.

Once upon a time, there was this kid that wanted to know about recursion. His father told him:

Son, once upon a time, there was this kid that wanted to know about recursion. His father told him:

Son, once upon a time, there was this kid that wanted to know about recursion. His father told him:

Son, once upon a time, there was this kid that wanted to know about recursion. His father told him:

Son, once upon a time, there was this kid that wanted to know about recursion. His father told him:

ok, now we’re five levels deep into recursion and also have 5 items (father+son stories) on the stack. Once a father decides to change the story to end it instead of recurse further, the stack will unwind and function at the top (this blog post), will end.

I’m creating a gaming website and one of the games has a complex CPU-intensive AI. Many possible positions for player and computer need to be examined before heuristics can do their work. For this I stack the game state, play a hypothetical move and run AI on it again. The number of moves it looks ahead is configurable, but the more there are the number of possible combinations grows exponentially. This will run on the web server, so it has to consume as little CPU as possible.

The rest of the website is written in PHP, but I started considering something faster for this. Of course, when you want to go fast, you want C or C++. I know some Assembly as well, but it’s PITA to use. But, before “wasting” any time in using C and having to set up the compiler to build the executable for target server, I wanted to make sure there is a reason to do it.

The benchmark is very simple. An array and a big loop doing trivial stuff with it:

------------------- bench.c -------------------

int main(int argc, char** argv)

{

int i,j, arr[10] = {0,0,0,0,0,0,0,0,0,0};

for (i = 0; i < 100; i++)

for (j = 0; j < 1000000; j++)

if (arr[i%5+j%5] == 0)

arr[i%2+j%2] = 1;

return 0;

}

-----------------------------------------------

I compiled it with GCC 4.2.3. Here’s the PHP version:

------------------- bench.php -----------------

$niz = array(0,0,0,0,0,0,0,0,0,0);

for ($i = 0; $i < 100; $i++)

for ($j = 0; $j < 1000000; $j++)

if ($niz[$i%5+$j%5] == 0)

$niz[$i%3+$j%3] = 1;

-

I run it with PHP 5.2.5 cli

Now the results:

$ time -p php -f bench.php

real 97.32

$ time -p ./bench

real 2.11

Amazing! C seems to be 46 times faster. I must admit I really expected better results from PHP. I wonder if there is some way to improve PHP speed on this one.

My I guess was that PHP’s excellent duct-tape arrays come with a price. To check this, I removed the array access, leaving only the for loops inside:

$ time -p ./bench

real 0.33

$ time -p php -f bench.php

real 15.14

Looks like array access is not to blame. It is consistently 46 times faster.

Tests were done on Intel Core2Duo CPU clocked at 2.16GHz running 32bit Slackware 12.1. All the results are averages of 10 runs.

Update: it looks like there are other benchmarks, and they come to similar conclusion. Facebook reports PHP being 39 times slower.

More often than not I find myself in situation to want to post something to a website forum I have been following for a long time. Latest example is today with HackerNews. I have been reading HN for about 6 months and today was the first time I was compelled to post something. I was able to comment the existing threads right away (which is nice), but not able to post a new link - most probably because my account was 10 minutes old.

By the time I earn privilege to post the story it wouldn’t be relevant to anyone anymore. Lesson learned the hard way. So, make sure you create an account for the website you are using daily, even if you don’t think you’ll ever need it.

Don’t you just hate how spammers have ruined the Internet.