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"
@@ -458,6 +458,54 @@ Require Not QP_NativeMutationPossible,"no native mutation"
''')
def test_zero_match_does_not_read_unused_component_geometry(tmp_path, monkeypatch):
counted = EXTRA.replace('Public UID, Refdes, Orientation, Scale, Attributes, AddCount',
'Public UID, Refdes, Orientation, Scale, Attributes, AddCount, LocationReads')
counted = counted.replace('Set GetLocation=Point',
'LocationReads=LocationReads+1\n Set GetLocation=Point', 1)
monkeypatch.setattr(f'{__name__}.EXTRA', counted)
run_vbs(tmp_path, 'preserve', '''
QP_Fso.DeleteFile SnapshotPath
Comp.LocationReads=0
QPCaptureAndSelect Command,SnapshotPath
Require QPReadState(QP_Fso.BuildPath(QP_Root,"status.tsv"),"value-job")="no_match","no-match state"
Require Comp.LocationReads=0,"no-match read geometry for an index that is never consumed"
Require Not QP_Fso.FileExists(SnapshotPath),"no-match generated a snapshot"
''')
def test_component_lookup_reads_fallback_geometry_only_when_needed(tmp_path, monkeypatch):
counted = EXTRA.replace('Public UID, Refdes, Orientation, Scale, Attributes, AddCount',
'Public UID, Refdes, Orientation, Scale, Attributes, AddCount, LocationReads')
counted = counted.replace('Set GetLocation=Point',
'LocationReads=LocationReads+1\n Set GetLocation=Point', 1)
monkeypatch.setattr(f'{__name__}.EXTRA', counted)
run_vbs(tmp_path, 'preserve', '''
Dim Records, Record, Candidate
Set Records=QPReadSnapshot(SnapshotPath)
Set Record=Records.Item("components").Item("0")
QPInvalidateComponentIndex
Comp.LocationReads=0
Set Candidate=QPFindUniqueByOldLocation(Record)
Require Candidate Is Comp,"UID lookup returned wrong component"
Require Comp.LocationReads=0,"UID lookup eagerly read fallback geometry"
Record("uid")="missing"
Set Candidate=QPFindUniqueByOldLocation(Record)
Require Candidate Is Comp,"refdes fallback returned wrong component"
Require Comp.LocationReads=0,"refdes lookup eagerly read location fallback"
Record("refdes")="missing"
Set Candidate=QPFindUniqueByOldLocation(Record)
Require Candidate Is Comp,"location fallback returned wrong component"
Require Comp.LocationReads=1,"location fallback did not read geometry exactly once"
Set Candidate=QPFindUniqueByOldLocation(Record)
Require Comp.LocationReads=1,"same read context rebuilt its fallback index"
QPInvalidateComponentIndex
Set Candidate=QPFindUniqueByOldLocation(Record)
Require Candidate Is Comp,"fresh location fallback returned wrong component"
Require Comp.LocationReads=2,"invalidated index reused stale geometry"
''')
def test_pin_index_avoids_repeated_full_sheet_queries(tmp_path):
run_vbs(tmp_path, 'part_number', '''
QPInvalidatePinIndex
+6 -1
View File
@@ -445,7 +445,8 @@ WScript.Echo CStr(InStr(Text, "G" & vbTab & "note" & vbTab) > 0)
@pytest.mark.parametrize('read_failure', [False, True])
def test_all_match_capture_protects_unselected_and_does_not_hide_pn_error(tmp_path, read_failure):
@pytest.mark.parametrize('exact_scope', [False, True])
def test_capture_protects_unselected_and_does_not_hide_target_pn_error(tmp_path, read_failure, exact_scope):
snapshot = str(tmp_path / 'captured.tsv')
output = run_vbs(tmp_path, UNTOUCHED_ROUTINES + ['QPCaptureAndSelect', 'QPSnapshotComponent',
'QPCommandTargets', 'QPBooleanText', 'QPLogField'], '''
@@ -459,8 +460,12 @@ QP_App.ActiveView.Components.Add Comp, True: QP_App.ActiveView.Components.Add Ot
Set Pin = New FakePin: Set Pin.Component = Comp: Pin.Number = "1": Pin.X = 0: Pin.Y = 0
Set Pin.Connection = Nothing: QP_App.ActiveView.Pins.Add Pin, True
Comp.RaiseOnFind = ''' + ('True' if read_failure else 'False') + '''
Other.RaiseOnFind = ''' + ('True' if exact_scope else 'False') + '''
Set Command = CreateObject("Scripting.Dictionary")
Command.Add "job_id", "test": Command.Add "old_part_number", "OLD": Command.Add "new_part_number", "NEW"
''' + ('''Command.Add "target_scope", "targets"
Command.Add "target_selectors", QPEncode("u1" & Chr(31) & "R1" & Chr(31))
''' if exact_scope else '') + '''
On Error Resume Next
QPCaptureAndSelect Command, "''' + snapshot + '''"
WScript.Echo Err.Number