More Related Content More from Daesung Park (9) TechTalk - Object Interning1. 여느 때처럼 열심히 개발하는 나
이미지 출처 : Instagram (@waterglasstoon)
3. 한 블로그에서 이상한
문제를 보게 된다.
• 변수 a와 b의 메모리 주소는 같을까요?
=> 같습니다.
// 0x7c1f277370 0x7c1f277370
5. 버전에 따라 결과가 달랐다.
• Python 2.x => 같음
• Python 3.6 => 같음
• Python 3.7 => 다름
• Python 3.8 => 같음
• ...
• Python 3.10 => 같음
9. 동일한 값을 가진 상수가 존재할 때
127.0.0.1
127.0.0.1
127.0.0.1
Memory
10. 재사용 할 수 없을까?
127.0.0.1
Memory
괜찮은데요?
11. 그럼 Object 를 재사용하면..?
•Immutable 객체에 적용하면 메모리를 절약할 수 있겠네!
•String Literal 도 재사용 하면 좋을 것 같은데?
JAVA
Python
12. Interning 적용 범위
Java
new String() 으로 생성시 지원 X
Double 지원 X
Integer -128 ~ 127 까지 지원
Python
[a-zA-Z0-9_] 해당되는 char 지원
Float 지원 X
정수 -5 ~ 256 까지 지원
13. Interning 을 개발자가 활용하면 안됩니다.
실수하기 쉽습니다.
Integer -128 ~ 127 까지 지원
그래서 Java는 .equals() 을 씁니다.
16. Python Compile Time에 최적화가 일어나기 때문
• Python은 Interpreter 언어인데요?
• Interpreter가 해석하는 값은 Compiled Bytecode.
확장자 .pyc
=> REPL과 .py로 된 코드를 실행할 때의 결과가 다를 수 있음
Python Interactive Shell
17. 이상했던 코드를 다시 봅시다.
[a-zA-Z0-9_] 해당되는 char 지원
Compile Time에 Interning!
19. 네 Python 3.7 의 버그입니다.
https://bugs.python.org/issue34100
이 버그는 3.7.0 에서 발견되어 3.8에서나 고쳐집니다.
20. 결론
• Interning 최적화 기법
• Interning 의 적용 범위
• Interning 주의사항
• Python Compiler Optimization Interning
• Python 3.7 Bug
21. 참고 자료
• https://nephtyws.github.io/python/interning/
• https://frhyme.github.io/java/java_Object_Interning/
• http://pythonstudy.xyz/python/article/512-
%ED%8C%8C%EC%9D%B4%EC%8D%AC-Object-Interning
• https://nedbatchelder.com/blog/201803/is_python_interpreted
_or_compiled_yes.html
• https://pythonprogramminglanguage.com/repl/
• https://gbsb.tistory.com/255
• https://dololak.tistory.com/699