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

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

[Kotlin][Java] Kotlin ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ์ด ์ž๋ฐ”๋กœ ์ปดํŒŒ์ผ ์‹œ์˜ ๋™์ž‘

์ถœ์ฒ˜

https://medium.com/%EC%8A%AC%EA%B8%B0%EB%A1%9C%EC%9A%B4-%EA%B0%9C%EB%B0%9C%EC%83%9D%ED%99%9C/java-string-%EC%97%B0%EC%82%B0-string-concat-d76a6eea75

 

javaโ€Š—โ€ŠString + ์—ฐ์‚ฐ (String Concat)

StringConcatFactory

medium.com

https://jerry92k.tistory.com/50

 

[Java] String + ์—ฐ์‚ฐ ์ตœ์ ํ™”

JDK 5 ์ด์ „ ๋ฒ„์ „์—์„œ๋Š” String + ์—ฐ์‚ฐ์‹œ, ๋งค ์—ฐ์‚ฐ๋งˆ๋‹ค String ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๋น„ํšจ์œจ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. String str1 = "a"; String str2 = str1+"b"+"c"; // => str1 ๊ฐ์ฒด ์ƒ์„ฑ, str1+"b" ๊ฐ์ฒด ์ƒ์„ฑ, str1+"b"+"c" ๊ฐ์ฒด ์ƒ์„ฑ ๊ทธ๋ž˜

jerry92k.tistory.com

https://kotlinlang.org/docs/whatsnew1520.html#string-concatenation-via-invokedynamic

 

What's new in Kotlin 1.5.20 | Kotlin

 

kotlinlang.org

https://docs.oracle.com/javase/9/docs/api/java/lang/invoke/StringConcatFactory.html

 

StringConcatFactory (Java SE 9 & JDK 9 )

Facilitates the creation of optimized String concatenation methods, that can be used to efficiently concatenate a known number of arguments of known types, possibly after type adaptation and partial evaluation of arguments. Typically used as a bootstrap me

docs.oracle.com

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

 

An Introduction to Invoke Dynamic in the JVM | Baeldung

Learn about invokedynamic and see how it can help library and language designers to implement many forms of dynamicity.

www.baeldung.com

 

 


๋ฌธ์ž์—ด ์—ฐ๊ฒฐ (String + ์—ฐ์‚ฐ)์‹œ ์ž๋ฐ”๋กœ ์–ด๋–ป๊ฒŒ ์ปดํŒŒ์ผ๋ ๊นŒ

๋ผ๋Š” ์งˆ๋ฌธ์€ ๊ฒฐ๊ตญ์—” ์ด ์งˆ๋ฌธ์ด ๋œ๋‹ค.

->  JVM์—์„œ ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ ์ฒ˜๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ํ• ๊นŒ

โžก๏ธ JDK 5 ์ด์ „

์ž๋ฐ”๋Š” String + String ์—ฐ์‚ฐ ์‹œ ์ƒˆ๋กœ์šด String ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.

 

๐Ÿ˜ฎ๋ฌธ์ œ : ๋งค๋ฒˆ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์–ด ๋น„ํšจ์œจ์ ์ด๋‹ค.

 

โžก๏ธ JDK 5 ๋ถ€ํ„ฐ JDK 8๊นŒ์ง€

๐Ÿ’ก๋ฌธ์ œ ํ•ด๊ฒฐ → String + String ์—ฐ์‚ฐ ์‹œ StringBuilder๋ฅผ ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ์ด์ „๋ณด๋‹ค ๊ฐ์ฒด ์ƒ์„ฑ ๋น„์šฉ๊ณผ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

 

์ฝ”ํ‹€๋ฆฐ ์ฝ”๋“œ์˜ ์ž๋ฐ”๋กœ์˜ ์ปดํŒŒ์ผ

1. ์ฝ”ํ‹€๋ฆฐ ์ฝ”๋“œ์—์„œ ๋ฌธ์ž์—ด ํ…œํ”Œ๋ฆฟ ์‚ฌ์šฉ

 

2. ์ฝ”ํ‹€๋ฆฐ ๋ฐ”์ดํŠธ ์ฝ”๋“œ, ์ž๋ฐ” 1.8 ๋ฒ„์ „(JVM 8)

 

๐Ÿ˜ฎ๋ฌธ์ œ : ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด StringBuilder๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค. 

 

๐Ÿ˜ฎ ๊ทธ ์™ธ StringBuilder์˜ ๋˜ ๋‹ค๋ฅธ ๋ฌธ์ œ

StringBuilder๋Š” ๊ฐ€๋ณ€ ๊ฐ์ฒด๋ผ์„œ ๊ฐ์ฒด ํ•˜๋‚˜๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ์žฌ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํšจ์œจ์ ์ธ๋ฐ, StringBuilder๋ฅผ ์ƒˆ ๊ฐ์ฒด๋กœ ์žฌํ• ๋‹นํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„๊ฐ€ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๋‹ค.

 

์˜ˆ์‹œ

StringBuilder sb = new StringBuilder();
sb.append("Hello, ");
sb.append("world!");
sb = new StringBuilder();  // ์ด์ „์˜ sb์— ๋Œ€ํ•œ ์ฐธ์กฐ๋Š” ์‚ฌ๋ผ์ง€๊ณ  ์ƒˆ๋กœ ํ• ๋‹น๋จ
sb.append("Goodbye, ");
sb.append("world!");

 

 

โžก๏ธ JDK 9 ์ดํ›„

๐Ÿ’ก๋ฌธ์ œ ํ•ด๊ฒฐ → StringConcatFactory.makeConcatWithConstants๋ฅผ ๋„์ž…ํ•ด String + String ์—ฐ์‚ฐ ์‹œ ์‚ฌ์šฉํ•œ๋‹ค.

 

๐Ÿค” StringConcatFactory.makeConcatWithConstants๋Š” ๋ฌด์—‡์ธ๊ฐ€

https://docs.oracle.com/javase/9/docs/api/java/lang/invoke/StringConcatFactory.html

 

StringConcatFactory (Java SE 9 & JDK 9 )

Facilitates the creation of optimized String concatenation methods, that can be used to efficiently concatenate a known number of arguments of known types, possibly after type adaptation and partial evaluation of arguments. Typically used as a bootstrap me

docs.oracle.com

 

StringConcatFactory.makeConcatWithConstants

๊ณต์‹ ๋ฌธ์„œ์˜ ์„ค๋ช…

Facilitates the creation of optimized String concatenation methods, that can be used to efficiently concatenate a known number of arguments of known types, possibly after type adaptation and partial evaluation of arguments. Typically used as a bootstrap method for invokedynamic call sites, to support the string concatenation feature of the Java Programming Language.
์ตœ์ ํ™”๋œ ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ ๋ฉ”์„œ๋“œ์˜ ์ƒ์„ฑ์„ ์šฉ์ดํ•˜๊ฒŒ ํ•˜๋Š” ๊ธฐ๋Šฅ์œผ๋กœ, ์•Œ๋ ค์ง„ ์œ ํ˜•์˜ ์•Œ๋ ค์ง„ ๊ฐœ์ˆ˜์˜ ์ธ์ˆ˜๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ , ์•„๋งˆ๋„ ํƒ€์ž… ๋ณ€ํ™˜ ๋ฐ ์ธ์ˆ˜์˜ ๋ถ€๋ถ„์  ํ‰๊ฐ€ ํ›„์— ์ด๋ค„์งˆ ๊ฒƒ์ด๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ invokedynamic ํ˜ธ์ถœ โ€‹โ€‹์‹œ์ ๋“ค์˜ ๋ถ€ํŠธ์ŠคํŠธ๋žฉ ๋ฉ”์„œ๋“œ๋กœ ์‚ฌ์šฉ๋˜์–ด Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์˜ ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•œ๋‹ค.

 

  • type adaption ํƒ€์ž… ๋ณ€ํ™˜ : ์ž๋ฐ”์—์„œ ๋ฌธ์ž์—ด์„ ๊ฒฐํ•ฉํ•  ๋•Œ ์ˆซ์ž๋‚˜ ๋‹ค๋ฅธ ํƒ€์ž…์ด ๋ฌธ์ž์—ด๋กœ ์ž๋™ ๋ณ€ํ™˜๋˜๋Š” ๊ฒƒ
  • partial evalution ๋ถ€๋ถ„ ํ‰๊ฐ€ : ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์ „์— ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’์„ ๊ฒŒ์‚ฐํ•ด ๋‘๋Š” ์ตœ์ ํ™” ๊ณผ์ •
  • invokedynamic : Java 7์— ๋„์ž…๋œ ๋ฐ”์ดํŠธ์ฝ”๋“œ ๋ช…๋ น์–ด. ๋Ÿฐํƒ€์ž„์— ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ๋ฐฉ์‹์„ ๊ฒฐ์ •ํ•˜๋„๋ก ํ•œ๋‹ค(=๋™์ ์œผ๋กœ ๊ฒฐ์ •ํ•˜๋„๋ก ํ•œ๋‹ค). (์ผ๋ฐ˜์ ์ธ ์ž๋ฐ” ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์€ ์ปดํŒŒ์ผ ์‹œ์ ์— ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.) 
    • ๊ด€๋ จ ๊ธ€ -> [JVM] invokedynamic ์ดํ•ดํ•˜๊ธฐ
    • invokedynamic ๊ฐ„๋žต ์„ค๋ช… : ๋™์  ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ๋ฐฉ์‹์œผ๋กœ, ์ฒ˜์Œ ํ˜ธ์ถœ ์‹œ์— bootstrap method๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ๋ฐ›์€ CallSite๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ดํ›„์— ๋ฉ”์†Œ๋“œ๊ฐ€ ๋‹ค์‹œ ์‹คํ–‰๋˜๋ฉด ์ด CallSite๋ฅผ ์ด์šฉํ•ด ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
  • bootstrap method : bootstrap์€ ์‹œ์Šคํ…œ์ด๋‚˜ ํ”„๋กœ๊ทธ๋žจ์ด ์Šค์Šค๋กœ ์ดˆ๊ธฐํ™”ํ•ด ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ์ค€๋น„ ์ƒํƒœ๋กœ ๋งŒ๋“œ๋Š” ๊ณผ์ •์œผ๋กœ, bootstrap method๋Š” ๋Ÿฐํƒ€์ž„์—์„œ ํ˜ธ์ถœํ•  ๋ฉ”์„œ๋“œ๋ฅผ ๋™์ ์œผ๋กœ ๊ฒฐ์ •ํ•˜๋Š” ์ดˆ๊ธฐํ™” ์—ญํ• ์„ ํ•œ๋‹ค.
public static CallSite makeConcatWithConstantsโ€‹(MethodHandles.Lookup lookup,
                                               String name,
                                               MethodType concatType,
                                               String recipe,
                                               Object... constants)
                                        throws StringConcatException
  • MethodHandlers.Loopup lookup: ํ˜ธ์ถœ ์‚ฌ์ดํŠธ์˜ ๋ฃฉ์—… ์ปจํ…์ŠคํŠธ. ์ž๋ฐ” ๋Ÿฐํƒ€์ž„์ด ํ˜ธ์ถœํ•  ๋ฉ”์„œ๋“œ ์ฐธ์กฐ๋ฅผ ๋™์ ์œผ๋กœ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.
    • MethodHandles : ๋ฉ”์„œ๋“œ๋ฅผ ์ฐพ๊ณ , ์กฐ์ •ํ•˜๊ณ , ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•œ ์ €์ˆ˜์ค€ ๋งค์ปค๋‹ˆ์ฆ˜
    • ๋ฃฉ์—… ์ปจํ…์ŠคํŠธ : ํŠน์ • ํด๋ž˜์Šค๋‚˜ ์Šค์ฝ”ํ”„ ๋‚ด์—์„œ ๋ฉ”์„œ๋“œ, ํ•„๋“œ, ์ƒ์„ฑ์ž์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋•๋Š”๋‹ค. 
  • String name: ํ˜ธ์ถœํ•  ๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„
  • Methodype concatType : ํ˜ธ์ถœํ•  ๋ฉ”์„œ๋“œ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž…๊ณผ ๋ฐ˜ํ™˜ ํƒ€์ž…
  • String receipe : ๊ฒฐํ•ฉํ•  ๋ฌธ์ž์—ด์˜ ํ…œํ”Œ๋ฆฟ(๋ ˆ์‹œํ”ผ). ์ƒ์ˆ˜์™€ ์ž๋ฆฌ ํ‘œ์‹œ์ž๊ฐ€ ํฌํ•จ๋œ ๊ตฌ์กฐ
    • ๋ ˆ์‹œํ”ผ : ์—ฐ๊ฒฐ๋œ ๋ฌธ์ž์—ด์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋กœ, ๋ถ€ํŠธ์ŠคํŠธ๋žฉ ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌ๋˜๋Š” ์ธ์ˆ˜์˜ ์ˆ˜๋ฅผ ์ค„์—ฌ์ค€๋‹ค. ๋ชจ๋“  ๋™์  ์ธ์ˆ˜์™€ ํ•˜๋‚˜์˜ ๋ฆฌ์‹œํ”ผ๋งŒ ์ „๋‹ฌํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  • Object[] constants : ํ…œํ”Œ๋ฆฟ์— ์‚ฝ์ž…ํ•  ๊ฐ’ ๋ชฉ๋ก
  • CallSite : ๋Ÿฐํƒ€์ž„์— ๊ฒฐํ•ฉ๋œ ๋ฌธ์ž์—ด์„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋Š” ์ฐธ์กฐ

์ฆ‰, ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ์‹œ CallSite์— ์—ฐ์‚ฐ์„ ๋ชจ์•„๋’€๋‹ค๊ฐ€ ๋งˆ์ง€๋ง‰์— ํ•œ ๋ฒˆ์— ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น๋ฐ›์•„์„œ String์„ ํ•œ ๋ฒˆ์— ๋งŒ๋“ ๋‹ค.

 

์ฝ”ํ‹€๋ฆฐ ์ฝ”๋“œ์˜ ์ž๋ฐ”๋กœ์˜ ์ปดํŒŒ์ผ

1. ์ฝ”ํ‹€๋ฆฐ ์ฝ”๋“œ์—์„œ ๋ฌธ์ž์—ด ํ…œํ”Œ๋ฆฟ ์‚ฌ์šฉ

 

2. ์ฝ”ํ‹€๋ฆฐ ๋ฐ”์ดํŠธ ์ฝ”๋“œ, ์ž๋ฐ” 9 ๋ฒ„์ „(JVM 9)

 

makeConcatWithContstants๊ฐ€ StringBuilder๋ณด๋‹ค ํšจ์œจ์ ์ธ ์ด์œ 

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

 

 

 

๐Ÿ˜€ ์ฝ”ํ‹€๋ฆฐ 1.5.20 ๋ฒ„์ „์—์„œ JVM์ด 9 ๋ฒ„์ „ ์ด์ƒ์ด๋ฉด, ์ปดํŒŒ์ผ StringconcatFactory.makeConcatWithConstants()๋ฅผ ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค. 

https://kotlinlang.org/docs/whatsnew1520.html#string-concatenation-via-invokedynamic

 

What's new in Kotlin 1.5.20 | Kotlin

 

kotlinlang.org