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

PoseService 랜덤 포즈 추천 로직 수정 #12

Merged
merged 2 commits into from
Jan 10, 2024

Conversation

Yun-YeoJun
Copy link
Collaborator

기존의 랜덤 포즈 조회 로직을 수정하였습니다.

  • 기존 로직에서는 randomIndexList에 randomIndex가 있는지 .contains로 매번 검사를 해주었는데, 이는 O(n)의 시간 복잡도를 갖기 때문에 비효율적입니다.
  • 대신 HashSet을 활용하여 중복 체크 시간 복잡도를 O(1)로 개선하였습니다. (HashSet.contains()의 시간 복잡도는 O(1))

  • 기존 로직에서는 randomIndexList을 초기화 할 때 크기를 지정하지 않았기 때문에, add()를 수행할 경우 크기를 늘리기 위해 내부적으로 리스트 전체를 복사하여 새로운 리스트를 만들 가능성이 있습니다.
  • 대신 randomIndexList를 초기화 할 때 크기를 지정하여 이러한 가능성을 제거하였습니다.

@Yun-YeoJun
Copy link
Collaborator Author

@cosmos-1885 코드 수정 완료했습니다!

두번째 커밋 개선점

  • 랜덤 포즈 조회를 요청할 때마다 DB를 조회하기 때문에 요청과 응답 사이의 시간이 길어졌던 문제를 해결했습니다.
    • 맨 처음에 PoseService 빈을 등록할 때 DB를 한 번 조회하고, DB에 새로운 포즈가 추가되었을 때 이를 반영하기 위해 랜덤 포즈 조회를 10번 수행하면 DB에서 포즈 목록을 다시 읽어오도록 했습니다.
  • 다시 확인해보니 첫번째 커밋에서는 포즈 중복 방지 기능이 제대로 작동하지 않았습니다. 관련 로직을 수정하여 제대로 포즈 중복 방지 기능이 작동함을 확인했습니다.

두번쨰 커밋 문제점

  • 처음에 PoseService 빈을 IoC 컨테이너에 등록할 때 DB에서 데이터를 읽어오는 작업을 수행합니다. peopleCount가 1부터 최댓값(5)인 경우까지 전부 조회합니다. 이러한 이유로 인해 초기 실행이 현재 약 40초 정도가 소요됩니다. JPA의 n+1 문제 같은데, 이를 어떻게 해결하면 좋을까요?

@Yun-YeoJun Yun-YeoJun merged commit 3cab82e into whatever-mentoring:develop Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant