perf(execution): build component association indexes only when needed

This commit is contained in:
阿白叔 committed 2026-10-19 10:13:37 +08:00
1 parent 2ad36ba45d
commit 8fe82c3124
3 files changed
+93 -15

No files matched your search

+39 -14
View File
@@ -401,30 +401,51 @@ Sub QPAddComponentIndexEntry(ByRef Index, ByVal Key, ByRef Comp)
End Sub
Sub QPBuildComponentIndex(ByRef Components)
Dim Uids, Refdeses, Locations, Comp, Point, Context, Refdes, Count
Dim Uids, Comp, Context, Count
Context = QPCanonicalPath(QPProjectPath()) & "|" & QPSheetIdentity()
Set Uids = CreateObject("Scripting.Dictionary")
Set Refdeses = CreateObject("Scripting.Dictionary")
Set Locations = CreateObject("Scripting.Dictionary")
Count = 0
For Each Comp In Components
QPAddComponentIndexEntry Uids, CStr(Comp.UID), Comp
Refdes = CStr(Comp.Refdes)
If Len(Refdes) > 0 Then QPAddComponentIndexEntry Refdeses, Refdes, Comp
Set Point = Comp.GetLocation()
QPAddComponentIndexEntry Locations, CStr(Point.X) & "|" & CStr(Point.Y), Comp
Count = Count + 1
If Count Mod 25 = 0 Then QPProgress "index_components", Count, 0
Next
If Context <> QPCanonicalPath(QPProjectPath()) & "|" & QPSheetIdentity() Then _
Err.Raise vbObjectError + 864, "QuickPartReplace", "active context changed during component indexing"
Set QP_ComponentUidIndex = Uids
Set QP_ComponentRefdesIndex = Refdeses
Set QP_ComponentLocationIndex = Locations
Set QP_ComponentRefdesIndex = Nothing
Set QP_ComponentLocationIndex = Nothing
QP_ComponentIndexContext = Context
QPLog QP_Root, "INFO", "component_index_built count=" & Count
End Sub
Function QPBuildComponentAssociationIndex(ByVal UseLocation)
Dim Result, Uid, Bucket, Index, Comp, Point, Key, Context, Count
Context = QPCanonicalPath(QPProjectPath()) & "|" & QPSheetIdentity()
If Context <> CStr(QP_ComponentIndexContext) Then _
Err.Raise vbObjectError + 864, "QuickPartReplace", "active context changed before component association indexing"
Set Result = CreateObject("Scripting.Dictionary")
Count = 0
For Each Uid In QP_ComponentUidIndex.Keys
Set Bucket = QP_ComponentUidIndex.Item(CStr(Uid))
For Each Index In Bucket.Keys
Set Comp = Bucket.Item(CStr(Index))
If UseLocation Then
Set Point = Comp.GetLocation()
Key = CStr(Point.X) & "|" & CStr(Point.Y)
Else
Key = CStr(Comp.Refdes)
End If
If Len(Key) > 0 Then QPAddComponentIndexEntry Result, Key, Comp
Count = Count + 1
If Count Mod 25 = 0 Then QPProgress "index_components", Count, 0
Next
Next
If Context <> QPCanonicalPath(QPProjectPath()) & "|" & QPSheetIdentity() Then _
Err.Raise vbObjectError + 864, "QuickPartReplace", "active context changed during component association indexing"
Set QPBuildComponentAssociationIndex = Result
End Function
Sub QPEnsureComponentIndex()
Dim Context, Components
Context = QPCanonicalPath(QPProjectPath()) & "|" & QPSheetIdentity()
@@ -1092,7 +1113,6 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath)
Dim Comp
Dim Matches
Dim PartNumber
Dim PartAttr
Dim Snapshot
Dim Index
Dim Selectors, Selector, Uid, ValueAttr, ValueText, MatchedUids, UntouchedCount
@@ -1100,22 +1120,23 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath)
If StrComp(CStr(Command("old_part_number")), CStr(Command("new_part_number")), vbTextCompare) = 0 Then _
Err.Raise vbObjectError + 829, "QuickPartReplace", "source and target must differ"
QPInvalidatePinIndex
QPInvalidateComponentIndex
QPProgress "query_components", 0, 0
Set Matches = CreateObject("Scripting.Dictionary")
QP_Stage = "query_components"
QPLog QP_Root, "INFO", "stage=query_components"
Set Components = QP_App.ActiveView.Query(VDM_COMP, VD_ALL)
QPBuildComponentIndex Components
' Capture walks the queried collection directly. Build lookup indexes only
' when restoration needs them, from fresh proxies after native mutation.
Set Selectors = QPCommandTargets(Command)
For Each Comp In Components
PartNumber = ""
Set PartAttr = QPFindCurrentAttribute(Comp, "Part Number")
If Not PartAttr Is Nothing Then PartNumber = QPEffectiveAttributeValue(PartAttr)
Uid = CStr(Comp.UID)
If Selectors Is Nothing Then
PartNumber = QPEffectiveComponentAttribute(Comp, "Part Number")
If StrComp(PartNumber, CStr(Command("old_part_number")), _
vbTextCompare) = 0 Then Matches.Add CStr(Matches.Count), Comp
ElseIf Selectors.Exists(Uid) Then
PartNumber = QPEffectiveComponentAttribute(Comp, "Part Number")
Selector = Selectors.Item(Uid)
ValueText = ""
Set ValueAttr = QPFindCurrentAttribute(Comp, "Value")
@@ -1826,6 +1847,8 @@ Function QPFindUniqueByOldLocation(ByRef ComponentRecord)
Exit Function
End If
Key = CStr(ComponentRecord("refdes"))
If QP_ComponentRefdesIndex Is Nothing Then _
Set QP_ComponentRefdesIndex = QPBuildComponentAssociationIndex(False)
If Len(Key) > 0 And QP_ComponentRefdesIndex.Exists(Key) Then
Set Bucket = QP_ComponentRefdesIndex.Item(Key)
If Bucket.Count > 1 Then Err.Raise vbObjectError + 802, "QuickPartReplace", "ambiguous replacement association"
@@ -1833,6 +1856,8 @@ Function QPFindUniqueByOldLocation(ByRef ComponentRecord)
Exit Function
End If
Key = CStr(ComponentRecord("x")) & "|" & CStr(ComponentRecord("y"))
If QP_ComponentLocationIndex Is Nothing Then _
Set QP_ComponentLocationIndex = QPBuildComponentAssociationIndex(True)
If QP_ComponentLocationIndex.Exists(Key) Then
Set Bucket = QP_ComponentLocationIndex.Item(Key)
If Bucket.Count > 1 Then Err.Raise vbObjectError + 802, "QuickPartReplace", "ambiguous replacement association"