Persistence

11 October 2009, 11:19 — Software Development
/**
 *  NF_Persistence
 *
 *  Agent Smith: "Why, Mr. Anderson, why? Why do you persist?"
 *  Neo:         "Because I choose to."
 *
 *  PHP Version 5
 *
 *  @category   NiftyFramework
 *  @package    NiftyFramework
 *  @subpackage Database
 *  @author     Mats Gefvert

 *  @license    http://www.sun.com/cddl/ Common Development and Distribution License
 */

class NF_PersistenceRelationMap
{
    ....

I make myself laugh sometimes. Is that a good sign? :)

Microsoft Like Moria, Not Mordor

2 June 2009, 13:00 — Reflections, Software Development

balrog

Contrary to public opinion, Microsoft is not Mordor.

Microsoft is like Moria.

Being a developer and faced with the task of developing some particularly interesting feature for, say, the Internet Information Server (IIS), is much like Gandalf and the companions looking at crossing the mountains through the treacherous kingdom of Moria. Once a kingdom of dwarves, treasures and royal hallways, the rumors that now spread from these caverns are dark and brooding. And entering the domains of Moria will always be an adventure, whichever way you look upon it.

You may get through, of course, if you’re quiet, don’t do anything surprising, and stick to the API’s. Keep it simple, and don’t touch anything you shouldn’t touch. It will be a long, dark passage, but you may eventually get through to the other side.

On the other hand, you may get lost, stray into tunnels that you shouldn’t have gone into; and disturb the deep darkness below. Too many have been lost in these caverns and you are now about to be the next victim. You pass through chambers with ominous writing inside (“here lies Balin, king of Moria”) and stumbling, falling, irretrievably lost proceeding further and further into the darkness…

You can abort. Cut your losses, and get out. You never should have come here. We’ll make for the gap of Rohan instead! Quickly, now!

But some part of you pushes on… There must be an exit ahead… Who knows, behind the next corner you may feel a gust of fresh wind, and a dim light far ahead, the opening to the outside and you’ll be through. But instead, you cross a barrier and suddenly you’re face to face with the Balrog, the most terrifying of all enemies. You’re at the brink of insanity and plunging forward. There is no escape.


Trying to script the IIS is an excellent example. The API is dark and mysterious, and faint murmurs of a dark evil reach you as you stand by the door. And a short way into the inside, you reach a decision point: Should you use the XML metabase? WMI? ADSI? All of these have possibilities, and yet, dangerous drawbacks.

You spent some time trying to figure out what wmic is and how it works. Turning out to be an utterly incomprehensible object, you shy away from it. Next up, you work with CIM Explorer to explore the WMI and MicrosoftIISv2 namespace. It is equally incomprehensible, and every attempt you make to understand it and get ahold of its promises are futile. Like a slithery Gollum, it writhes out of your grip and disappears into the caves. “My preciousssss!” you hear a scream in the distance.

Next up: ADsUtil. A wonderful little VBScript gratitiously provided by Microsoft. By it quickly breaks in your hands – it returns strange and complicated errors when applying it to the IPSecurity object. But, even though ADsUtil crumbles to dust (for magical reasons, you assume), the ADSI approach seems worthwhile. The air in this tunnel seems fresher. You stumble on.

A while later, while pouring over .NET, DirectoryEntry, and hitting your head repeatedly on sharp, nasty stalagmites stalactites hanging from the ceiling, you force yourself into twisting little dark tunnels, which now consist of strange, magical invocations of objects, GetTypes, unexplored COM objects, and you have the vicious feeling that nobody has been here before you in a very long time.

And right there, just when you hit entry.CommitChanges() and think that you’re home safe, an exception is suddenly thrown out of the blue. A wall collapses in front of you. And there it is… the Balrog, full of flame and fire and smoke and radiating with a pure evil from aeons past.

I should have made for the Gap of Rohan. Now, it’s over.

*sigh*

Speeding Up Delphi’s TStringList.IndexOf

1 June 2009, 15:33 — Music, Software Development

Delphi’s TStringList is one of the objects I love the most. If it’s sorted (StringList.Sorted := true) then you can use it to parse huge chunks of data quickly.

For instance, looping through an enormous amount of IP addresses and keeping count of how many times they appeared, is easily done using the following code (not compiled or checked for syntax errors):

ls := TStringList.Create;
ls.Sorted := true;
for ip in ipAddresses do begin
  n := ls.IndexOf(ip);
  if n = -1 then
    ls.AddObject(ip, TObject(1))
  else
    ls.Objects[n] := TObject(Integer(ls.Objects[n]) + 1);
end;

It’s very efficient. Since TStringList.IndexOf always does a binary search, it operates in log2(n) time, and using Objects as integers allows us to keep track of count without messing with the string data.

But there are things we can do to speed it up. For instance, TStringList.IndexOf relies on TStringList.Find, which itself uses AnsiCompareStr, which is a slow Windows call, taking locale and its mother into consideration. Overriding this with our own method should be worthwhile. (The code below is adapted straight from the Classes unit.)

type
  TStringListEx = class(TStringList)
  public
    function Find(const S: string; var Index: Integer): Boolean; override;
  end;

function TStringListEx.Find(const S: string; var Index: Integer): Boolean;
var
  L, H, I, C: Integer;
begin
  Result := False;
  L := 0;
  H := Count - 1;
  while L <= H do
  begin
    I := (L + H) shr 1;
    C := CompareStr(Get(I), S);
    if C < 0 then L := I + 1 else
    begin
      H := I - 1;
      if C = 0 then
      begin
        Result := True;
        if Duplicates <> dupAccept then L := I;
      end;
    end;
  end;
  Index := L;
end;

We’ve replaced AnsiCompareStr with Delphi’s own CompareStr, which is a highly optimized FastCode routine. There are some drawbacks – things will always be sorted in byte order and no case-sensitivity is done. But we don’t care about this – it can always be done afterwards; right now, speed is the main importance.

And it turns out that using the above code, in pure examples, can slash execution time with up to about 80%. Dramatic savings, indeed. In my own example, where I analyze ftp log data, I managed to cut execution time on 122 MB of data down from 7 seconds down to 3.1 seconds.

Best of all, since TStringList.Find is declared virtual, we don’t need to change any types anywhere, just do a TStringListEx.Create instead of a TStringList.Create and you’re good to go.

Computer Speed and Memory Access

4 May 2009, 10:15 — Software Development

I’ve found a new great blog, Gustavo Duarte’s blog, where he talks about hardware, operating system kernerls, and other fun items.

I liked his comparison of how fast different types of memory is.

The first thing that jumps out is how absurdly fast our processors are. Most simple instructions on the Core 2 take one clock cycle to execute, hence a third of a nanosecond at 3.0Ghz. For reference, light only travels ~4 inches (10 cm) in the time taken by a clock cycle…

Normally when the CPU needs to touch the contents of a memory region they must either be in the L1/L2 caches already or be brought in from the main system memory … To put this into perspective, reading from L1 cache is like grabbing a piece of paper from your desk (3 seconds), L2 cache is picking up a book from a nearby shelf (14 seconds), and main system memory is taking a 4-minute walk down the hall to buy a Twix bar…

[...] Even main memory is blazing fast compared to hard drives. Keeping with the office analogy, waiting for a hard drive seek is like leaving the building to roam the earth for one year and three months. This is why so many workloads are dominated by disk I/O and why database performance can drive off a cliff once the in-memory buffers are exhausted …

Subscribed. :)

Phar: Not Yet Ready

10 April 2009, 11:56 — Software Development

I’ve been trying to get Phar to work on my PHP-5.2.9-Windows box. It’s been frustrating.

The problem is that the only binary version you can get – now that pecl4win is down – of php_phar is a 2.0-alpha version, which unfortunately seems to have a serious bug on the Windows platform. As soon as you try to access any phar file with a path delimiter in it, Phar breaks with the message

Cannot create phar ‘C:/Home/projects/Web.Unauth/nifty-phar-test-1/phar-public/app.phar’, file extension (or combination) not recognised’

This also goes for whenever you try to do something with the actual Phar file, like Phar::compressFiles or new RecursiveIteratorIterator($phar).

I guess we’ll just have to wait.

Updates

29 March 2009, 21:12 — Software Development

Life goes on.

I’ve been working on Nifty this weekend. Seemed like a good idea, because I’m updating a lot of stuff on the intranet at work, building on the 1.2 branch, and I need a couple of new features. So I made a 1.3 branch.

Some really nifty features so far:

  • Pages aren’t limited to one level anymore. You can have a page at /index, one at /start/rss, and one at /intranet/projects/newsfeeds/rss. The system translates the call into an index.php?__path=nnn statement, and resolves the class names and commands automatically. Which means you should be able to build much more complex applications.
  • Modules are no more. They’ve been replaced with components. You can now build entire forms and web-page components completely standalone, and insert them at will into your templates/pages. You can even make callbacks through HTML POSTs or Ajax directly into a component, dynamically updating and reloading parts of the web page.
  • The exception system has been overhauled, giving more meaningful error codes.
  • Responses are now automatically prepared for Ajax calls, decoding and encoding responses as necessary (no more $Response->reset()).
  • The database/persistence layers can now handle relations between tables. You can now issue a $users = $Persistence->loadAll(‘Data_Users’); and immediately thereafter a $Persistence->loadRelated(‘Data_CompanyDepartments’, $users) and link users with corresponding department objects. The initial objects are dynamically updated with references to the new objects.
  • Page session objects and page ViewState is being incorporated directly into PageBase, no longer requiring the use of Forms.
  • Templates can now load through loadDefault() and runDefault(), automatically determining the template file names from the current call context.
  • The Autoloader can make more checks for loadable classes and has a more fine-grained control.

It’s currently in development (possibly soon alpha), but that’s the news so far.

Lament for Delphi

15 February 2009, 18:23 — Poetry, Software Development

It was with a pang of sorrow
That I realized today
We may have to say goodbye

We were brothers in arms for so long
You helped me do things
I could never have done on my own.
We built applications, you and I
Hacked together utilities and tools
And the sun was shining.

You never once complained.
Never once faltered.
You had your oddities and quirks
Unappreciated and ridiculed
But you were mine!
And underneath that bland exterior of yours
Was raw power…
The skies were limitless.

I held on to you for as long as I could.
But now you’re slipping through my fingers
And a new group of people will benefit now.

Farewell, my brother,
Remember how I loved you.
Remember the victories we won.

I will never forget.

Code folding in Netbeans PHP

6 February 2009, 10:26 — Software Development

I recently found out a tip that C#-style #region and #endregion-style declarations work in NetBeans for PHP too, although with a slightly different syntax.

The trick was making it work in HTML, since I use it mostly for defining different sections of my .phtml templates, which can get pretty long:

<? // <editor-fold defaultstate="collapsed" desc="JavaScript code"> ?>
...
<? // </editor-fold> ?>

Realized you had to escape to PHP for it to work. A little extra syntax, but look at my beautiful, beautiful HTML code:

I’m Publishing My Little Programs Now

3 January 2009, 21:25 — Software Development

I’ve published one or two of my little programs before, but now I’m expanding with a set of new ones.

  • DesktopShooter (shoot up your desktop)
  • Mobile Clock (display a neat clock/countdown on the desktop)
  • Mobile Notepad (the old “notepad” under a new and better name)
  • Mobile Wallpaper (replacement for Webshots)

These are all available under the Software Downloads page. Knock yourself out.

(Oh, and, uh, don’t expect an installation program.)

Nifty Framework 1.2 is Available for Download

3 December 2008, 11:17 — Software Development

At long last, the Nifty Framework 1.2 is available for public download.

No guarantees implied. It’s a bleeding edge copy of production code, though.

But it’s darn good, if I may say so myself. :)

Older Posts »