(Reactive)QueryDslImpl is an object created for every query creation and is used for select, update, and delete.
If you look inside this class, you can see the code that initializes mutableList(Map)Of at the time of object creation for used or unused clauses.
private var joins: MutableList<JoinSpec<*>> = mutableListOf()
private var wheres: MutableList<PredicateSpec> = mutableListOf()
private var groupBys: MutableList<ExpressionSpec<*>> = mutableListOf()
private var havings: MutableList<PredicateSpec> = mutableListOf()
private var orderBys: MutableList<OrderSpec> = mutableListOf()
private var sqlHints: MutableList<String> = mutableListOf()
private var jpaHints: MutableMap<String, Any> = mutableMapOf()
private var params: MutableMap<ColumnSpec<*>, Any?> = mutableMapOf()
Even though one very simple params is used only in update, for example, and the rest are unused, params are forcibly created as an empty MutableMap whenever select and delete queries are used.
In the case of mutableListOf , ArrayList is used internally, and this code has a default capacity of 10 (mutableMapOf is 16). In other words, you can see that you are wasting space by pre-creating an array that you may not even be using.
To solve this problem, we must either declare and use Lazy directly or implement by lazy { mutableListOf() } to create objects only when necessary as lazy.
However, createCriteriaQuerySpec, createCriteriaUpdateQuerySpec, createCriteriaDeleteQuerySpec, createSubquerySpec
When creating a spec through these four methods, note that all necessary clauses are always obtained, so that they are created only when necessary.
-- korean
(Reactive)QueryDslImpl 는 매 쿼리 생성마다 생성되는 객체이며 select, update, delete 모두에 사용 됩니다.
이 클래스의 내부를 살펴보면 사용 되거나 혹은 사용되지 않을 clause 들을 위해 mutableList(Map)Of 를 객체 생성 시점에 초기화 해두는 코드를 확인할 수 있습니다.
private var joins: MutableList<JoinSpec<*>> = mutableListOf()
private var wheres: MutableList<PredicateSpec> = mutableListOf()
private var groupBys: MutableList<ExpressionSpec<*>> = mutableListOf()
private var havings: MutableList<PredicateSpec> = mutableListOf()
private var orderBys: MutableList<OrderSpec> = mutableListOf()
private var sqlHints: MutableList<String> = mutableListOf()
private var jpaHints: MutableMap<String, Any> = mutableMapOf()
private var params: MutableMap<ColumnSpec<*>, Any?> = mutableMapOf()
이중 아주 간단한 params 하나를 예를 들면 update 에서만 사용되고 나머지는 사용되지 않음에도 select, delete 쿼리를 사용할때마다 params는 빈 MutableMap 으로 강제적으로 생성됩니다.
mutableListOf 의 경우 내부적으로 ArrayList 를 사용하고 있고 이 코드는 기본 capacity 가 10(mutableMapOf 는 16)입니다. 즉, 사용하지도 않을수도 있는 배열을 미리 생성해서 공간을 낭비하고 있는 것을 알 수 있습니다.
이러한 문제를 해결하기 위해 우리는 Lazy 를 직접 선언해서 사용하거나 by lazy { mutableListOf() } 를 통해 lazy 로 필요할때만 객체를 생성하게 구현하여야 합니다.
단, createCriteriaQuerySpec, createCriteriaUpdateQuerySpec, createCriteriaDeleteQuerySpec, createSubquerySpec
이 네개의 메소드를 통해 spec 을 생성할때 필요한 모든 clause 들을 항상 get 한다는 것에 주의하여 필요할때만 생성되도록 해야 합니다.