Home > Delphi, JavaScript, nodeJS, Object Pascal, Smart Mobile Studio > Smart Pascal FastCode inspiration

Smart Pascal FastCode inspiration

Remember the Fastcode project? The alternative memory management and memory operation routines which almost doubled the speed of Delphi 5-7 applications? And they did it by replacing the bog standard code from Borland with hand-optimized assembly language alternatives.

The opus magnum of nerdvana

The opus magnum of Nerdvana

Well, today I wrote the Smart Pascal equivalent of the Fastcode project for Delphi. Or at least that’s what it reminded me of. So here is the Smart Pascal variations of the “Fastcode” like replacement modules 🙂

And yes, I did test these against 2-3 variations of the same procedures and these are as fast as I can make them without introducing lookup tables.

How fast are these routines? Well, the following code snippet was used for benchmarking:

for x:=1 to 1000000 do
begin
  mMemory:=AllocMem(1024);
  try
    WriteMem(mMemory,14,TDatatype.BytesToTypedArray
    ([10,20,30,40,50,60,70,80,90,100]));
    mMemory:=ReAllocMem(mMemory,512);
    Readmem(mMemory,14,10);
  finally
    Freemem(mMemory);
  end;
end;

The original code executes with time-code 551855 (which is approx 2.3 seconds) while this new variation executes with time-code 131816 which is a quite significant boost! But this is naturally because I have completely bypassed marshaled pointers which we are about to introduce in the next update of Smart Mobile Studio.

Pointers? Really?

As explained earlier, a marshaled pointer is ultimately an object which is managed, or takes care of something for you. In this case it assumes the role of a pointer, taking care of the reference to the memory segment (TMemoryHandle, as used above) and the offset into that segment.

The methods below does the exact same as those in the RTL, except they do not create a marshaled pointer at all. So this is for advanced Smart Mobile Studio developers only. If you hate low-level stuff and abhor getting your hands filthy from contact with JavaScript –then you probably dont want to mess with code like this in the first place.

But for the rest of us that like to squeeze every drop of performance out of both clients and server, this will give you something to play with. And it’s good practice for learning to optimize code which targets the most widely used virtual-machine in the world, namely the browser. A technology which eventually will take over the world and replace most of our native systems. We are a few years ahead of our time with SMS, but hey- world domination is hard work!

If you need more into on pointers under Smart Mobile Studio, I described that in detail here. You may also want to have a peek at this article and finally some inside info on the imminent update.

function  AllocMem(const Size:Integer):TMemoryHandle;
begin
  if Size>0 then
  Result:=new JUInt8ClampedArray(Size);
end;

procedure Freemem(var Memory:TMemoryHandle);
begin
  if (memory) then
  begin
    // decouple buffer from type
    // this does not release memory, but "hints" to the GC
    // to mark the segment for level 1 release classification
    JUInt8ClampedArray(Memory).buffer := NIL;
    Memory := null;
  end;
end;

function ReAllocMem(Memory:TMemoryHandle;
         Size:Integer):TMemoryHandle;
begin
  if (Memory) then
  begin
    if Size>0 then
    begin
      if Memory.length > Size then
      Memory:=JUInt8ClampedArray(memory).SubArray(0,Size-1);
      result:=new JUInt8ClampedArray(Size);
      JUInt8ClampedArray(result).Set(JUInt8ClampedArray(Memory),0);
    end;
  end else
  result:=AllocMem(Size);
end;

function WriteMem(const Memory:TMemoryHandle;
         const Offset:Integer;
         const Data:TMemoryHandle):Integer;
begin
  if (Memory) then
  begin
    if (Data) then
    begin
      if offset + data.length-1 > memory.length then
      result:=(offset + data.length-1) - memory.length else
      result:=data.length;

      if offset + Data.length > memory.length then
      JUInt8ClampedArray(Memory).Set(
      new JUInt8ClampedArray (JTypedArray(
      JUInt8ClampedArray(data).buffer.Slice(0,result-1))), offset) else
      JUInt8ClampedArray(Memory).Set(JTypedArray(data),offset);
    end;
  end;
end;

function ReadMem(const Memory:TMemoryHandle;
         const Offset:Integer;
         Size:Integer):TMemoryHandle;
begin
  if (Memory) then
  begin
    if Offset>=0 then
    begin
      if offset + Size-1 > Memory.length then
      dec(Size,(offset + Size-1)-Memory.length);
      result:=new JUInt8ClampedArray(JTypedArray(
      JUInt8ClampedArray(Memory).buffer.Slice(Offset,Offset + Size)));
    end;
  end;
end;
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: