Recent

Author Topic: Is it possible to suggest a different implementation in the RTL?  (Read 5713 times)

jmm72

  • Jr. Member
  • **
  • Posts: 79
  • Very experienced in being a beginner...
Greetings.

I'm still polishing my Object Pascal, and I think I'm only scratching the surface of all I can do with FPC+Lazarus. But I looked at some of the source of the RTL to see how I could squeeze a bit of performance for certain tasks by knowing how some classes and functions work internally. And I found that TStringList doesn't implement an IndexOfName to take advantage of the list if it's sorted, just like TStringList.IndexOf does.

I don't know if this is intentional or if the devs have overlooked it. Maybe there's a rule to keep the implementations just like Delphi ones, and if that's so, then TStringList wouldn't override TStrings.IndexOfName (or could it be overriden as long as it doesn't break compatibility?). Or some other reason, I don't think I'm that smart since imho it's a pretty obvious thing that can be greatly enhanced.

To overcome two limitations of the TStringList, I made a descendant with my own implementations and extensions. But I wonder if this specific enhancement could be implemented into the RTL so we could benefit from a certainly better way to deal with the simple but sometimes useful feature of the name=value pairs in TStringList.

Now that I think of it, I'm unsure if implementing a method that overrides one of TStrings would break Delphi compatibility or what. Is it possible to write code that would fail if there's an overriding method which shouldn't be there? In any case, before reporting at the bugtracker to suggest this RTL enhancement, I think I'd better ask here to get some enlightment on the issue so I don't slip too much  %)

Excuse my inexperience with what you actually want for FPC development, but I think that if you ask a silly question, you might look silly for 5 minutes; but if you don't ask it, you could be silly for a lifetime. I prefer to look silly and learn.

Regards, JMM
Lazarus 1.6.4 + FPC 3.0.2 64bits under Windows 7 64bits
Only as a hobby nowadays
Current proyect release: TBA

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Is it possible to suggest a different implementation in the RTL?
« Reply #1 on: January 07, 2015, 04:44:10 am »
Greetings.

I'm still polishing my Object Pascal, and I think I'm only scratching the surface of all I can do with FPC+Lazarus. But I looked at some of the source of the RTL to see how I could squeeze a bit of performance for certain tasks by knowing how some classes and functions work internally. And I found that TStringList doesn't implement an IndexOfName to take advantage of the list if it's sorted, just like TStringList.IndexOf does.

I don't know if this is intentional or if the devs have overlooked it. Maybe there's a rule to keep the implementations just like Delphi ones, and if that's so, then TStringList wouldn't override TStrings.IndexOfName (or could it be overriden as long as it doesn't break compatibility?). Or some other reason, I don't think I'm that smart since imho it's a pretty obvious thing that can be greatly enhanced.

To overcome two limitations of the TStringList, I made a descendant with my own implementations and extensions. But I wonder if this specific enhancement could be implemented into the RTL so we could benefit from a certainly better way to deal with the simple but sometimes useful feature of the name=value pairs in TStringList.

Now that I think of it, I'm unsure if implementing a method that overrides one of TStrings would break Delphi compatibility or what. Is it possible to write code that would fail if there's an overriding method which shouldn't be there? In any case, before reporting at the bugtracker to suggest this RTL enhancement, I think I'd better ask here to get some enlightment on the issue so I don't slip too much  %)

Excuse my inexperience with what you actually want for FPC development, but I think that if you ask a silly question, you might look silly for 5 minutes; but if you don't ask it, you could be silly for a lifetime. I prefer to look silly and learn.

Regards, JMM
If what you want is to enhance (NOT extend) existing code: Create a patch, attach it in a report to the bugtracker, let the devs decide whether the implementation is acceptable or not.

jmm72

  • Jr. Member
  • **
  • Posts: 79
  • Very experienced in being a beginner...
Re: Is it possible to suggest a different implementation in the RTL?
« Reply #2 on: January 07, 2015, 01:06:08 pm »
That's one of the doubts I have. TStringList doesn't override TStrings.IndexOfName, but my implementation would do, and I don't know if under the view of the devs, that is extending the code or not.
Lazarus 1.6.4 + FPC 3.0.2 64bits under Windows 7 64bits
Only as a hobby nowadays
Current proyect release: TBA

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11455
  • FPC developer.
Re: Is it possible to suggest a different implementation in the RTL?
« Reply #3 on: January 07, 2015, 01:09:36 pm »
That's one of the doubts I have. TStringList doesn't override TStrings.IndexOfName, but my implementation would do, and I don't know if under the view of the devs, that is extending the code or not.

It should not change the behaviour of code that overrides tstringlist.indexofname and calls inherited, and delphi compatibility should not be changed in any way. (so basically if this changes the state of the tstringlist, it is already toast)

If it is truly a speed optimization only, it might do. But testing it will still be hard (think of duplicates etc )

jmm72

  • Jr. Member
  • **
  • Posts: 79
  • Very experienced in being a beginner...
Re: Is it possible to suggest a different implementation in the RTL?
« Reply #4 on: January 07, 2015, 03:12:50 pm »
That's what I fear, that having TStringList override and not inherite this method might change the behaviour of TStringList descendants which might rely on knowing that the actual method called will be TStrings.IndexOfName. So I still wonder, would implementing a method which wasn't previously overriden break compatibility with existing code?

Regarding testing, I heavily tested my modification with random name=key values, where there are duplicates, some strings are just 'name=' without the value or even 'name' only, and checked with different NameValueSeparators that sort before and after the generated random names. I generate the same random names and values for an original TStringList and my TStringListMod. Then I generate a batch of random names for searching, but about half of them are out of the original range so there are lots of searches for names which will not be found. Finally I run that batch of searches for each stringlist and store the values found in independent places, and after both searches have finished, I check that the values found are consistent between both stringlists. Pretty much I think I've covered every corner, including "what-if-the-searched-name-would-go-last", where Find returns false *and* an index out of the current stringlist.

Another thing is whether a certain implementation is just a bug, or it's a design decision to keep exactly the same Delphi behaviour. Imagine this scenario: a TStrings object, sorted, Duplicates set to dupIgnore, and with OwnsObjects = true. If you add a string with an object attached with AddObject(s, aobject), and the string already exists in the list (with an object attached), the object is simply overwritten without freeing it first. Supposedly one uses OwnsObjects to not care about freeing objects before destroying the stringlist. Again, this happens because TStringList doesn't override AddObject to take into account that objects shouldn't be overwritten that easily in the case of OwnsObjects = true.

Code: [Select]
Function TStrings.AddObject(const S: string; AObject: TObject): Integer;

begin
  Result:=Add(S);
  Objects[result]:=AObject;
end;

Of course TStrings knows nothing of OwnsObjects, but TStringList should have overriden that method so to check if OwnsObjects and there's an object there to free because it's going to be overwritten.

Maybe the path to go is to suggest these things to Embarcadero first, and if they ever implement it, FPC would follow them to match implementations. But maybe that's a path that FPC devs would think it's bad, because if Embarcadero changes its implementations, old code might break, and thus FPC old code might break as well.

That's why I'm going careful with this, to know before acting, and learn more before reporting at the bugtracker. Right now I just program as hobby, and I mainly use simple stuff like TStringList which is easy to deal with. I see some shortcomings with the current implementation of TStringList, at least in the way I want to use it, and I just want to know if the community in general could benefit from my ideas to overcome those limitations.
Lazarus 1.6.4 + FPC 3.0.2 64bits under Windows 7 64bits
Only as a hobby nowadays
Current proyect release: TBA

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11455
  • FPC developer.
Re: Is it possible to suggest a different implementation in the RTL?
« Reply #5 on: January 07, 2015, 03:17:25 pm »
Maybe this uses some notification system? Try with an object that writes some text in the destructor to verify it is called.

Anyway if you decide to submit it, please package the tests (preferably as standalone console apps) with it.

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Is it possible to suggest a different implementation in the RTL?
« Reply #6 on: April 15, 2015, 10:09:09 pm »
...
No chance unfortunately.
If you touch code that also exists in Delphi, no improvements (in Lazarus terms: incompatibilities) will be accepted.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

 

TinyPortal © 2005-2018