SWAP keyword

Forum for discussion about the documentation project.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SWAP keyword

Post by fxm »

Proposed note improvement:
Note: For var-len strings, Swap is more efficient than a rustic exchange by circular assignments (using a third temporary string), because only the strings descriptors are exchanged rather than all strings data characters.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: SWAP keyword

Post by MrSwiss »

Your Poposal: -> OK, for String

I'd like you, however, to add something like:
The same applies to Ptr's, even in UDT's (Ptr's swapped, no change to 'pointed to' values).

<OT>
just had a server failure (trying to post):

503

Service Unavailable

The server is temporarily busy, try again later!
</OT>
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: SWAP keyword

Post by Tourist Trap »

fxm wrote:Proposed note improvement:
Note: For var-len strings, Swap is more efficient than a rustic exchange by circular assignments (using a third temporary string), because only the strings descriptors are exchanged rather than all strings data characters.
Hi fxm. Not sure than rustic is neutral enough. After all, some may have a good reason to use the "rustic" case in some special case. Maybe "classical", or "basic" would be more suitable.

I have a question that bothers me. If the UDT are swapped more efficently than the standard types, at least for the INTEGER. Would you recommend to encapsulate the INTEGER in a UDT, if we were to be extremist (or simply if we have a real performance challenge in this context)?
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SWAP keyword

Post by fxm »

Tourist Trap wrote:
fxm wrote:Proposed note improvement:
Note: For var-len strings, Swap is more efficient than a rustic exchange by circular assignments (using a third temporary string), because only the strings descriptors are exchanged rather than all strings data characters.
Hi fxm. Not sure than rustic is neutral enough. After all, some may have a good reason to use the "rustic" case in some special case. Maybe "classical", or "basic" would be more suitable.
MrSwiss wrote:Your Poposal: -> OK, for String

I'd like you, however, to add something like:
The same applies to Ptr's, even in UDT's (Ptr's swapped, no change to 'pointed to' values).
Okay both for your remarks:
Note: For var-len strings, Swap is more efficient than a classical exchange by circular assignments (using a third temporary string), because only the strings descriptors are exchanged rather than all strings data characters. The same applies to Ptr's, even in UDT's (Ptr's values swapped, no change to 'pointed to' data).
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: SWAP keyword

Post by Tourist Trap »

fxm wrote: Note: For var-len strings, Swap is more efficient than a classical exchange by circular assignments (using a third temporary string), because only the strings descriptors are exchanged rather than all strings data characters. The same applies to Ptr's, even in UDT's (Ptr's values swapped, no change to 'pointed to' data).
Hi fxm,

thanks first,

then just to offer you an alternative to think of. I would make it differently. Here we see a general case, where a variable type is compound of a header + data. What is faster obviously is to move the headers alone, without the burden of the data. To illustrate this, we can then derivate both the case of the string (header = descriptor) and the pointer (header = address).

Of course I dont ask you to express things like that, because the term header is probably already dedicated to very different cases. It's still how I would try to reformulate if I knew the right wording. Anyway your proposal already gives most of the informations, just not the general principle - maybe.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SWAP keyword

Post by fxm »

Tourist Trap wrote:I have a question that bothers me. If the UDT are swapped more efficently than the standard types, at least for the INTEGER. Would you recommend to encapsulate the INTEGER in a UDT, if we were to be extremist (or simply if we have a real performance challenge in this context)?
Encapsulate an Integer in a UDT does not improve the efficiency of 'Swap'.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: SWAP keyword

Post by Tourist Trap »

fxm wrote: Encapsulate an Integer in a UDT does not improve the efficiency of 'Swap'.
Yes I think that now I see why. If I'm on the right path with my previous intuition. Here, the *header* is of the same weight as the *data*, so no win.
There is a mystery then, why is there a slight advantage still obvservable when using swap with integers, rather than when we have the classical use of temp?
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SWAP keyword

Post by fxm »

Tourist Trap wrote:then just to offer you an alternative to think of. I would make it differently. Here we see a general case, where a variable type is compound of a header + data. What is faster obviously is to move the headers alone, without the burden of the data. To illustrate this, we can then derivate both the case of the string (header = descriptor) and the pointer (header = address).

Of course I dont ask you to express things like that, because the term header is probably already dedicated to very different cases. It's still how I would try to reformulate if I knew the right wording. Anyway your proposal already gives most of the informations, just not the general principle - maybe.
Perhaps:
Note: For var-len strings, Swap is more efficient than a classical exchange by circular assignments (using a third temporary string), because only the strings descriptors are exchanged rather than all strings data characters.
More generally, when the data are referenced by a pointer, alone or within a descriptive structure (a UDT, for example), it is most efficient to swap only the pointers values or the descriptive structures fields without accessing data themselves.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: SWAP keyword

Post by MrSwiss »

fxm wrote:Perhaps:

Note: For var-len strings, Swap is more efficient than a classical exchange by circular assignments (using a third temporary string), because only the strings descriptors are exchanged rather than all strings data characters.
More generally, when the data are referenced by a pointer, alone or within a descriptive structure (a UDT, for example), it is most efficient to swap only the pointers values or the descriptive structures fields without accessing data themselves.
I'd swap the two parts:
start with the general part (sort of: in principle)
then:
use the string (as a example) for a struct copy vs. comlpete round the circle assigning.
Last edited by MrSwiss on May 04, 2019 19:21, edited 1 time in total.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SWAP keyword

Post by fxm »

Tourist Trap wrote:There is a mystery then, why is there a slight advantage still obvservable when using swap with integers, rather than when we have the classical use of temp?
For example, Swap uses an internal register as temporary variable.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SWAP keyword

Post by fxm »

Next proposal:
Note: More generally Generally, when the data are referenced by a pointer, alone or within a descriptive structure (a UDT, for example), it is most efficient to swap only the pointers values or the descriptive structures fields without accessing data themselves.
That's why for var-len strings, Swap is more efficient than a classical exchange by circular assignments (using a third temporary string), because only the strings descriptors are exchanged rather than all strings data characters.
[edit]
Taken into account MrSwiss remark below.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: SWAP keyword

Post by MrSwiss »

Thumbs up, full marks, thanks ... just use *generally" as first word (after NOTE:)
by simply deleting "more"
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SWAP keyword

Post by fxm »

KeyPgSwap → fxm [Reworded Note]
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: SWAP keyword

Post by Tourist Trap »

fxm wrote:KeyPgSwap → fxm [Reworded Note]
Thanks a lot. This little note now helps understanding the logic behind.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: SWAP keyword

Post by counting_pine »

I think it’s enough to say that with variable-length strings, the descriptors are swapped without reallocating any string data memory.
And for UDTs, the contents of the structures are swapped without any operators or methods being called.

I don’t think it is worth discussing efficiency. The semantics are more important, and readers who are interested in efficiency should expect swapping just the descriptors to be more efficient anyway.

Overall, I think the documentation should be “as simple as possible, but no simpler”.
I’m also wary of over-documenting things. If a specific behaviour is documented in one version, then if it is changed it in future, that means breaking documented behaviour.
Post Reply