3 ms·
You're right about thread safety. And when you do want it, you probably want more guarantees than StringBuffer gives you (ability to synchronize multiple insert
by hyperpape 10y ago
You're right about thread safety. And when you do want it, you probably want more guarantees than StringBuffer gives you (ability to synchronize multiple inserts, for instance).
With regard to the sufficiently smart compiler: it seems like you'd need every method the StringBuffer is passed to be final to do that kind of analysis.
In the case where you're writing:
public void foo(List<String> strings) {
StringBuffer x = new StringBuffer();
for (String s: strings) {
x.append(s);
}
}
You could conceivably do the analysis at compile time. But the whole idea behind the JIT is that you don't need to. If the code is hot, it'll get compiled, and you'll probably be fine thanks to removing all the synchronization, while if it's not hot no one cares.
- Someone 10y agoYou're right, I forgot that all methods are implicitly virtual by default in Java.
- vardump 10y agoNot quite same as "virtual" in C++ though. Java methods can still be inlined at runtime. I wish C++ had a runtime that could do same, inline smaller virtual methods, function pointers and shared library (.dll/.dylib/.so) calls at runtime. It could also do other transformations, like optimize those branches away it can prove to be always true or false. It could flatten most abstractions, removing redundant parameter checks and call frame setups/teardowns. It could also do CPU (instruction set) specific optimizations. I think C++ runtime that does this partial JITting is possible, but not easy. However, the benefits are interesting. While getting there won't be easy, you can achieve faster than native performance. Something like this has already been actually done. Some examples below. QuaC: http://digitalassets.lib.berkeley.edu/techreports/ucb/text/CSD-94-792.pdf http://digitalassets.lib.berkeley.edu/techreports/ucb/text/C... DynamoRio: DynamoRio had some early success JITting native binaries resulting faster than native performance. Unfortunately current versions are slower than native. https://github.com/DynamoRIO/dynamorio https://github.com/DynamoRIO/dynamorio). https://dspace.mit.edu/bitstream/handle/1721.1/87441/54863587-MIT.pdf?sequence=2 https://dspace.mit.edu/bitstream/handle/1721.1/87441/5486358...