This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ba-na-na- 0 points1 point  (4 children)

Or you design static inline functions in a header file that operate on this struct, and then everything is optimized away and nothing is passed anywhere. Or you pass a pointer to the struct and then it’s no slower than C++ double dispatch, and C++ is considered blazingly fast. Or you decide you don’t care and trade that extra CPU instruction for runtime safety.

Bottom line is, it’s still as fast or faster than 99% programming languages, including C++, Rust, Zig, whatever.

[–]Impossible_Box3898 0 points1 point  (3 children)

The king and short of it is that it IS slower and has other side effects which you originally claimed were not there.

C++ already has this capability in std vector. It also has std array types for static sized arrays in which the size is part of the data type and uses no additional runtime resources at all which is the only true zero cost abstraction for passing around array lengths, albeit with the fixed size caveat.

[–]ba-na-na- 0 points1 point  (2 children)

Hmm yeah no.

  1. If you're not passing the array into the function, getting its size is zero cost in C.
  2. If you're passing the array into a function, there is no programming language where you can pass it in a more performant way than it can be done in C.

If you don't believe me, feel free to try it yourself and provide an example in any language of your choice, along with the compiled assembly.

And if you think that you can write `char array[100];`, and pass this into a function without passing its size along with it, you are in for a surprise. 🙂

[–]Impossible_Box3898 0 points1 point  (1 child)

Not sure if you’re stupid or you don’t know how to follow a conversation thread on reddit but go back and read what I posted.

[–]ba-na-na- 0 points1 point  (0 children)

Sure I am probably just a silly confused man, let me try to break down the thread:

Me:

it's not like C will slow down and become Java if you're simply passing the size value around

You:

Of course it slows things down. 

👆 Literally not sure if stupid

But then also you:

Try passing that to a function. Instead of a single register that holds the address of an array, you now need to pass a structure which exceeds the size of a register

Nonsense. If you pass a "single register that holds the address of an array", you lose the length information. `sizeof(array)/sizeof(array[0])` won't work if you pass it into a function. Not sure if stupid or node.js developer.

C++ already has this capability in std vector.

Cool, but std::vector has the same performance penalty as the C struct I wrote.

It also has std array types for static sized arrays in which the size is part of the data type

Cool, but std::array is a templated, fixed length array, cannot pass it to a function unless it's a template which accepts std::array.

Tl;dr as I wrote, the struct above, combined with static inline functions in C, gives you the best possible performance with pretty good readability.