Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] #520 - DateFormatter() 인스턴스 메모리 최적화 #528

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from

Conversation

yungu0010
Copy link
Contributor

@yungu0010 yungu0010 commented Mar 29, 2025

🌴 PR 요약

🌱 작업한 브랜치

🌱 PR Point

  • 세진님께서 남겨주신 코드리뷰를 참고하여 DateFormatter() 인스턴스 초기화 비용과 함수 실행 시간에 대한 테스트를 진행했습니다. 테스트 기록

테스트 결과를 요약하면 아래와 같습니다!

인스턴스 생성시간
DateFormatter 1ms
NSString 0.2ms
UIView 20ms
Date 0.5ms
DateFormatter 인스턴스를 매번 초기화 DateFormatter 인스턴스 재활용
string(from: Date) 31ms 3ms
date(from: String) 51ms 2ms
DateFormatter 인스턴스를 매번 초기화 DateFormatter 인스턴스 재활용
calendar 89ms 3ms
timezone 78ms 1ms
locale 34ms 2ms
dateFormat 65ms 2ms
dateStyle 119ms 8ms

테스트를 통해 DateFormatter초기화 비용은 다른 객체와 큰 차이가 나지 않지프로퍼티를 변경하거나 함수를 사용할 때 비용이 크다는 것을 알 수 있었습니다. 따라서 싱글톤으로 객체를 관리하는 DateFormatManager 클래스를 생성하여 공통 인스턴스를 활용하도록 리팩토링을 진행했습니다.

리팩토링 이후 전, 후의 DateFormatter 인스턴스 수를 memory graph로 확인해보았습니다.

최적화 여부 스크린샷
최적화 전 develop
최적화 후 fix
NetworkLogger 최적화 전 image
NetworkLogger 최적화 후 image

최적화 후 인스턴스 수가 3개로 줄어들고 NetworkLoggerPlugin이 더 이상 DateFormatter 인스턴스를 참조하지 않는 것을 확인할 수 있었습니다.

3개의 인스턴스 중 불필요한 객체는 없는지 마지막으로 확인해보았는데요,

인스턴스 스크린샷
DateFormatManager dateFormat
Sentry sentry
앰플리튜드 amplitude

memory graph로 확인했을 때3개 인스턴스는 각각 DateFormatManager, Sentry, Amplitude객체에서 사용되는 것으로 확인되었습니다.

+) Sentry 제거와 crashlytics 부착 작업 일정도 논의해보아요!

📌 참고 사항

  • NetworkLogger를 제외한 모든 곳에서 ko_KR을 사용하고 있어 NetworkLogger도 한국시간대로 통일했습니다.
  • DateFormatManager가 사용된 부분은 출석뷰, 홈화면, 솝마디, 알림(알림이 얼마나 되었는지, 솝마디를 확인할 수 있는 날인지)입니다.

📮 관련 이슈

@yungu0010 yungu0010 added Fix 문제 해결, 코드 수정 Refactor 전면 수정 labels Mar 29, 2025
@yungu0010 yungu0010 self-assigned this Mar 29, 2025
Copy link

height bot commented Mar 29, 2025

Link Height tasks by mentioning a task ID in the pull request title or commit messages, or description and comments with the keyword link (e.g. "Link T-123").

💡Tip: You can also use "Close T-X" to automatically close a task when the pull request is merged.

Copy link
Contributor

@meltsplit meltsplit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

실험군들 비교해가며 인스턴스 메모리 최적화 잘 봤습니다 :)
저도 실제 성능 테스트를 하는 습관을 들여야겠어요 호호.

코드리뷰에선 날짜 변환의 책임 관련해서 리뷰 남겼습니다.
의견 남겨주세요!

Comment on lines 128 to 131
extension CalendarCardCVC {
func configureCell(model: HomePresentationModel.RecentSchedule,
userType: UserType) {
// TODO: 서버 - 날짜 포맷 변경 후 반영
self.dateLabel.text = setDateFormat(date: model.date, to: "MM.dd")
self.dateLabel.text = DateFormatManager.shared.transformDateFormat(model.date, from: .monthDayWIthDash, to: .monthDayWithDot)
Copy link
Contributor

@meltsplit meltsplit Mar 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

서버에서 받아온 날짜 관련 String을
뷰계층에서 from -> to로 바꿔주는 것이군요 :)

서버와 약속한 Date format으로 Date를 변환하는 역할은 누구에게 책임이 있다고 생각하시나요?
저는 해당 책임은 데이터 모듈 혹은 네트워크 모듈이 지녀야 한다고 생각해요.
서버의 문자열 format이 바뀌었을 때 뷰계층 코드를 변경하는 것보단 네트워크쪽 코드를 변경하는 것이 자연스럽다고 느껴지기 때문이에요

Date값이 Data계층에서 Domain계층으로 넘어갈 때 String 값을 Date값으로 변경하고,
Presentation 계층에선 Date -> String으로 변환하는 방식은 어떨까요?

Domain에는 가장 순수한 Date 타입만 존재하면 좋을 것 같아요

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다시 생각해보니 해당 PR은 메모리 성능 최적화가 중점적이라, PR에서 작업하지 않은 내용을 리뷰한 것 같네요 ㅎㅎ;
그래도 같이 고민해보면 좋을 것 같습니다 .. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리뷰 감사합니다 !! 기존 코드 수정에만 집중하다 보니 각 레이어의 역할 분리에 대해 깊이 있게 고민하지 못했던 것 같아요.

석우님 말씀처럼 서버에서 받아온 날짜(String)값Data -> Domain 단계에서 Date 타입으로로 변환하면 UI 상황에 맞게 다양한 포맷으로 변경할 수 있어 더 유연한 구조가 될 것 같네요 ! 리뷰에 함께 반영하도록 하겠습니다👍👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

피드백 수용해주셔서 감사함다 :)

현 구조에선 toDomain()에서 String을 Date로 변환하는 것이 가장 편할 것 같네요.

이외에도 서버의 requestBody의 json을 Decodable 구조체로 디코딩할 때 JSONDecoder의 dateStrategyFormat값을 통해 처음부터 해당 밸류값을 Date 타입으로도 받아오는 방법도 있습니다~!

Copy link
Contributor

@dlwogus0128 dlwogus0128 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

굿굿

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fix 문제 해결, 코드 수정 Refactor 전면 수정 size/L 윤서🍉
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Refactor] DateFormatter 중복 생성 최적화
3 participants