@@ -322,22 +322,25 @@ struct TabQueryContent: Equatable {
322322 }
323323
324324 static func == ( lhs: TabQueryContent , rhs: TabQueryContent ) -> Bool {
325- // The query can be multiple megabytes and is bridged from the editor's NSTextStorage. Same-box comparison is
326- // O(1); otherwise use NSString literal equality, which returns in O(1) when the lengths differ (every keystroke
327- // changes the length) and avoids Swift's canonical Unicode normalization.
328- guard lhs. queryStorage === rhs. queryStorage || ( lhs. query as NSString ) . isEqual ( to: rhs. query) ,
329- lhs. queryParameters == rhs. queryParameters,
330- lhs. isParameterPanelVisible == rhs. isParameterPanelVisible,
331- lhs. sourceFileURL == rhs. sourceFileURL,
332- lhs. loadMtime == rhs. loadMtime,
333- lhs. externalModificationDetected == rhs. externalModificationDetected else {
334- return false
335- }
336- switch ( lhs. savedFileContent, rhs. savedFileContent) {
325+ // Cheap scalar fields short-circuit first. The query and saved-file text can be multiple megabytes and are
326+ // bridged from NSTextStorage, so they are compared last and with `sameText` to avoid Swift's canonical Unicode
327+ // comparison (O(n) on the bridged text); the same-box identity check makes an unchanged query O(1).
328+ lhs. isParameterPanelVisible == rhs. isParameterPanelVisible
329+ && lhs. externalModificationDetected == rhs. externalModificationDetected
330+ && lhs. sourceFileURL == rhs. sourceFileURL
331+ && lhs. loadMtime == rhs. loadMtime
332+ && lhs. queryParameters == rhs. queryParameters
333+ && ( lhs. queryStorage === rhs. queryStorage || sameText ( lhs. query, rhs. query) )
334+ && sameText ( lhs. savedFileContent, rhs. savedFileContent)
335+ }
336+
337+ /// Literal text equality that skips Swift's canonical Unicode comparison, returning in O(1) when the lengths differ.
338+ private static func sameText( _ lhs: String ? , _ rhs: String ? ) -> Bool {
339+ switch ( lhs, rhs) {
337340 case ( nil , nil ) :
338341 return true
339- case let ( lhsSaved ? , rhsSaved ? ) :
340- return ( lhsSaved as NSString ) . isEqual ( to: rhsSaved )
342+ case let ( lhs ? , rhs ? ) :
343+ return ( lhs as NSString ) . isEqual ( to: rhs )
341344 default :
342345 return false
343346 }
0 commit comments