SWAP keyword

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

Re: SWAP keyword

Postby fxm » May 04, 2019 16:43

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: 3222
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: SWAP keyword

Postby MrSwiss » May 04, 2019 17:07

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: 2762
Joined: Jun 02, 2015 16:24

Re: SWAP keyword

Postby Tourist Trap » May 04, 2019 17:35

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
Posts: 9134
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SWAP keyword

Postby fxm » May 04, 2019 17:57

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: 2762
Joined: Jun 02, 2015 16:24

Re: SWAP keyword

Postby Tourist Trap » May 04, 2019 18:15

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
Posts: 9134
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SWAP keyword

Postby fxm » May 04, 2019 18:33

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: 2762
Joined: Jun 02, 2015 16:24

Re: SWAP keyword

Postby Tourist Trap » May 04, 2019 18:37

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
Posts: 9134
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SWAP keyword

Postby fxm » May 04, 2019 19:13

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: 3222
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: SWAP keyword

Postby MrSwiss » May 04, 2019 19:20

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
Posts: 9134
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SWAP keyword

Postby fxm » May 04, 2019 19:20

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
Posts: 9134
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SWAP keyword

Postby fxm » May 04, 2019 19:26

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: 3222
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: SWAP keyword

Postby MrSwiss » May 04, 2019 19:29

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

Re: SWAP keyword

Postby fxm » May 04, 2019 20:37

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

Re: SWAP keyword

Postby Tourist Trap » May 05, 2019 10:04

fxm wrote:KeyPgSwap → fxm [Reworded Note]

Thanks a lot. This little note now helps understanding the logic behind.
counting_pine
Site Admin
Posts: 6170
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: SWAP keyword

Postby counting_pine » May 05, 2019 15:32

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.

Return to “Documentation”

Who is online

Users browsing this forum: No registered users and 1 guest