4 ms·
With java scalar replacement (possible from escape analysis/partial or not) calling new Point(x,y) in a hot loop does not allocate an object on heap at all (or
by jerven 10y ago
With java scalar replacement (possible from escape analysis/partial or not) calling new Point(x,y) in a hot loop does not allocate an object on heap at all (or even the stack) unless it escapes.
In the newer JITs that escaping allocation will be deferred until the last possible point in time.
This is actually a nice point about JVM languages, _new_ has defined behavior and can thus be elided if the effect is not observable. malloc (or new in C++, although I am not sure about the spec there) do not and eliding a call to new may break the language specification as function is not called that should have been.
I don't know what Rust does here, and what is allowed in regards to the new operator. i.e. must a local variable be on the stack or can it generate direct values in machine registers?
- Rusky 10y agoRust doesn't have a new operator (its equivalent to malloc is Box). When that's used, it forces the value onto the heap- but that's not what you would write most of the time. Local variables that are not explicitly heap-allocated (and don't have their address taken) can be on the stack or in registers, and this is true of both Rust and C/C++.