Results 1 to 10 of 25

Thread: CLASS vs. OBJECT and memory management

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #10
    Quote Originally Posted by phibermon View Post
    Yes - pre-allocate storage for data, pre-construct object instances and then just fetch them from the pools. You can grow as needed and construct double linked lists for quick insertion and deletion if required.
    That's just what the engines I know use (Build, Action Arcade Adventure Set). And this is the way I'll use in my engine.

    Like this:
    Code:
    TYPE
      TSpritePtr = ^TSPrite;
    
      TSprite = RECORD
        x, y: INTEGER;
        Bmp: ImageRef;
        NextSpr: TSpritePtr;
      END;
    
    VAR
       SpriteList: ARRAY OF TSprite; { Or a class containing the list, or a spezialized GENERIC or...}
       PlayerSpr: TSpritePtr;
       FirstEnemy: TSpritePtr;
       FirstBullet: TSpritePtr;
    Since the pointer itself is stored inside the list, there are very little CPU-cache collisions, so traverse the linked list should be quite fast.

    [edit]
    Fun fact: Actually this may be faster in the old times (i.e. 8086, 80286, 80386), when memory were divided in pages (do you remember the FAR and NEAR pointers?). If the whole list is in one single memory page, no page change is needed, and memory access may be faster specially if the related code is also in the same memory page. The AAAkit book talks about it too.

    Quote Originally Posted by turrican View Post
    Here is a little benchmark for Delphi I did.

    https://github.com/turric4n/Delphi-B...jectsVSRecords

    Records are more faster than Heap allocated Records (Pointer to Records) and much more faster than Objects.
    Thanks pal. It confirms the hypothesis "Objects/CLASSes are the slowest".

    [I must test it on FPC though, but I think it will be the same ]
    Last edited by Ñuño Martínez; 13-07-2017 at 08:09 AM.
    No signature provided yet.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •