3 ms·
Java is a bad example since it could (theoretically) do all kinds of optimizations based on runtime profiling under the hood. It also has features like deduplic
by zuzun 9y ago
Java is a bad example since it could (theoretically) do all kinds of optimizations based on runtime profiling under the hood. It also has features like deduplication, so your string might use no additional memory at all.
- vog 9y ago> Java is a bad example since it could (theoretically) do all kinds of optimizations based on runtime profiling under the hood Isn't that true for every language? No offense intended, but this strongly reminds me of the "Sufficiently Smart Compiler" [1] The Java community had more than enough time to implement that, especially compared to a young language like Rust, so there's probably a reason they don't have it yet. [1] http://wiki.c2.com/?SufficientlySmartCompiler http://wiki.c2.com/?SufficientlySmartCompiler
- twic 9y agoThey have implemented it. javac compiles chained string concatenation to a sequence of StringBuilder calls, and HotSpot knows how to specifically optimize those: https://groups.google.com/forum/m/#!topic/jvm-languages/KtVBYfeT83g https://groups.google.com/forum/m/#!topic/jvm-languages/KtVB... That said, I'm sure there are common cases it doesn't catch.
- moomin 9y agoNot really, you've identified something where the compiler spots a special case pattern and rewrites it, something available to any compiler writer who is sufficiently chummy with the standard library. I'm not saying it isn't useful, but it's nothing like the runtime optimisations the OP was talking about. It can't even handle a for loop... Closest real world example to a sufficiently smart compiler is GHC, and that can be plenty dumb at times.
- vog 9y agoAnother example is the query planner of a good relational database (PostgreSQL, etc.). Here, SQL can also be viewed as a lazy evaulated programming language, due to its declarative nature and the fact that the execution order is not defined in the code, but instead an (hopefully optimal) execution order is determined by the query planner.
- twic 9y ago> Not really, you've identified something where the compiler spots a special case pattern and rewrites it Which, string handling being specifically optimised by the compiler, is exactly what was being discussed upthread?
- moomin 9y agoWhat was being discussed upthread went a lot further than what you're talking about. The string concat thing is a specific piece of ordinary static analysis that any language with a compile stage can perform. It's also pretty trivially defeated in ways that crop up in real code. I'm not saying it isn't useful, I'm saying it's nowhere close to what would be required to render the performance costs of doing strings the Java way equivalent to the Rust way. Therefore, to go even further upthread, Rust still provides a meaningful choice, trading Java's convenience for Rust's performance.