View Issue Details

IDProjectCategoryView StatusLast Update
0036143FPCRTLpublic2019-10-09 09:14
ReporterSerge AnvarovAssigned ToMichael Van Canneyt 
PrioritynormalSeverityminorReproducibilityhave not tried
Status resolvedResolutionfixed 
Product Version3.3.1Product Build 
Target VersionFixed in Version3.3.1 
Summary0036143: Patch. Micro-optimization of TList
DescriptionAll methods that call TFPList without additional code are marked as inline.
TListEnumerator has the same signature, as TFPListEnumerator, so the base class is used instead of duplicating.
Method TFPList.Error is not virtual and does not use Self, so it is marked as static.
TagsNo tags attached.
Fixed in Revision43155
FPCOldBugId
FPCTarget3.2.0
Attached Files
  • TList.diff (4,680 bytes)
    Index: rtl/objpas/classes/classesh.inc
    ===================================================================
    --- rtl/objpas/classes/classesh.inc	(revision 43143)
    +++ rtl/objpas/classes/classesh.inc	(working copy)
    @@ -242,7 +242,7 @@
         function Add(Item: Pointer): Integer; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
         procedure Clear;
         procedure Delete(Index: Integer); {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
    -    class procedure Error(const Msg: string; Data: PtrInt);
    +    class procedure Error(const Msg: string; Data: PtrInt); static;
         procedure Exchange(Index1, Index2: Integer);
         function Expand: TFPList; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
         function Extract(Item: Pointer): Pointer;
    @@ -288,15 +288,9 @@
       TListNotification = (lnAdded, lnExtracted, lnDeleted);
       TList = class;
     
    -  TListEnumerator = class
    -  private
    -    FList: TList;
    -    FPosition: Integer;
    +  TListEnumerator = class(TFPListEnumerator)
       public
         constructor Create(AList: TList);
    -    function GetCurrent: Pointer;
    -    function MoveNext: Boolean;
    -    property Current: Pointer read GetCurrent;
       end;
     
       TList = class(TObject,IFPObserved)
    @@ -312,15 +306,15 @@
         procedure DoOr(ListA, ListB : TList);
         procedure DoXOr(ListA, ListB : TList);
       protected
    -    function Get(Index: Integer): Pointer;
    +    function Get(Index: Integer): Pointer; inline;
         procedure Grow; virtual;
         procedure Put(Index: Integer; Item: Pointer);
         procedure Notify(Ptr: Pointer; Action: TListNotification); virtual;
    -    procedure SetCapacity(NewCapacity: Integer);
    -    function GetCapacity: integer;
    +    procedure SetCapacity(NewCapacity: Integer); inline;
    +    function GetCapacity: Integer; inline;
         procedure SetCount(NewCount: Integer);
    -    function GetCount: integer;
    -    function GetList: PPointerList;
    +    function GetCount: Integer; inline;
    +    function GetList: PPointerList; inline;
       public
         constructor Create;
         destructor Destroy; override;
    @@ -335,19 +329,19 @@
         procedure Exchange(Index1, Index2: Integer);
         function Expand: TList;
         function Extract(item: Pointer): Pointer;
    -    function First: Pointer;
    +    function First: Pointer; inline;
         function GetEnumerator: TListEnumerator;
    -    function IndexOf(Item: Pointer): Integer;
    +    function IndexOf(Item: Pointer): Integer; inline;
         procedure Insert(Index: Integer; Item: Pointer);
    -    function Last: Pointer;
    -    procedure Move(CurIndex, NewIndex: Integer);
    +    function Last: Pointer; inline;
    +    procedure Move(CurIndex, NewIndex: Integer); inline;
         procedure Assign (ListA: TList; AOperator: TListAssignOp=laCopy; ListB: TList=nil);
         function Remove(Item: Pointer): Integer;
    -    procedure Pack;
    -    procedure Sort(Compare: TListSortCompare);
    -    procedure Sort(Compare: TListSortCompare; SortingAlgorithm: PSortingAlgorithm);
    -    procedure Sort(Compare: TListSortComparer_Context; Context: Pointer);
    -    procedure Sort(Compare: TListSortComparer_Context; Context: Pointer; SortingAlgorithm: PSortingAlgorithm);
    +    procedure Pack; inline;
    +    procedure Sort(Compare: TListSortCompare); inline;
    +    procedure Sort(Compare: TListSortCompare; SortingAlgorithm: PSortingAlgorithm); inline;
    +    procedure Sort(Compare: TListSortComparer_Context; Context: Pointer); inline;
    +    procedure Sort(Compare: TListSortComparer_Context; Context: Pointer; SortingAlgorithm: PSortingAlgorithm); inline;
         property Capacity: Integer read GetCapacity write SetCapacity;
         property Count: Integer read GetCount write SetCount;
         property Items[Index: Integer]: Pointer read Get write Put; default;
    Index: rtl/objpas/classes/lists.inc
    ===================================================================
    --- rtl/objpas/classes/lists.inc	(revision 43143)
    +++ rtl/objpas/classes/lists.inc	(working copy)
    @@ -140,7 +140,7 @@
       begin
         SetCount(0);
         SetCapacity(0);
    -    FList := nil;
    +    // FList := nil; // Already set by SetCapacity
       end;
     end;
     
    @@ -535,22 +535,10 @@
     
     constructor TListEnumerator.Create(AList: TList);
     begin
    -  inherited Create;
    -  FList := AList;
    -  FPosition := -1;
    +  inherited Create(AList.FList);
     end;
     
    -function TListEnumerator.GetCurrent: Pointer;
    -begin
    -  Result := FList[FPosition];
    -end;
     
    -function TListEnumerator.MoveNext: Boolean;
    -begin
    -  Inc(FPosition);
    -  Result := FPosition < FList.Count;
    -end;
    -
     {****************************************************************************}
     {*                TList                                                     *}
     {****************************************************************************}
    
    TList.diff (4,680 bytes)

Activities

Serge Anvarov

2019-10-07 19:05

reporter  

TList.diff (4,680 bytes)
Index: rtl/objpas/classes/classesh.inc
===================================================================
--- rtl/objpas/classes/classesh.inc	(revision 43143)
+++ rtl/objpas/classes/classesh.inc	(working copy)
@@ -242,7 +242,7 @@
     function Add(Item: Pointer): Integer; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
     procedure Clear;
     procedure Delete(Index: Integer); {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
-    class procedure Error(const Msg: string; Data: PtrInt);
+    class procedure Error(const Msg: string; Data: PtrInt); static;
     procedure Exchange(Index1, Index2: Integer);
     function Expand: TFPList; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
     function Extract(Item: Pointer): Pointer;
@@ -288,15 +288,9 @@
   TListNotification = (lnAdded, lnExtracted, lnDeleted);
   TList = class;
 
-  TListEnumerator = class
-  private
-    FList: TList;
-    FPosition: Integer;
+  TListEnumerator = class(TFPListEnumerator)
   public
     constructor Create(AList: TList);
-    function GetCurrent: Pointer;
-    function MoveNext: Boolean;
-    property Current: Pointer read GetCurrent;
   end;
 
   TList = class(TObject,IFPObserved)
@@ -312,15 +306,15 @@
     procedure DoOr(ListA, ListB : TList);
     procedure DoXOr(ListA, ListB : TList);
   protected
-    function Get(Index: Integer): Pointer;
+    function Get(Index: Integer): Pointer; inline;
     procedure Grow; virtual;
     procedure Put(Index: Integer; Item: Pointer);
     procedure Notify(Ptr: Pointer; Action: TListNotification); virtual;
-    procedure SetCapacity(NewCapacity: Integer);
-    function GetCapacity: integer;
+    procedure SetCapacity(NewCapacity: Integer); inline;
+    function GetCapacity: Integer; inline;
     procedure SetCount(NewCount: Integer);
-    function GetCount: integer;
-    function GetList: PPointerList;
+    function GetCount: Integer; inline;
+    function GetList: PPointerList; inline;
   public
     constructor Create;
     destructor Destroy; override;
@@ -335,19 +329,19 @@
     procedure Exchange(Index1, Index2: Integer);
     function Expand: TList;
     function Extract(item: Pointer): Pointer;
-    function First: Pointer;
+    function First: Pointer; inline;
     function GetEnumerator: TListEnumerator;
-    function IndexOf(Item: Pointer): Integer;
+    function IndexOf(Item: Pointer): Integer; inline;
     procedure Insert(Index: Integer; Item: Pointer);
-    function Last: Pointer;
-    procedure Move(CurIndex, NewIndex: Integer);
+    function Last: Pointer; inline;
+    procedure Move(CurIndex, NewIndex: Integer); inline;
     procedure Assign (ListA: TList; AOperator: TListAssignOp=laCopy; ListB: TList=nil);
     function Remove(Item: Pointer): Integer;
-    procedure Pack;
-    procedure Sort(Compare: TListSortCompare);
-    procedure Sort(Compare: TListSortCompare; SortingAlgorithm: PSortingAlgorithm);
-    procedure Sort(Compare: TListSortComparer_Context; Context: Pointer);
-    procedure Sort(Compare: TListSortComparer_Context; Context: Pointer; SortingAlgorithm: PSortingAlgorithm);
+    procedure Pack; inline;
+    procedure Sort(Compare: TListSortCompare); inline;
+    procedure Sort(Compare: TListSortCompare; SortingAlgorithm: PSortingAlgorithm); inline;
+    procedure Sort(Compare: TListSortComparer_Context; Context: Pointer); inline;
+    procedure Sort(Compare: TListSortComparer_Context; Context: Pointer; SortingAlgorithm: PSortingAlgorithm); inline;
     property Capacity: Integer read GetCapacity write SetCapacity;
     property Count: Integer read GetCount write SetCount;
     property Items[Index: Integer]: Pointer read Get write Put; default;
Index: rtl/objpas/classes/lists.inc
===================================================================
--- rtl/objpas/classes/lists.inc	(revision 43143)
+++ rtl/objpas/classes/lists.inc	(working copy)
@@ -140,7 +140,7 @@
   begin
     SetCount(0);
     SetCapacity(0);
-    FList := nil;
+    // FList := nil; // Already set by SetCapacity
   end;
 end;
 
@@ -535,22 +535,10 @@
 
 constructor TListEnumerator.Create(AList: TList);
 begin
-  inherited Create;
-  FList := AList;
-  FPosition := -1;
+  inherited Create(AList.FList);
 end;
 
-function TListEnumerator.GetCurrent: Pointer;
-begin
-  Result := FList[FPosition];
-end;
 
-function TListEnumerator.MoveNext: Boolean;
-begin
-  Inc(FPosition);
-  Result := FPosition < FList.Count;
-end;
-
 {****************************************************************************}
 {*                TList                                                     *}
 {****************************************************************************}
TList.diff (4,680 bytes)

Michael Van Canneyt

2019-10-09 09:14

administrator   ~0118440

Checked and applied, thank you very much for the patch!

Issue History

Date Modified Username Field Change
2019-10-07 19:05 Serge Anvarov New Issue
2019-10-07 19:05 Serge Anvarov File Added: TList.diff
2019-10-08 11:01 Michael Van Canneyt Assigned To => Michael Van Canneyt
2019-10-08 11:01 Michael Van Canneyt Status new => assigned
2019-10-09 09:14 Michael Van Canneyt Status assigned => resolved
2019-10-09 09:14 Michael Van Canneyt Resolution open => fixed
2019-10-09 09:14 Michael Van Canneyt Fixed in Version => 3.3.1
2019-10-09 09:14 Michael Van Canneyt Fixed in Revision => 43155
2019-10-09 09:14 Michael Van Canneyt FPCTarget => 3.2.0
2019-10-09 09:14 Michael Van Canneyt Note Added: 0118440