Skip to content

[item49] Object ํด๋ž˜์Šค์˜ checkIndex ๋ฉ”์„œ๋“œ์˜ @ForceInline์€ ๋ญ˜๊นŒย #114

@jun108059

Description

@jun108059

์ดํŽ™ํ‹ฐ๋ธŒ์ž๋ฐ” ์„œ์ ์—ย ์•„์ดํ…œ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์—์„œ ๋‹ค๋ค„์กŒ๋‹ค.

๊ฒฐ๋ก ์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ์˜ ์˜ค๋ฒ„ํ—ค๋“œ๊นŒ์ง€ ์ตœ์ ํ™”ํ•˜๋ ค ํ•˜์ง€ ๋ง๊ณ ,ย JIT ์ปดํŒŒ์ผ๋Ÿฌ์˜ ์ตœ์ ํ™”๋ฅผ ๋ฏฟ์œผ๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.

๊ทธ๋Ÿผ ์™œย @ForceInline์„ ์„ ์–ธํ–ˆ๋‚˜

์„ค๋ช…์ด ์žˆ์„๊นŒ ์‹ถ์–ด Oracle docs์™€ baeldung,ย SOF๋ฅผ ์ฐพ์•„๋ดค์ง€๋งŒ ๊ถ๊ธˆ์ฆ์„ ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ–ˆ๋‹ค.

์ถ”์ธกํ•˜๊ธฐ๋กœ๋Š” HostSpot VM ์ตœ์ ํ™” ๊ณผ์ •์— ์„ฑ๋Šฅ ์ด์ ์„ ๋ˆ„๋ฆฌ๊ธฐ ์œ„ํ•œ ์กฐ์น˜์ผ ๊ฒƒ ๊ฐ™์€๋ฐ ์ •ํ™•ํ•œ ์ด์œ ๋Š” ๋ชจ๋ฅด๊ฒ ๋‹ค.

ํ˜„์žฌ๋Š” ์–ด๋–ป๊ฒŒ ๋” ์กฐ์‚ฌํ•˜๋ฉด ์ข‹์„์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์œผ๋‹ˆ,ย stack overflow์— ์งˆ๋ฌธ์„ ๋‚จ๊ฒผ๋‹ค.

๋ˆ„๊ตฐ๊ฐ€ ์„ค๋ช…ํ•ด์ฃผ๋ฉด.. ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ ํ•œ๋‹ค.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions