Skip to content

Commit

Permalink
GC allocs for Bind fixed (no more allocs)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saeed Barari committed Jul 22, 2023
1 parent ee99dda commit 9d08602
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
31 changes: 24 additions & 7 deletions BContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public sealed class BContext : MonoBehaviour {
[NonSerialized] readonly HashSet<Type> _dependencyTypes = new( 16 );
[NonSerialized] bool _initialized;

#if UNITY_EDITOR
/// <summary>
/// When true, <see cref="StructDependencies_Serialized"/> will be updated.
/// </summary>
[NonSerialized] internal bool IsEditorInspecting_EditorOnly;
#endif

#if UNITY_EDITOR
void OnValidate() {
if (Application.isPlaying) return;
Expand Down Expand Up @@ -93,7 +100,7 @@ void OnValidate() {

void Awake() {
_structDependencies.Clear();
AddAllSerializedStructs();
ApplyAllSerializedStructs();
if (!_initialized) {
SyncAllDependencyTypes( true );
BinjectManager.AddContext( this );
Expand Down Expand Up @@ -121,12 +128,13 @@ void OnDestroy() {
/// the old one.
/// </summary>
public void Bind<T>(T dependency) {
if (_dependencyTypes.Add( dependency.GetType() )) {
if (_dependencyTypes.Add( typeof(T) )) {
// new type
if (typeof(T).IsValueType) {
_structDependencies.Add( new RealValueHolder<T>( dependency ) );
#if UNITY_EDITOR
StructDependencies_Serialized.Add( new BoxedValueHolder( dependency ) );
if (IsEditorInspecting_EditorOnly)
StructDependencies_Serialized.Add( new BoxedValueHolder( dependency ) );
#endif
}
else if (IsUnityObjectType( dependency.GetType() ))
Expand All @@ -140,14 +148,16 @@ public void Bind<T>(T dependency) {
if (_structDependencies[i] is RealValueHolder<T> sd) {
sd.Value = dependency;
#if UNITY_EDITOR
StructDependencies_Serialized[i].BoxAndSetValue( dependency );
if (IsEditorInspecting_EditorOnly)
StructDependencies_Serialized[i].BoxAndSetValue( dependency );
#endif
break;
}
if (_structDependencies[i].GetValueType() == typeof(T)) {
_structDependencies[i].BoxAndSetValue( dependency );
#if UNITY_EDITOR
StructDependencies_Serialized[i].BoxAndSetValue( dependency );
if (IsEditorInspecting_EditorOnly)
StructDependencies_Serialized[i].BoxAndSetValue( dependency );
#endif
break;
}
Expand Down Expand Up @@ -181,7 +191,8 @@ public void Unbind<T>() {
if (_structDependencies[i].GetValueType() == typeof(T)) {
_structDependencies.RemoveAt( i );
#if UNITY_EDITOR
StructDependencies_Serialized.RemoveAt( i );
if (IsEditorInspecting_EditorOnly)
StructDependencies_Serialized.RemoveAt( i );
#endif
return;
}
Expand Down Expand Up @@ -236,11 +247,17 @@ public T GetDependencyNoCheck<T>() {


[MethodImpl( MethodImplOptions.AggressiveInlining )]
void AddAllSerializedStructs() {
void ApplyAllSerializedStructs() {
for (int i = 0; i < StructDependencies_Serialized.Count; i++)
_structDependencies.Add( StructDependencies_Serialized[i] );
}

internal void PopulateSerializedStructs() {
StructDependencies_Serialized.Clear();
for (int i = 0; i < _structDependencies.Count; i++)
StructDependencies_Serialized.Add( new BoxedValueHolder( _structDependencies[i].BoxAndGetValue() ) );
}

/// <summary>
/// Stores types of all dependencies to <see cref="_dependencyTypes"/>.
/// </summary>
Expand Down
16 changes: 14 additions & 2 deletions Editor/BContextEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ internal class BContextEditor : Editor {
ReorderableList _structList;
ReorderableList _objectList;
bool _advancedFoldout;

BContext _context;

static GUIStyle _headerStyle;
static GUIContent _objectListHeaderGuiContent;
static GUIContent _classListHeaderGuiContent;
static GUIContent _structListHeaderGuiContent;


static TypeSelectDropDown _classTypeDropDown = new( new AdvancedDropdownState(),
filter: type =>
true
Expand Down Expand Up @@ -263,8 +265,18 @@ void SetupStructList() {

static bool HasCustomEditor(SerializedProperty prop) => prop.managedReferenceValue is IBHasCustomDrawer;

void OnEnable() {
_context = (BContext)target;
_context.PopulateSerializedStructs();
_context.IsEditorInspecting_EditorOnly = true;
}

void OnDisable() {
_context.IsEditorInspecting_EditorOnly = false;
}

public override void OnInspectorGUI() {
if ( classDependenciesProp == null ) {
if ( classDependenciesProp == null ) {
objectDependenciesProp = serializedObject.FindProperty( nameof(BContext.UnityObjectDependencies) );
classDependenciesProp = serializedObject.FindProperty( nameof(BContext.ClassDependencies) );
structDependenciesProp = serializedObject.FindProperty( nameof(BContext.StructDependencies_Serialized) );
Expand Down

0 comments on commit 9d08602

Please sign in to comment.