Kotlin Collections Immutable: ImmutableList vs PersistentList 이해하기

hbj0209·2025년 12월 10일

Kotlin

목록 보기
2/2

ImmutableList vs PersistentList 이해하기

Kotlin의 kotlinx.collections.immutable는 불변 컬렉션을 효율적으로 다루기 위한 전용 컬렉션 구현체를 제공합니다. 특히 ImmutableList와 PersistentList는 API 설계와 내부 구현에서 각각 다른 목적을 갖습니다.


TL;DR (요약)

// PersistentList로 생성하고
val list = persistentListOf(1, 2, 3)

// ImmutableList 타입으로 노출
fun getItems(): ImmutableList<Int> = list
  • ImmutableList: 읽기 전용 불변 리스트 인터페이스 (외부 API용)
  • PersistentList: 수정 가능하지만 불변성을 유지하는 인터페이스 (내부 구현용)
  • 패턴: PersistentList로 생성 → ImmutableList 타입으로 노출

타입 계층 구조 이해하기

Kotlin Collection
       ↓
	List<E>
       ↓
ImmutableList<E>  ← 읽기 전용 계약
       ↓
PersistentList<E>  ← 불변성을 유지하며 수정 가능
       ↓
PersistentVector<E>  ← 실제 구현체 (internal)

ImmutableList: 순수한 읽기 전용

  • 인터페이스: 불변 리스트의 계약을 정의
    public interface ImmutableList<out E> : List<E>, ImmutableCollection<E>
  • 읽기 전용 작업만 제공
  • 파라미터나 프로퍼티 타입으로 사용

PersistentList: 불변성을 유지하는 수정 가능 리스트

  • 인터페이스: ImmutableList를 확장한 인터페이스
  • 수정 메서드를 제공하지만 원본을 변경하지 않고 새 인스턴스 반환
  • add(), removeAt(), set() 등의 메서드 제공
  public interface PersistentList<out E> : ImmutableList<E> {
      fun add(element: @UnsafeVariance E): PersistentList<E>
      fun add(index: Int, element: @UnsafeVariance E): PersistentList<E>
      fun removeAt(index: Int): PersistentList<E>
      fun set(index: Int, element: @UnsafeVariance E): PersistentList<E>
          // ...
  }

Persistent Data Structure란?

일반적인 불변 컬렉션과의 차이점

// 일반 불변 리스트 (매번 전체 복사)
val list1 = listOf(1, 2, 3, 4, 5)  // O(n) 메모리
val list2 = list1 + 6              // O(n) 복사
val list3 = list2 + 7              // O(n) 복사
// 매번 전체 리스트를 복사하므로 비효율적

// Persistent 리스트 (구조적 공유)
var plist = persistentListOf(1, 2, 3, 4, 5)  // O(n) 메모리
plist = plist.add(6)  // O(log n) - 변경된 부분만 새로 생성
plist = plist.add(7)  // O(log n) - 대부분의 구조 공유
// 이전 버전들과 대부분의 내부 구조를 공유하여 효율적

Persistent Data Structure는 내부적으로 트리 구조를 사용합니다.

  • 변경된 경로(path)만 새로 생성
  • 나머지는 기존 구조를 재사용
  • 시간 복잡도: O(log n)
  • 공간 효율적

언제, 어떻게 써야 하나?

상황권장 타입
외부에 상태 노출 (ViewModel, Domain, API)ImmutableList
내부에서 변형 가능한 불변 컬렉션 필요PersistentList
매번 전체 복사 비용을 피하고 싶을 때PersistentList

참고자료

https://github.com/Kotlin/kotlinx.collections.immutable

profile
안녕하세요!

0개의 댓글