Earlier quoted context omitted.
Specifically it's well-defined behavior to mutate an object after const_cast-ing away constness if the object wasn't const to begin with (const references or const pointers can refer to non-const objects). In your first example you have `int x = 1;` which isn't const, so the compiler has to assume that `f` may mutate it after const casting. In your second example you have `const int x = 1;` which is const so the comp…
Except that on embedded const objects might land on read only memory and cast-ing away constness will give hours of debugging pleasure.
When static makes your C code 10 times faster
101–110 of 113 posts
Re: When static makes your C code 10 times faster
#102Earlier quoted context omitted.
How do you set a variable as mutable after the fact in rust?
You can do: let foo = "1234".to_string(); let mut foo = foo; foo = "5678"; This only works if you have ownership of the variable though. And in practice it's a pretty good compromise, because it's pretty hard to do this by mistake. Note: const in rust is different to const in some other languages: it represents a value that is duplicated inline into each use site at compile time.
Re: When static makes your C code 10 times faster
#103Earlier quoted context omitted.
Except that on embedded const objects might land on read only memory and cast-ing away constness will give hours of debugging pleasure.
They covered that with if the object wasn't const to begin with .
Here are the references to linker scripts?
Re: When static makes your C code 10 times faster
#104Earlier quoted context omitted.
They covered that with if the object wasn't const to begin with .
Partially "(const references or const pointers can refer to non-const objects)" . Here are the references to linker scripts?
I believe this account of things is accurate (ignoring C++'s reference types for simplicity):
The C and C++ standards are written in such a way as to enable compilers to place constant data on read-only memory. Casting away constness is never illegal in and of itself, but if you do so and then assign to a variable which was declared as const, that is undefined behaviour.
Similarly, assigning into the character array of a string literal is undefined behaviour, whether or not const was used. Again that's to enable the compiler to make use of read-only memory.
Going in the other direction is safe, as preventing assignments doesn't introduce problems. That is to say, using a pointer-to-const type to point to a non-const variable poses no problem.
Related:
• https://stackoverflow.com/a/9079161/
• https://wiki.sei.cmu.edu/confluence/display/c/EXP05-C.+Do+no...
Re: When static makes your C code 10 times faster
#105Earlier quoted context omitted.
I respectfully disagree on both points. > I don't think your program is better when you pass in i/o functionality to every single class context in the constructor. Abstracting over I/O transport is an excellent thing to do. This allows you to do things like easily record and replay a network stream. Which is useful for both debugging and automated tests. I/O comes in a kazillion flavors. Networked, interprocess, seri…
There is one common situation where not using mutable globals is a recipe for complexity and a serious code smell. That case is when your internal structures directly control the physical resources of the machine. There is no amount of window dressing that can make these anything but global structures because that is what they literally are. That gets hidden a bit if you delegate resource management to the OS but you…
There are definitely resources which are globally unique. But that does not necessarily follow that access should also be global.
Rust has some elegant patterns when working with embedded devices. For example GPIO pins could totally be stateful globals. But instead their passed around as types and Rust’s type system + borrow checker ensure correctness. It’s pretty neat.
I’ll assume you’re right for databases. My expertise is real-time VR video game type stuff. Which is also high-performance, but of a different variety.
I strongly agree that layers of abstraction compound into convoluted and inscrutable garbage. I loathe web development for this very reason.
Re: When static makes your C code 10 times faster
#106Earlier quoted context omitted.
>Division is slow I wondered if that's really still true, since I haven't done much assembly language programming since PowerPC was new. Here, it says the M1 has 7-9 cycles latency for division instructions, but throughput of 2 cycles per . https://dougallj.github.io/applecpu/firestorm-int.html "The M1 is 10x faster than the Xeon at 64 bit divides. It’s…just wow." So, given all of the other things that can slow you u…
You shouldn't assume you're running on the performance cores. Not everyone is writing an app, and even if you are, most of your code will be better off on the efficiency cores.
Re: When static makes your C code 10 times faster
#107Earlier quoted context omitted.
Yes, see last few paragraphs of the post, in which I also speculate why GCC doesn't infer that there is only one compilation unit.
How could it ever infer it, given C's compilation model? The only option is when all TU are given at the same time to the compiler, or when LTO is used (in which case it is actually the linker doing the work). Even then, this won't apply to libraries.
Exactly -- which is the case here. But implementing such a cross cutting implementation would probably be annoying, which is what I wanted to convey with
> I think they could concievably assume that the value of modulus won’t be changed in this case, since we’re producing an executable directly, but it’s probably annoying to have an optimization looking so far into the future of the compiler pipeline.
Re: When static makes your C code 10 times faster
#108Re: When static makes your C code 10 times faster
#109This can be accomplished more simply and reliably by marking modulus as const. The compiler currently has to reason about the whole compilation unit to determine that modulus is not modified, which works. However, if future code modifies modulus (either on purpose or accidentally) or something changes that prevents the compiler from performing global reasoning, the optimization will be lost. By marking the actual int…
Re: When static makes your C code 10 times faster
#110Without `static`, compiler exports a symbol. $ cat value.c int value = 42; int get_value() { return value; } $ make value.o gcc -c -o value.o value.c $ nm value.o 0000000000000000 T get_value U _GLOBAL_OFFSET_TABLE_ 0000000000000000 D value This symbol, not being `const` can be modified by any other compilation unit. $ cat main.c #include int value; int get_value(); int main() { value = 123456789; printf("%d\n", get_…