Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #146016
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: in place swap of float types |
| Date | 2019-09-05 14:54 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <qkr0i2$322$1@dont-email.me> (permalink) |
| References | (6 earlier) <91wbF.219020$893.137679@fx02.am4> <qknvie$as0$1@dont-email.me> <s_MbF.456041$_l5.198989@fx32.am4> <qkofkt$6m7$1@dont-email.me> <pMQbF.165925$Xs5.5927@fx06.am4> |
On 04/09/2019 17:30, Bart wrote:
> On 04/09/2019 14:53, David Brown wrote:
>> On 04/09/2019 13:12, Bart wrote:
>
>>> The incidence of swap() is not high, but it depends on the code: in a
>>> general code base of mine of 100K lines (mostly language stuff), it
>>> occurs once in about 4000 lines. That doesn't sound much, but it's
>>> exactly the same incidence as fopen() is called.
>>>
>>
>> I don't think my code would have as many swaps as that. But then, I
>> don't think I've used "fopen" more than once or twice in total, in all
>> my C code. I don't view my own code as "typical" of C code, however, to
>> the extent that I even think it makes sense to talk about "typical" C
>> code.
>>
>> But of course your own code is the best source of statistical samples
>> for your tools.
>>
>> At a rate of 1 per 4000 lines, I would not see "swap" as a candidate for
>> a language feature and keyword, but perhaps for a "standard library"
>> function.
>
> The same code base had a slightly lower incidence of XOR (^ or ^= in C)
> yet XOR is common in languages. And MAX occurred as frequently as SWAP
> (MAX is also part of some instruction sets).
>
There is always a balance in these things, and a matter of choices. One
important metric is to ask how often the feature is needed (around the
same usage rate, for your statistics, but that may not match statistics
for C). Another is to ask how you can get the results using the
existing language. "x ^ y" is quite awkward to duplicate when you need
it but don't have an xor operation - "(x | y) & ~(x & y)". Swap and max
are simpler. And when considering the use of names or operator symbols,
think about how often the feature will be used in complex expressions.
Again, I am not saying "do it like C", or "don't make "swap" a built in
function in your language". I am merely encouraging that you think
about it, and make considered decisions on a solid basis. The focus
should be on what is best for the programmer, the user of the language.
I feel that sometimes your focus is on showing off the simplicity of
your compiler (adding features just because it is easy to do so), or on
picking features precisely because C does not have that feature. As
justifications for features, these are not much better than saying you
are adding the <^v> "min-max-swap" operator because you had a dream
about it. And I know you don't want to go there!
Depending on the expected usage domains of your language, I think it
would be reasonable to relegate "xor" to a standard library function
too, leaving the ^ symbol for a power operator, or re-direction operator
(like in Pascal).
> However, as I understand it you don't have block scope
>> temporaries in your language, which would make { T t = a; a = b; b = t;}
>> sequences more inconvenient in your language than in C. That may affect
>> the weighting and balance here.
>
> In interpreted byte-code languages, optimisation is difficult. Then,
> expressing your intention directly can mean fewer byte-codes and faster
> execution. And when the interpreter sees a 'swap' byte-code, it can make
> that very efficient even for large data.
>
> And, once 'swap' is used in one language, you want to use it in others too.
>
>>> Most uses (2/3 or more) are to swap two locals, the rest swap data in
>>> memory.
>>
>> If I were making my own language, I'd support multiple assignment. Then
>> there would be no need for "swap" as a function, builtin, macro or
>> anything else - you'd just write "a, b = b, a".
>
> I have multiple assignment too. There, using (a,b):=(b,a) also generates
> four instructions like swap(a,b).
>
> But with more complex terms like (a[i],b[j+1):=(b[j+1],a), each term has
> to be written twice, duplication that the compiler now has to try and
> eliminate; it brings up the issue of side-effects; it risks extra typos;
> and it means you might not be able to see at a glance if this is indeed
> a swap, or whether something else is intended.
It is all about the level of usage. It's fine for complex actions to
require complex code, especially if it is used only on occasion.
(And, as I have mentioned before, good use of spaces will make your code
hugely more legible.)
To me, multiple assignment removes the need for a special "swap" feature
for handling common cases. I would want a "swap" function if it were
designed to handle more complex types (such as handling "deep swaps" of
trees or lists), and there the critical point is that it must be
extendable and overloaded by user code - /not/ a compiler built-in fixed
function.
>
> The real test is this: if you were writing pseudo-code to exchange A[i]
> with A[i+1], which of these would you write:
>
> T temp=A[i]; A[i]=A[i+1]; A[i+1]=A
>
> A[i], A[i+1] = A[i+1], A[i]
>
> swap(A[i], A[i+1])
>
> in order to most clearly express what is intended? Pseudo-code ought to
> be a more common inspiration for language designers.
>
There is no doubt about the order of clarity there. But I don't agree
that this is "the real test". It is certainly an important
consideration, but it is not the only one.
>>
>> There are safer ways to handle a swap macro. A step forward is :
>>
>> #define swap(a, b) \
>> { typedef typeof(a) T; \
>> T* pA = &(a); \
>> T* pB = &(b); \
>> T t = *pA; \
>> *pA = *pB; \
>> *pB = t; \
>> }
>>
>> This is safe from side-effects like above. It will not work if the
>> parameters are arrays, but at least it will fail with a compile-time
>> error in such cases - a vast improvement over silent mistakes.
>
>> Where possible, prefer "static inline" functions to macros. You can't
>> make them type generic, but the parameters are only evaluated once.
>>
>>
>> C++ templates are better:
>>
>> template < typename T >
>> void swap(T &a, T &b) {
>> T t = a;
>> a = b;
>> b = t;
>> }
>>
>> With C++ concepts, it is even neater:
>>
>> void swap(auto &a, auto &b) {
>> auto t = a;
>> a = b;
>> b = t;
>> }
>>
>
> It looks like you acknowledge that 'swap' can be useful, but don't want
> it as part of a language, yet you demonstrate several ways of doing just
> that.
>
I entirely agree that it can be useful. And if /you/ think it is
important enough to be part of /your/ language, that is /your/ choice.
I am happy to see it as part of a standard library, I am less keen on it
being built into the language. I see that as having lots of
disadvantages and very few advantages.
> As I said, it would be simplest as a built-in, then all the problems
> with duplication, side-effects, typos, generics and clarity are all
> taken care of. Even the name pollution is reduced to one common
> identifier across a million programmers.
>
No, you don't take care of everything with a builtin "swap". You take
care of /some/ things, yes. But not others.
You can have both ways, of course - and get the maximum benefits. You
can have "__builtin_swap(a, b)" which does a pure memory swap of "a" and
"b" in the most optimised way. And then you could have your standard
library include:
namespace sys {
void swap(auto &a, auto &b) {
__builtin_swap(a, b);
}
}
Then people can use "sys::swap" for swapping, getting the most efficient
possible code for normal types. And they can override it for types that
need something more complex. They can have "using namespeace sys;" or
"using sys::swap;" somewhere in their code, and then use the name "swap"
as though it were part of the language - yet it is still magically
overrideable. They can use "swap" as their own identifier and still
access "sys::swap" or "__builtin_swap" according to preference.
(Of course you might want a different syntax for namespaces than C++,
but I would definitely recommend having something equivalent. Lack of
namespaces is a big disadvantage of C, IMHO, and not one to copy. It
may or may not be combined with a concept of modules.)
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
in place swap of float types 3queens@shabbyroad.com - 2019-08-27 17:03 +0000
Re: in place swap of float types Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-08-27 19:22 +0000
Re: in place swap of float types Andrew Smallshaw <andrews@sdf.org> - 2019-09-02 15:25 +0000
Re: in place swap of float types Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-09-02 19:37 +0000
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-09-03 06:37 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-03 16:13 +0200
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-03 16:54 +0100
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-04 11:19 +0200
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-09-04 02:35 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-04 12:31 +0200
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-04 12:12 +0100
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-04 15:53 +0200
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-09-04 07:17 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-05 15:12 +0200
Re: in place swap of float types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-09-05 21:11 +0100
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-04 16:30 +0100
Re: in place swap of float types jadill33@gmail.com - 2019-09-04 10:26 -0700
Re: in place swap of float types Ike Naar <ike@iceland.freeshell.org> - 2019-09-05 06:00 +0000
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-05 11:14 +0100
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-05 14:54 +0200
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-05 15:48 +0100
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-06 10:35 +0200
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-09-06 02:52 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-06 13:18 +0200
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-06 12:14 +0100
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-06 15:08 +0200
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-09-06 06:23 -0700
Re: in place swap of float types Keith Thompson <kst-u@mib.org> - 2019-09-04 14:16 -0700
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-09-04 22:42 +0100
Re: in place swap of float types Keith Thompson <kst-u@mib.org> - 2019-09-04 15:21 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-05 15:15 +0200
Re: in place swap of float types Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-09-04 08:05 -0600
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-05 15:17 +0200
Re: in place swap of float types Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-09-05 20:28 -0600
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-06 10:39 +0200
Re: in place swap of float types Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-09-06 08:11 -0600
Re: in place swap of float types scott@slp53.sl.home (Scott Lurndal) - 2019-09-06 18:31 +0000
Re: in place swap of float types Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-09-04 19:02 +0000
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-02 22:16 +0200
Re: in place swap of float types Robert Wessel <robertwessel2@yahoo.com> - 2019-08-27 17:59 -0500
Re: in place swap of float types Richard Damon <Richard@Damon-Family.org> - 2019-08-27 17:45 -0700
Re: in place swap of float types James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-08-27 20:48 -0400
Re: in place swap of float types Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-09-03 06:17 -0700
Re: in place swap of float types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-08-28 02:09 +0100
Re: in place swap of float types Siri Cruise <chine.bleu@yahoo.com> - 2019-08-27 21:26 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-08-28 09:22 +0200
Re: in place swap of float types Andrey Tarasevich <andreytarasevich@hotmail.com> - 2019-08-28 16:36 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-08-28 09:36 +0200
Re: in place swap of float types Bart <bc@freeuk.com> - 2019-08-28 12:02 +0100
Re: in place swap of float types James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-08-28 05:08 -0700
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-08-28 05:26 -0700
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-08-28 14:40 +0200
Re: in place swap of float types Keith Thompson <kst-u@mib.org> - 2019-08-28 13:01 -0700
Re: in place swap of float types Philipp Klaus Krause <pkk@spth.de> - 2019-09-04 11:18 +0200
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-04 12:57 +0200
Re: in place swap of float types Philipp Klaus Krause <pkk@spth.de> - 2019-09-04 13:24 +0200
Re: in place swap of float types Bonita Montero <Bonita.Montero@gmail.com> - 2019-09-05 08:18 +0200
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-05 15:19 +0200
Re: in place swap of float types Bonita Montero <Bonita.Montero@gmail.com> - 2019-09-05 16:18 +0200
Re: in place swap of float types Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-09-05 07:32 -0700
Re: in place swap of float types Bonita Montero <Bonita.Montero@gmail.com> - 2019-09-05 17:32 +0200
Re: in place swap of float types David Brown <david.brown@hesbynett.no> - 2019-09-06 10:54 +0200
Re: in place swap of float types Bonita Montero <Bonita.Montero@gmail.com> - 2019-09-06 14:23 +0200
csiph-web