-
Notifications
You must be signed in to change notification settings - Fork 4
Description
์ดํํฐ๋ธ์๋ฐ ์์ ์ย ์์ดํ 49. ๋งค๊ฐ๋ณ์๊ฐ ์ ํจํ์ง ๊ฒ์ฌํ๋ผย ์คํฐ๋ ์งํ ์ค์ Java9์์ ์ถ๊ฐ๋ Objects ํด๋์ค์ checkIndex ๋ฉ์๋๋ฅผ ํ์ธํด๋ณด๋ค๊ฐ ์ฒ์๋ณด๋ ์ ๋ํ ์ด์ ์ด ์์ด ๊ถ๊ธ์ฆ์ ์ฐพ์๋ณด์๋ค.
// Object class (In openjdk-18.0.2.1)
@ForceInline
public static int checkIndex(int index, int length) {
return Preconditions.checkIndex(index, length, null);
}์ฐธ๊ณ ๋ก java ๋ฒ์ ์ย openjdk-18.0.2.1์ด๋ค.
javadoc ์ฝ์ด๋ณด๊ธฐ
A method or constructor may be annotated asย "force inline"ย if the standard inlining metrics are to be ignored when the HotSpot VM inlines the method or constructor.This annotation must be used sparingly.ย It is useful when the only reasonable alternative is to bind the name of a specific method or constructor into the HotSpot VM for special handling by the inlining policy.This annotation must not be relied on as an alternative to avoid tuning the VM's inlining policy.In a few cases,ย it may act as a temporary workaround until the profiling and inlining performed by the HotSpot VM is sufficiently improved.
- HotSpot VM์ด ๋ฉ์๋ ๋๋ ์์ฑ์๋ฅผ ์ธ๋ผ์ธํํ ๋ ํ์ค ์ธ๋ผ์ธ ๋ฉํธ๋ฆญ์ ๋ฌด์ํ ๊ฒฝ์ฐ ๋ฉ์๋ ๋๋ ์์ฑ์์ "์ธ๋ผ์ธํ ๊ฐ์ "๋ผ๋ ์ฃผ์์ ๋ฌ ์ ์๋ค.
- ์ต๋ํ ์ฌ์ฉํ์ง ๋ง์๋ผ.
- Inline ์ ์ฑ ์ ์ํ ํน๋ณํ ์ฒ๋ฆฌ๋ฅผ ์ํด HotSpot VM์ ๋ฐ์ธ๋ฉํ๋ ๊ฒ์ด ์ ์ผํ ๋ฐฉ๋ฒ์ผ ๋ ์ฌ์ฉํด๋ผ
- VM์ inlining ์ ์ฑ ์ ํผํ๊ธฐ ์ํ ๋์์ผ๋ก ์ฌ์ฉํ๋ฉด ์๋๋ค.
- ๋๋ฌธ ๊ฒฝ์ฐ์, HotSpot VM์ inlining์ด ๊ฐ์ ๋ ๋๊น์ง ์์ ํด๊ฒฐ ๋ฐฉ๋ฒ์ผ๋ก ์ฌ์ฉํ ์ ์๋ค.
Inline์ด๋
- ์ฝ๋ ๋ด๋ถ์ ๋ค์ด๊ฐ ํจ์
- ์ฑ๋ฅ๋๋ฌธ์ ๋ณดํต C++์์ ๋ง์ด ๋ค๋ค์๋ค.
- ํจ์๊ฐ ํธ์ถ๋ ๋๋ง๋ค ๋ฐ์ํ๋ ์ค๋ฒํค๋๋ฅผ ์ค์ด๊ธฐ ์ํดย inlineย ํค์๋๋ฅผ ์ฌ์ฉํ๋ค.
- ์ต๊ทผ C++ ์ปดํ์ผ๋ฌ๋ ํจ์๋ฅผ ์ ์ ํ๊ฒ inline ํด์ฃผ๊ธฐ ๋๋ฌธ์ ํค์๋ ๋ถํ์
void PrintHello() {
cout << "Hello, World!" << endl;
}
int main() {
PrintHello();
PrintHello();
return 0;
}
์ ์ฝ๋๋ฅผ inline ํ๋ฉด ์๋์ ๊ฐ์์ง๋ค.
int main() {
cout << "Hello, World!" << endl;
cout << "Hello, World!" << endl;
return 0;
}Java์์์ inline
์ฐพ์๋ณด๋ inline์ ํ์์ฑ๊ณผ ๋์๋ฐฉ์์ ๋ํ ๋ ผ์๋ ์ฌ๋ฌ์ฐจ๋ก stack overlow์์ ๋ค๋ค์ก๋ค.
- https://stackoverflow.com/questions/1159087/inlining-in-java
- https://stackoverflow.com/questions/7744210/force-inline-in-java
๊ฒฐ๋ก ์ ํจ์ ํธ์ถ์ ์ค๋ฒํค๋๊น์ง ์ต์ ํํ๋ ค ํ์ง ๋ง๊ณ ,ย JIT ์ปดํ์ผ๋ฌ์ ์ต์ ํ๋ฅผ ๋ฏฟ์ผ๋ฉด ๋ ๊ฒ ๊ฐ๋ค.
๊ทธ๋ผ ์ย @ForceInline์ ์ ์ธํ๋
์ค๋ช ์ด ์์๊น ์ถ์ด Oracle docs์ baeldung,ย SOF๋ฅผ ์ฐพ์๋ดค์ง๋ง ๊ถ๊ธ์ฆ์ ํด๊ฒฐํ์ง ๋ชปํ๋ค.
์ถ์ธกํ๊ธฐ๋ก๋ HostSpot VM ์ต์ ํ ๊ณผ์ ์ ์ฑ๋ฅ ์ด์ ์ ๋๋ฆฌ๊ธฐ ์ํ ์กฐ์น์ผ ๊ฒ ๊ฐ์๋ฐ ์ ํํ ์ด์ ๋ ๋ชจ๋ฅด๊ฒ ๋ค.
ํ์ฌ๋ ์ด๋ป๊ฒ ๋ ์กฐ์ฌํ๋ฉด ์ข์์ง ์ ๋ชจ๋ฅด๊ฒ ์ผ๋,ย stack overflow์ ์ง๋ฌธ์ ๋จ๊ฒผ๋ค.
๋๊ตฐ๊ฐ ์ค๋ช ํด์ฃผ๋ฉด.. ์ ๋ฆฌํด๋ณด๋ ค๊ณ ํ๋ค.