๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[JVM] invokedynamic ์ดํ•ดํ•˜๊ธฐ

์ถœ์ฒ˜

https://stackoverflow.com/a/62354389

 

What is a bootstrap method?

I have been reading this presentation about java8 lambdas implementation http://fr.slideshare.net/czechscala/java-8-under-the-hood LambdaMetaFactory contains bootstrap methods for converting la...

stackoverflow.com

https://blogs.oracle.com/javamagazine/post/understanding-java-method-invocation-with-invokedynamic

https://docs.deistercloud.com/content/Technology.50/Java/invokedynamic.xml?embedded=true

 

Invokedynamic

In a dynamic language, type-checking typically occurs at runtime. Developers must pass appropriate types or risk runtime failures. It's often the case that java.lang.Object is the most accurate type for a method argument. This situation complicates type ch

docs.deistercloud.com

https://www.baeldung.com/java-invoke-dynamic

 

 


๋“ฑ์žฅ ๋ฐฐ๊ฒฝ : ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋‹ค.

Java 7 ์ด์ „์—๋Š” ๋„ค ๊ฐ€์ง€ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ๋ฐฉ์‹๋งŒ JVM์— ์กด์žฌํ–ˆ๋‹ค.

 

1. invokevirtual : ์ผ๋ฐ˜ ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

2. invokestatic : ์ •์  ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

3. invokeinterface : ์ธํ„ฐํŽ˜์ด์Šค ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

4. invokespecial : ์ƒ์„ฑ์ž๋‚˜ private ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

 

์ด ๋ฐฉ์‹๋“ค์€ ์ด๋ฏธ ์ฃผ์–ด์ง„ ๋ฐฉ์‹์œผ๋กœ ๊ฐ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ์ด ์ด๋ค„์ง„๋‹ค. ์ฆ‰, ์ •์  ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ๋ฐฉ์‹์œผ๋กœ๋งŒ ์ด๋ค„์ง„๋‹ค. ๋” ์œ ์—ฐํ•œ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ์„ ์œ„ํ•ด ๊ธฐ์กด์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•๋“ค์„ ์ด์šฉํ–ˆ๋‹ค

 

1. ์ปดํŒŒ์ผ ํƒ€์ž„์— ์ฝ”๋“œ ์ƒ์„ฑ(code generation)์„ ํ–ˆ๋‹ค. ์Šค์นผ๋ผ๋‚˜ ์ฝ”ํ‹€๋ฆฐ ๊ฐ™์€ ์–ธ์–ด๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ, ๋Ÿฐํƒ€์ž„ ์‹œ ์„ฑ๋Šฅ์ด ํšจ์œจ์ ์ด์ง€๋งŒ, ์‹œ์ž‘ ์†๋„๊ฐ€ ๋Š๋ ค์งˆ ์ˆ˜ ์žˆ๊ณ  ์ฝ”๋“œ๊ฐ€ ์ทจ์•ฝํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

2. ๋Ÿฐํƒ€์ž„์— ๋ฆฌํ”Œ๋ ‰์…˜์„ ์‚ฌ์šฉํ•ด ๋™์  ํ˜ธ์ถœ์„ ๊ตฌํ˜„ํ•œ๋‹ค. JRuby์™€ ๊ฐ™์€ JVM ๊ธฐ๋ฐ˜ ๋™์  ์–ธ์–ด์—์„œ ์‚ฌ์šฉ๋œ๋‹ค. ๋ฆฌํ”Œ๋ ‰์…˜ ๊ธฐ๋ฐ˜์ด๋ผ์„œ ์„ฑ๋Šฅ์ด ๋‚ฎ๊ณ  ๋น„ํšจ์œจ์ ์ด๋‹ค.

 

invokedynamic์œผ๋กœ ๋™์  ์–ธ์–ด์˜ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•˜๊ณ , ๋™์  ์–ธ์–ด์™€ ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๋Š” JVM ์–ธ์–ด์„ ๋” ์‰ฝ๊ฒŒ ์ง€์›ํ•˜, ์ปดํŒŒ์ผ ํƒ€์ž„ ๋ฐ ๋Ÿฐํƒ€์ž„ ๋ชจ๋‘์—์„œ ๋” ์œ ์—ฐํ•˜๊ณ  ์„ฑ๋Šฅ์ด ๋›ฐ์–ด๋‚œ ๋™์  ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•ด์กŒ๋‹ค.

 

invokedynamic์€ ๋ฌด์—‡์ธ๊ฐ€

JVM ์•ˆ์—์„œ ์ž„์˜์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ๋™์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๋Š” ๋ฐ”์ดํŠธ์ฝ”๋“œ ์—ฐ์‚ฐ์ด๋‹ค. 

 

invokedynamic์˜ ๋™์ž‘

1. ์ฒ˜์Œ์œผ๋กœ invokedynamic์„ ํ˜ธ์ถœํ•œ๋‹ค.

2. ๋ถ€ํŠธ์ŠคํŠธ๋žฉ ๋ฉ”์„œ๋“œ(BSM)๊ฐ€ ์ฒ˜์Œ ํ˜ธ์ถœ๋œ๋‹ค. BSM์ด ์‹คํ–‰๋˜์–ด ํ˜ธ์ถœ ๊ณผ์ •์˜ ์ดˆ๊ธฐ ์„ค์ •์„ ๋‹ด๋‹นํ•œ๋‹ค. ํ˜ธ์ถœํ•  ๋ฉ”์„œ๋“œ ํ•ธ๋“ค์„ ์„ค์ •ํ•œ๋‹ค. 

3. BSM์ด ์™„๋ฃŒ๋˜๋ฉด CallSite ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. CallSite๋Š” MethodHandle(JVM์— ์‹คํ–‰ํ•˜๋Š” ์‹ค์ œ ๋กœ์ง์˜ ํฌ์ธํ„ฐ)๊ณผ ๋ฐ˜ํ™˜๋œ CallSite์˜ ์œ ํšจ์„ฑ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. CallSite๋Š” ์บ์‹œ์— ์ €์žฅ๋œ๋‹ค.

4. ์ด์ œ๋Š”  invokedynamic์ด ํ˜ธ์ถœ๋˜๋ฉด ์บ์‹œ๋œ CallSite๋กœ ๋ฉ”์†Œ๋“œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

2. ConstantCallSite : BSM์„ ํ†ตํ•ด ์„ค์ •๋œ ํ˜ธ์ถœ ์ง€์ ์€ ConstantCallSite๋กœ ์•ˆ์ •ํ™”๋˜๋ฉฐ, ์ดํ›„์—๋Š” ์ง์ ‘ ํ˜ธ์ถœ๋˜์–ด JIT ์ปดํŒŒ์ผ๋Ÿฌ์™€ ์นœํ™”์ ์ธ ์„ฑ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

 

๋ฉ”์„œ๋“œ ํ•ธ๋“ค(MethodHandle) API

MethoadHandle๊ณผ MethodType API๋กœ ๋ฉ”์„œ๋“œ ์„œ๋ช… ๋ฐ ํ˜ธ์ถœ์„ ๋” ์•ˆ์ „ํ•˜๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋”ฐ. MethodHandle์€ ๋Ÿฐํƒ€์ž„์— ๋ฉ”์„œ๋“œ ํ•ธ๋“ค์„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, MethodType์€ ๋ฉ”์„œ๋“œ์˜ ๋ฐ˜ํ™˜ํ˜•๊ณผ ์ธ์žํ˜•์„ ํ‘œํ˜„ํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค. ๊ธฐ์กด ๋ฆฌํ”Œ๋ ‰์…˜ API ๋ณด๋‹ค ๊ฐ„๋‹จํ•˜๊ณ  ์•ˆ์ •ํ•˜๊ฒŒ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.