In Delphi sane people use a class
to define objects.
In Turbo Pascal for Windows we used object
and today you can still use object
to create an object.
The difference is that a object
lives on the stack and a class
lives on the heap.
And of course the object
is depreciated.
Putting all that aside:
is there a benefit to be had, speed wise by using object
instead of class?
I know that object
is broken in Delphi 2009, but I've got a special use case1) where speed matters and I'm trying to find if using object
will make my thing faster without making it buggy
This code base is in Delphi 7, but I may port it to Delphi 2007, haven't decided yet.
1) Conway's game of life
Long comment
Thanks all for pointing me in the right direction.
Let me explain a bit more. I'm trying to do a faster implementation of hashlife, see also here or here for simple sourcecode
The current record holder is golly, but golly uses a straight translation of Bill Gospher original lisp code (which is brilliant as an algorithm, but not optimized at the micro level at all). Hashlife enables you to calculate a generation in O(log(n)) time.
It does this by using a space/time trade off. And for this reason hashlife needs a lot of memory, gigabytes are not unheard of. In return you can calculate generation 2^128 (340282366920938463463374607431770000000) using generation 2^127 (170141183460469231731687303715880000000) in o(1) time.
Because hashlife needs to compute hashes for all sub-patterns that occur in a larger pattern, allocation of objects needs to be fast.
Here's the solution I've settled upon:
Allocation optimization
I allocate one big block of physical memory (user settable) lets say 512MB. Inside this blob I allocate what I call cheese stacks. This is a normal stack where I push and pop, but a pop can also be from the middle of the stack. If that happens I mark it on the free
list (this is a normal stack). When pushing I check the free
list first if nothing is free I push as normal. I'll be using records as advised it looks like the solution with the least amount of overhead.
Because of the way hashlife works, very little pop
ping takes place and a lot of push
es. I keep separate stacks for structures of different sizes, making sure to keep memory access aligned on 4/8/16 byte boundaries.
Other optimizations
- recursion removal
- cache optimization
- use of
inline
- precalculation of hashes (akin to rainbow tables)
- detection of pathological cases and use of fall-back algorithm
- use of GPU