Question on dividers in Delphi

I have a large number of objects that have a file name stored internally. All file names are within the specified base directory (call it C:\BaseDir\ ). I am now considering two alternatives:

  • Keep absolute paths in objects
  • Keep relative paths in the object and optionally save the base path

If I understand Delphi strings correctly, the second approach will require much less memory, because the main path string is shared - given that I pass the same string field to all objects like this:

 TDataObject.Create (FBasePath, RelFileName); 

Is this assumption true? Will there be only one instance of the base path string in memory?

If someone knows a better way to deal with such situations, feel free to comment on this.

Thanks!

+4
source share
2 answers

You're right. When you write s1: = s2 with two string variables, there is one line in memory with (at least two) references to it.

You also ask whether it is worth trying to reduce the number of lines in memory. It depends on how many lines you have compared to other memory consuming items. Only you can truly answer that.

+6
source

As David said, the shared string will be split (unless you use iea UniqueString () ).

Having said that, it looks like premature optimization. If you really need to work with full paths and you never need to separate dir and filename separately, then you should think about separating them only when you really run into memory problems. The constant concatenation of parts of the database and file name can significantly slow down your program and cause memory fragmentation.

+2
source

Source: https://habr.com/ru/post/1369115/


All Articles