Skip to content

Commit

Permalink
Uses a normal HashMap for initial state
Browse files Browse the repository at this point in the history
Summary: Uses a normal HashMap for initial state. All reads and write to the map are under a lock  a sync'd map is not required.

Reviewed By: kingsleyadio

Differential Revision: D63755164

fbshipit-source-id: 003aa26d3383422c40fd83a00606cd104798af5f
  • Loading branch information
adityasharat authored and facebook-github-bot committed Oct 3, 2024
1 parent 57f6994 commit c365c27
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions litho-core/src/main/java/com/facebook/litho/InitialState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import com.facebook.litho.ComponentsSystrace.beginSection
import com.facebook.litho.ComponentsSystrace.endSection
import com.facebook.litho.ComponentsSystrace.isTracing
import com.facebook.litho.state.ComponentState
import java.util.Collections
import java.util.HashMap
import java.util.HashSet
import javax.annotation.concurrent.GuardedBy
Expand All @@ -41,9 +40,11 @@ class InitialState {
// All the initial states that have been created and can not yet be released. This is a concurrent
// map as we can access it from multiple threads. The safety is given by the fact that we will
// only get and set for a key while holding a lock for that specific key.
@JvmField
@GuardedBy("this")
@VisibleForTesting
val states = Collections.synchronizedMap(HashMap<String, ComponentState<out StateContainer>>())
private val _states = hashMapOf<String, ComponentState<out StateContainer>>()
val states: Map<String, ComponentState<out StateContainer>>
get() = _states

@GuardedBy("this") private val createInitialStateLocks: MutableMap<String, Any> = HashMap()

Expand Down Expand Up @@ -71,7 +72,7 @@ class InitialState {

return synchronized(stateLock) {
val state =
states.getOrPut(key) {
_states.getOrPut(key) {
createInitialState(context = scopedContext, component = component)
}
state
Expand Down Expand Up @@ -140,7 +141,7 @@ class InitialState {
("Unexpected useState hook sequence encountered: $hookIndex (states size: ${hookStates.states.size}). This usually indicates that the useState hook is being called from within a conditional, loop, or after an early-exit condition. See https://fblitho.com/docs/mainconcepts/hooks-intro/#rules-for-hooks for more information on the Rules of Hooks.")
}
val newState = state?.copy(value = hookStates) ?: ComponentState(value = hookStates)
this.states[key] = newState
this._states[key] = newState
newState
}
}
Expand All @@ -157,7 +158,7 @@ class InitialState {
// This is safe as we have a guarantee that by this point there is no layout happening
// and therefore we can not be executing createOrGetInitialStateForComponent or
// createOrGetInitialHookState from any thread.
states.clear()
_states.clear()
}
}
}

0 comments on commit c365c27

Please sign in to comment.