Repository: dotnet/efcore
File: src/EFCore/ChangeTracking/Internal/EntityGraphAttacher.cs (C#)

Code before:
                cancellationToken).ConfigureAwait(false);

            _visited = null;
            rootEntry.StateManager.CompleteAttachGraph();
        }
        catch
        {
            _visited = null;
            rootEntry.StateManager.AbortAttachGraph();
            throw;
        }
    }

    private bool PaintAction(
        EntityEntryGraphNode<(EntityState TargetState, EntityState StoreGenTargetState, bool Force)> node)
    {
        SetReferenceLoaded(node);

        var internalEntityEntry = node.GetInfrastructure();

        if ((internalEntityEntry.EntityState != EntityState.Detached
                && (internalEntityEntry.EntityState != EntityState.Deleted
                    || node.SourceEntry?.GetInfrastructure().SharedIdentityEntry == null))
            || (_visited != null && _visited.Contains(internalEntityEntry.Entity)))
        {
            return false;
        }

        var (targetState, storeGenTargetState, force) = node.NodeState;

        var (isGenerated, isSet) = internalEntityEntry.IsKeySet;

        if (internalEntityEntry.StateManager.ResolveToExistingEntry(
                internalEntityEntry,
                node.InboundNavigation, node.SourceEntry?.GetInfrastructure()))
        {
            (_visited ??= new HashSet<object>(ReferenceEqualityComparer.Instance)).Add(internalEntityEntry.Entity);
        }
        else
        {
            internalEntityEntry.SetEntityState(
                isSet
                    ? (isGenerated ? storeGenTargetState : targetState)
                    : EntityState.Added, // Key can only be not-set if it is store-generated
                acceptChanges: true,
                forceStateWhenUnknownKey: force ? targetState : null,
                fallbackState: targetState);
        }

        return true;
    }

Review comment (performance):
`node.SourceEntry?.GetInfrastructure()` is evaluated multiple times in this condition and again when calling `ResolveToExistingEntry`. Since `EntityGraphAttacher` is on a hot path, consider storing the source `InternalEntityEntry` in a local and reusing it to reduce repeated calls and improve readability.

Code after:
                cancellationToken).ConfigureAwait(false);

            _visited = null;
            rootEntry.StateManager.CompleteAttachGraph();
        }
        catch
        {
            _visited = null;
            rootEntry.StateManager.AbortAttachGraph();
            throw;
        }
    }

    private bool PaintAction(
        EntityEntryGraphNode<(EntityState TargetState, EntityState StoreGenTargetState, bool Force)> node)
    {
        SetReferenceLoaded(node);

        var internalEntityEntry = node.GetInfrastructure();
        var sourceEntry = node.SourceEntry?.GetInfrastructure();

        if ((internalEntityEntry.EntityState != EntityState.Detached
                && (internalEntityEntry.EntityState != EntityState.Deleted
                    || sourceEntry?.SharedIdentityEntry == null))
            || (_visited != null && _visited.Contains(internalEntityEntry.Entity)))
        {
            return false;
        }

        var (targetState, storeGenTargetState, force) = node.NodeState;

        var (isGenerated, isSet) = internalEntityEntry.IsKeySet;

        if (internalEntityEntry.StateManager.ResolveToExistingEntry(
                internalEntityEntry,
                node.InboundNavigation, sourceEntry))
        {
            (_visited ??= new HashSet<object>(ReferenceEqualityComparer.Instance)).Add(internalEntityEntry.Entity);
        }
        else
        {
            internalEntityEntry.SetEntityState(
                isSet
                    ? (isGenerated ? storeGenTargetState : targetState)
                    : EntityState.Added, // Key can only be not-set if it is store-generated
                acceptChanges: true,
                forceStateWhenUnknownKey: force ? targetState : null,
                fallbackState: targetState);
        }

        return true;