브라이언 고츠(Brian Goetz)는 선 마이크로 시스템즈(Sun)에 근무하는 자바 에반젤리스트(Java evangelist)이고, 자바에 많은 공헌을 했지만, 특별히 java.util.concurrent 패키지에 많은 공헌을 했습니다. 아래 인터뷰는 2006년에 한 내용으로, 빠르고 성능좋은 자바 애플리케이션을 작성하는 기본에 대한 내용입니다.
In the interview, Goetz gave this advice on how to write faster-performing Java code:
Often, the way to write fast code in Java applications is to write dumb code?code that is straightforward, clean, and follows the most obvious object-oriented principles.
This has to do with the nature of dynamic compilers, which are big pattern-matching engines. Because compilers are written by humans who have schedules and time budgets, the compiler developers focus their efforts on the most common code patterns, because that’s where they get the most leverage. So if you write code using straightforward object-oriented principles, you’ll get better compiler optimization than if you write gnarly, hacked-up, bit-banging code that looks really clever but that the compiler can’t optimize effectively.
So clean, dumb code often runs faster than really clever code, contrary to what developing in C might have taught us. In C, clever source code turns into the expected idiom at the machine-code level, but it doesn’t work that way in Java applications…
If I could wave a magic wand and send out one message about Java programming, it would be this: Trust the JVM. It’s smarter than you think. Stop trying to outwit or outsmart it. Tell it what you want, and it will do its damnedest to make your application run as fast as it can.
이 인터뷰를 간단하게 정리하면, “코드를 어렵게 또는 최적화해서 쓰지 말고, 많이 사용하는 패턴으로 작성하면 컴파일러가 최적화해서 비교적 성능이 좋은 자바 애플리케이션을 개발할 수 있다”는 것이다. 즉, 어렵게 코드를 개발하지 말고 쉽게 쉽게 가독성 좋게 코드를 쓰자.