class RuboCop::Cop::SketchupPerformance::SelectionBulkChanges
Prefer changing selection in bulk instead of modifying selection within loops. It's much faster to change the selection in bulk. UI updates are triggered when you update the selection, so reduce the amount of calls.
@example Poor performance
model = Sketchup.active_model model.active_entities.each { |entity| model.selection.add(entity) if entity.is_a?(Sketchup::Face) }
@example Better performance
model = Sketchup.active_model faces = model.active_entities.map { |entity| entity.is_a?(Sketchup::Face) } model.selection.add(faces)
@example Better performance and simpler
model = Sketchup.active_model faces = model.active_entities.grep(Sketchup::Face) model.selection.add(faces)
Constants
- MSG
Public Instance Methods
iterator?(node)
click to toggle source
# File lib/rubocop/sketchup/cop/performance/selection_bulk.rb, line 61 def iterator?(node) node.is_a?(RuboCop::AST::ForNode) || node.is_a?(RuboCop::AST::UntilNode) || node.is_a?(RuboCop::AST::WhileNode) || block_loop?(node) || numeric_loop?(node) end
on_send(node)
click to toggle source
# File lib/rubocop/sketchup/cop/performance/selection_bulk.rb, line 69 def on_send(node) return unless selection?(node) return unless node.ancestors.any?(&method(:iterator?)) add_offense(node, location: range_with_receiver(node)) end