Archive
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
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;
Recent
The vatican vault
- January 2022
- October 2021
- March 2021
- November 2020
- September 2020
- July 2020
- June 2020
- April 2020
- March 2020
- February 2020
- January 2020
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- December 2018
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- June 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- October 2017
- August 2017
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- December 2016
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- June 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- November 2014
- October 2014
- September 2014
- August 2014
- July 2014
- June 2014
- May 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- August 2013
- July 2013
- June 2013
- May 2013
- February 2013
- August 2012
- June 2012
- May 2012
- April 2012
You must be logged in to post a comment.