5 ms·
Every language has their esoteric quirks. Tell me that it isn't weird to write this https://en.wikipedia.org/wiki/Duff%27s_device https://en.wikipedia.org/wiki
by chii 7y ago
Every language has their esoteric quirks.
Tell me that it isn't weird to write this https://en.wikipedia.org/wiki/Duff%27s_device https://en.wikipedia.org/wiki/Duff%27s_device
Tell me it isn't strange that array access is the same as pointer deref (aka a[index] is same as index[a])
- enlyth 7y ago> counting words > esoteric quirks It shouldn't be this easy to shoot yourself in the foot when doing simple tasks
- cosarara 7y agoThe Duff's device is a low level optimization that can even hurt modern processors due to branch prediction. It's a niche trick from the olden days. It is very strange on first sight that a[index] is same as index[a], but you will never see the second one used in practice. When you are learning, you can write your code using normal array access syntax and solve problems. And then, you can learn to use pointers and deref them. And then one day you might realize that x[y] is *(x+y) and thus y[x] and that it looks funny. But that will be a long time after writing `wc`.
- heavenlyblue 7y agoDoes that even work in C when sizeof x[i] > 1?
- cosarara 7y agoYes, it does. You can try it yourself with this code example: https://repl.it/repls/PutridUselessComputer https://repl.it/repls/PutridUselessComputer
- heavenlyblue 7y agoIt still made more sense to me more than foldl vd foldl’.
- sifar 7y agoHow would this work for unaligned accesses - which index would very likely be. A neat way to look at things nevertheless, thanks.
- cosarara 7y agoThe alignment doesn't matter. `index` is not an offset in bytes, it's just an index, a number. The position in memory of the variable also doesn't matter as it is not used, only the value. `index[arr]` means `*(index + arr)`. Which is exactly the same thing as arr[index], no matter how you look at it. The type of arr is taken into account for pointer arithmetic. Whatever works with `arr[index]` will also work with `index[arr]`. You can also use a constant, for example `2[arr]`. Damn I hate HN formatting.
- sifar 7y agoyes, I realized this after asking the question :).I must admit I never thought it in this way. Thinking more about this, this might fail if the compiler uses post-incrementing load/stores which update the pointer addresses too. Or will it not use them if I am using index[arr] ? Lets say the load/store updates the pointer too. So after first execution, ptr will index+arr in both cases, but during the next iteration , the former will be ptr[index] while the latter will be ptr[arr].
- tom_mellior 7y agoUnless you try hard to provoke one, there is no unaligned access. Adding a pointer and an integer in C always displaces the pointer by elements, not by bytes. So if p is a pointer to some type T and is aligned, then p + 1 will be aligned as well. You need to cast to another pointer type, do arithmetic on that, then cast back to produce an unaligned access.
- coldtea 7y ago>Tell me that it isn't weird to write this https://en.wikipedia.org/wiki/Duff%27s_device* https://en.wikipedia.org/wiki/Duff%27s_device* Duff's device is an abuse of an obscure syntax option nobody expected, and it's also seldom used. Foldr, on the other hand, is bread and butter in Haskell. >Tell me it isn't strange that array access is the same as pointer deref (aka a[index] is same as index[a])* I can tell you, it isn't strange.
- klingonopera 7y agoNo, it is strange, because C decides to dereference according to the datatype of "a" in both cases, and not according to how the user syntactically declares what is to be understood as array and index. In other words, if a is char[]/char* and index is int, C does *((char*)a + (int)index) and *((int)index + (char*)a) and not as one could expect *((char*)a + (int)index) and *((int*)index + (char)a) for a[index] and index[a] respectively.
- simias 7y agoI always understood it as some kind of compiler-level macro, where a[b] is expanded into <star>(a + b) and then normal arithmetic rules take over, in which case there's no strangeness as far as I can tell (i.e. it doesn't matter which of a or b is the pointer and which is the integer because C defines both "int + ptr" and "ptr + int" as doing the same thing). But frankly it's a bad choice if we're talking about C's "esoteric quirks" because both this and Duff's Device are things that you effectively never encounter in the wild (it's the "Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo" of the C language, they're interesting to know about but not of much practical use). There are much better "features" of C to single out, for instance the two million different meanings for the "static" keyword, the fact that arrays declared as function parameters do not behave like arrays but like pointers (hey, you can put "static" in there too now! But it still doesn't make it work like a proper array), the many surprising undefined behaviors regarding integer overflow, aliasing rules etc...
- gpderetta 7y agoAdding a pointer and an index yield a pointer no matter which side the pointer is, i.e operator+ is commutative. It would be extremely surprising if switching sides of the sum would implicitly convert an index to a pointer. You could argue that operator[] should not allow a non pointer on the LHS, and you would probably right, but as it stands now, it is just syntactic sugar for operator+ and just dereferencing the while expression.
- mschaef 7y ago> Every language has their esoteric quirks. But most let you avoid them in favor of concepts manageable by an average middle schooler.
- UserIsUnused 7y agoAnd haskell does this. This is way over engineering for performance. Different level. It's the same in other language when you go deep into performance engineering, it just depends how deep considering if the algorithm fits better a functional or imperative model.
- mschaef 7y ago> It's the same in other language when you go deep into performance engineering Take a look at the optimized code for the C version of wc: https://opensource.apple.com/source/text_cmds/text_cmds-68/wc/wc.c.auto.html https://opensource.apple.com/source/text_cmds/text_cmds-68/w... There are a couple special cases for a line count and a total character count, but it's essentially a couple of nested loops. I agree with you that going deep into performance engineering brings out a lot of esoteric edge cases, but in this case for the C program it wasn't even necessary to go down that path. Basic concepts in C outran or at least matched something heavily optimized in Haskell. I'm a big fan of higher level languages, and use them almost exclusively myself in my day job, but there's something to be said for basic constructs that just run quickly out of the box. It may be harder to optimize the C ode, but you're also less likely to need to do so.
- cmroanirgo 7y agoDisclaimer: old timer (not sure if you whippersnappers do the same anymore). The few times that I really needed performance out of a particular C/C++ function, I'd enable the compiler's output to generate ASM, and then see what was being generated, and either rework the code (to keep it readable for the next guy), or optimise directly with ASM, and often an iterative mix of the two. Loop unrolling (as in Duff's device) was a common one for array iteration performance improvements. I don't see a lot of "optimized C" in the wc impl. I'd imagine that wc optimised for use in a GPU would be pretty spectacular, even beyond using a few ordinary cores like the Haskell result did... but as you say, it's pretty unnecessary in this case.
- boomlinde 7y agoStrange, but unsurprising after seeing the examples, IMO. Knowing that the memory spike in Haskell is due to the strict form of foldl (as indicated by the tick) only being strict up to "weak head normal form" seems quite a bit more esoteric to me. But maybe I am biased due to learning more about how computer implementations fundamentally operate (in which case the only surprising thing about a[b] == b[a] is that the C standard allows it) than FP theory and how that abstraction can be baked down into something that the computer can actually execute efficiently.
- roca 7y agoNormal programmers never write those things, and one practically never sees them in real life, so they're not relevant.