Recent

Author Topic: FGL lib DeleteRange method is too lazy to free mem  (Read 297 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 2654
    • UVviewsoft
FGL lib DeleteRange method is too lazy to free mem
« on: November 19, 2025, 08:16:02 am »
My use case: CudaText's plugin Spell Checker adds lots of marks (200k for example if DE lang is chosen and English file is active). Now user calls "Clear marks" plugin cmd which internally calls DeleteRange with the range 0..Count-1 (it calculates the range basing of current marks, it is not always the whole range!!).
DeleteRange DONT REALLOC MEM when some condition is not met.
Code: Pascal  [Select][+][-]
  1. procedure TFPSList.DeleteRange(IndexFrom, IndexTo : Integer);
  2. ...
  3.   System.Move(InternalItems[IndexTo+1]^, InternalItems[IndexFrom]^, (OldCnt - IndexTo-1) * FItemSize);
  4.   // Shrink the list if appropriate
  5.   if (FCapacity > 256) and (FCount < FCapacity shr 2) then
  6.   begin
  7.     FCapacity := FCapacity shr 1;
  8.     ReallocMem(FList, (FCapacity+1) * FItemSize);
  9.   end;
  10. ...
  11. end;

I suggest: in DeleteRange, check, if range is from 0 to Count-1. if so, call
Code: Pascal  [Select][+][-]
  1. begin
  2.   Clear;
  3.   Exit;
  4. end;

This will realloc the mem. Command 'Clear marks' frees mem.
« Last Edit: November 19, 2025, 08:23:58 am by AlexTP »

AlexTP

  • Hero Member
  • *****
  • Posts: 2654
    • UVviewsoft
Re: FGL lib DeleteRange method issue
« Reply #1 on: November 19, 2025, 08:22:42 am »
Additional condition can be the check for 80% .. 90% of items count (IndexTo-IndexFrom >= Count * 9 div 10).
In this case FGL should make Capacity the minimum, for example, not only `FCapacity := FCapacity shr 1`.
« Last Edit: November 19, 2025, 08:41:07 am by AlexTP »

jamie

  • Hero Member
  • *****
  • Posts: 7407
Re: FGL lib DeleteRange method is too lazy to free mem
« Reply #2 on: November 19, 2025, 11:35:07 am »
Nothing stops you from using the "Pack" to compress the memory, but its like that for speed.
The only true wisdom is knowing you know nothing

AlexTP

  • Hero Member
  • *****
  • Posts: 2654
    • UVviewsoft
Re: FGL lib DeleteRange method is too lazy to free mem
« Reply #3 on: November 19, 2025, 12:05:46 pm »
From this AI answer I don't think that Pack() reallocs the list to the minimum. Also my attrib-list in CudaText don't have Nil elements at all.

Quote
In Free Pascal with the FGL (Free Pascal Generic Library) unit, the TFPSList.Pack method is used to remove empty (nil) slots from the list, effectively compacting it.
What does TFPSList.Pack do?

Removes nil entries: If your list contains nil elements (for object lists) or empty slots, Pack will remove them.
Compacts the list: After removing nil entries, the list is reindexed so that all remaining elements are contiguous, starting from index 0.
« Last Edit: November 19, 2025, 12:09:30 pm by AlexTP »

 

TinyPortal © 2005-2018