Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2010-09

PShortString

8 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Hans-Peter Diettrich

When dynamic strings are used all around, is the use of pointers to ShortString still recommended? (fmodule contains a lot of them)

In many cases (almost all I found) the strings are duplicated when assigned, because the given dynamic strings disallow to simply copy the reference.

IMO P[Short]Strings only were useful when the referenced string shall be changed later, with that change reflected automatically in all references. Since this seems not to occur anywhere, I'd suggest to replace or retype PShortString by [Ansi]String.

DoDi

Reply by email

Florian Klaempfl
to Hans-Peter Diettrich
Hans-Peter Diettrich wrote
When dynamic strings are used all around, is the use of pointers to ShortString still recommended? (fmodule contains a lot of them)

1. Ancient code, keep in mind, most code not being back end code was written ~10 years ago. At this time we even could not depend on perfectly working ansistrings.
2. Ansistring create an explicit exception frame etc. which slows down things.

Due to the explicit exception frame generation, I would propose not to change it but only if the length limitation is a problem.

Show quoted text 16 lines
In many cases (almost all I found) the strings are duplicated when assigned, because the given dynamic strings disallow to simply copy the reference. IMO P[Short]Strings only were useful when the referenced string shall be changed later, with that change reflected automatically in all references. Since this seems not to occur anywhere, I'd suggest to replace or retype PShortString by [Ansi]String. DoDi _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Sergei Gorelkin
to Hans-Peter Diettrich: When dynamic strings are used all around, is the use of pointers to ShortString still…
Hans-Peter Diettrich wrote
When dynamic strings are used all around, is the use of pointers to ShortString still recommended? (fmodule contains a lot of them)

Whenever you care about performance, you'll quickly realize that dynamic strings are plain inappropriate. When it is also known that 255 character limit won't be exceeded, ShortString is nearly an ideal choice.

Show quoted text 9 lines
In many cases (almost all I found) the strings are duplicated when assigned, because the given dynamic strings disallow to simply copy the reference. IMO P[Short]Strings only were useful when the referenced string shall be changed later, with that change reflected automatically in all references. Since this seems not to occur anywhere, I'd suggest to replace or retype PShortString by [Ansi]String.

They provide fine-grained control over memory allocations, avoiding implicit try..finally blocks and memory allocations for temps. Copying still occurs, but with the highly optimized Move procedure it is pretty cheap. So please don't change such things just because something is "used all around".

Regards,
Sergei

Reply by email

Hans-Peter Diettrich
to Florian Klaempfl: 1. Ancient code, keep in mind, most code not being back end code was written ~10 years…
1. Ancient code, keep in mind, most code not being back end code was written ~10 years ago. At this time we even could not depend on perfectly working ansistrings.

I'm talking about nowadays situation.

2. Ansistring create an explicit exception frame etc. which slows down things.

Also when the strings are const parameters, as they are in this case?

DoDi

Reply by email

Hans-Peter Diettrich
to Sergei Gorelkin: Whenever you care about performance, you'll quickly realize that dynamic strings are plain
Show quoted text 5 lines
> When dynamic strings are used all around, is the use of pointers to > ShortString still recommended? (fmodule contains a lot of them) > Whenever you care about performance, you'll quickly realize that dynamic strings are plain inappropriate.

The concrete use of PShortString strings is inappropriate in your sense, because it results in allocation, copy and deallocation of temporary strings in the code, in many places. This behaviour gave birth to my question.

Show quoted text 5 lines
They provide fine-grained control over memory allocations, avoiding implicit try..finally blocks and memory allocations for temps. Copying still occurs, but with the highly optimized Move procedure it is pretty cheap. So please don't change such things just because something is "used all around".

All that would no more be needed after a decent redesign. Or the existing code is inappropriate, and all the mentioned operations can be removed from the source code, without affecting the behaviour and result.

DoDi

Reply by email

Daniël Mantione
to Hans-Peter Diettrich

Op Fri, 10 Sep 2010, schreef Hans-Peter Diettrich:

Show quoted text 11 lines
Sergei Gorelkin schrieb: >> When dynamic strings are used all around, is the use of pointers to >> ShortString still recommended? (fmodule contains a lot of them) >> > Whenever you care about performance, you'll quickly realize that dynamic > strings are plain inappropriate. The concrete use of PShortString strings is inappropriate in your sense, because it results in allocation, copy and deallocation of temporary strings in the code, in many places. This behaviour gave birth to my question.

Ansistrings result allocation copy and deallocation during *processing*. The use of ansistrings would make the compiler at least twice as slow and quadruple its memory consumption.

Daniël

Reply by email

Florian Klaempfl
to Hans-Peter Diettrich: I'm talking about nowadays situation. Also when the strings are const parameters, as they…
Hans-Peter Diettrich wrote 7 lines
Florian Klaempfl schrieb: > 1. Ancient code, keep in mind, most code not being back end code was > written ~10 years ago. At this time we even could not depend on > perfectly working ansistrings. I'm talking about nowadays situation.

You asked for reasons why it is done as it is done.

Show quoted text 5 lines
> 2. Ansistring create an explicit exception frame etc. which slows down > things. Also when the strings are const parameters, as they are in this case?

If there is no other use, no. But if there is any processing or just a local ansistring: yes.

Reply by email

Sergei Gorelkin
to Daniël Mantione: Op Fri, 10 Sep 2010, schreef Hans-Peter Diettrich: Ansistrings result allocation copy and…

Daniël Mantione пишет:

Show quoted text 21 lines
Op Fri, 10 Sep 2010, schreef Hans-Peter Diettrich: > Sergei Gorelkin schrieb: > >>> When dynamic strings are used all around, is the use of pointers to >>> ShortString still recommended? (fmodule contains a lot of them) >>> >> Whenever you care about performance, you'll quickly realize that >> dynamic strings are plain inappropriate. > > The concrete use of PShortString strings is inappropriate in your > sense, because it results in allocation, copy and deallocation of > temporary strings in the code, in many places. This behaviour gave > birth to my question. Ansistrings result allocation copy and deallocation during *processing*. The use of ansistrings would make the compiler at least twice as slow and quadruple its memory consumption.

Also, don't forget that an AnsiString assignment, even when it doen not end up in memory allocations, will execute two intelocked instructions in multithreaded process.

Regards,
Sergei

Reply by email