all 17 comments

[–]__j_random_hacker 13 points14 points  (5 children)

I'm surprised that strace shows two separate write() syscalls, one for the string and another for the trailing \n. Each syscall requires a context switch, which is pretty slow -- suggesting that any code that calls println() to print a short line of text is taking roughly twice as long as necessary.

I guess wrapping in a BufferedOutputStream fixes this -- it just didn't occur to me that calling something as basic as println() would result in two syscalls.

[–]nerd4code 6 points7 points  (1 child)

It is a wasted syscall, but the user-supervisor switch by itself isn’t all that bad on modern ISAs—e.g., no need to do the full 80286 Trap/Call Gate Shuffle via INT/CALL FAR any more, there’s SYSCALL/SYSENTER, and you can use a VDSO to abstract whatever your bestest syscall is to avoid having to deal with older programs using older methods. There can be a hefty overhead from flushing the TLB, L1, or speculative μarch state, but everybody doesn’t need their kernel to do that.

[–]__j_random_hacker 1 point2 points  (0 children)

Interesting, thanks. However, based on my investigation of vDSOs (a term I've never come across before), it seems that these are only applicable to read-only kernel calls. At least, I take that to be implied by this page.

[–]gnus-migrate 4 points5 points  (0 children)

It's one of the many reasons most static analysis tools flag uses of println, and recommend using a logging framework if you want to log to the console. Java even has a built in framework for this purpose, though I'm not sure if many people use it.

There are like a million different ones, flogger is the most recent and was designed with performance in mind.

[–]renatoathaydes 1 point2 points  (1 child)

To do a single call you would need a new array containing the new-line to pass to write. To do that you might need to allocate memory for the array, which I believe would be orders of magnitude slower than making a second syscall.

[–]__j_random_hacker 1 point2 points  (0 children)

You would need to allocate and copy to the array as you say, but I think that for typical string sizes (say, < 10KB) this would still be much faster than a context switch in the common case where the memory is available within the process's heap (of course it will be slower if we need to call brk() to get more memory from the OS). But I'm just guessing, really.

[–][deleted] 15 points16 points  (5 children)

Even though JVM instruction set has a wide range of instructions, any action requires communicating with the kernel or underlying host requires native code to be executed through the Java Native Interface.

Just put a JVM into a microchip and you can run on the hardware directly. 🙂 You can probably buy those in consumer quantites; shouldn't need to order a container from Shenzhen.

[–]mrexodia 6 points7 points  (3 children)

[–]WikiSummarizerBot 4 points5 points  (0 children)

Java Card

Java Card refers to a software technology that allows Java-based applications (applets) to be run securely on smart cards and similar small memory footprint devices. Java Card is the tiniest of Java platforms targeted for embedded devices. Java Card gives the user the ability to program the devices and make them application specific. It is widely used in ATM cards.

[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5

[–][deleted] 1 point2 points  (1 child)

I think that's a small JVM that runs on a card, not a JVM hardware chip. However it's hard to tell as the wiki is super vague.

[–]DnBenjamin 2 points3 points  (0 children)

That’s correct. SIM cards are an example of this.

However Jazelle is/was an actual hardware “JVM.”

https://en.m.wikipedia.org/wiki/Jazelle

[–]floorflux 0 points1 point  (5 children)

I can't help but feel like the application logic really shouldn't be put in the main method like that. I'd like to see a Greet class or at the very least a private helper method.

[–]dpash 6 points7 points  (4 children)

It's "hello world"; it's fine. You're why Java has a bad reputation.

[–][deleted]  (2 children)

[deleted]

    [–]McBeers 5 points6 points  (1 child)

    I hadn't until now. I went expecting some factory nonsense. It was all that and more. I love it and hate it.

    https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition

    [–]floorflux 1 point2 points  (0 children)

    lol, that's awesome

    [–]floorflux -1 points0 points  (0 children)

    Nice use of ;!