|
| 1 | +/** |
| 2 | + * @name Could be hoisted |
| 3 | + * @description Hoist Rails `ActiveRecord::Relation` query calls out of loops. |
| 4 | + * @kind problem |
| 5 | + * @problem.severity info |
| 6 | + * @precision high |
| 7 | + * @id rb/could-be-hoisted |
| 8 | + * @tags performance |
| 9 | + */ |
| 10 | + |
| 11 | +// Possible Improvements; |
| 12 | +// - Consider also Associations. |
| 13 | +// Associations are lazy-loading by default, so something like |
| 14 | +// in a loop over `article` do |
| 15 | +// `article.book` |
| 16 | +// if you have 1000 articles it will do a 1000 calls to `book`. |
| 17 | +// If you already did `article includes book`, there should be no problem. |
| 18 | +// - Consider instances of ActiveRecordInstanceMethodCall, for instance |
| 19 | +// calls to `pluck`. |
| 20 | +import ruby |
| 21 | +private import codeql.ruby.AST |
| 22 | +import codeql.ruby.ast.internal.Constant |
| 23 | +import codeql.ruby.Concepts |
| 24 | +import codeql.ruby.frameworks.ActiveRecord |
| 25 | +private import codeql.ruby.TaintTracking |
| 26 | + |
| 27 | +string loopMethodName() { |
| 28 | + result in [ |
| 29 | + "each", "reverse_each", "map", "map!", "foreach", "flat_map", "in_batches", "one?", "all?", |
| 30 | + "collect", "collect!", "select", "select!", "reject", "reject!" |
| 31 | + ] |
| 32 | +} |
| 33 | + |
| 34 | +class LoopingCall extends DataFlow::CallNode { |
| 35 | + DataFlow::CallableNode loopBlock; |
| 36 | + |
| 37 | + LoopingCall() { |
| 38 | + this.getMethodName() = loopMethodName() and loopBlock = this.getBlock().asCallable() |
| 39 | + } |
| 40 | + |
| 41 | + DataFlow::CallableNode getLoopBlock() { result = loopBlock } |
| 42 | +} |
| 43 | + |
| 44 | +predicate happensInLoop(LoopingCall loop, DataFlow::CallNode e) { |
| 45 | + loop.getLoopBlock().asCallableAstNode() = e.asExpr().getScope() |
| 46 | +} |
| 47 | + |
| 48 | +predicate happensInOuterLoop(LoopingCall outerLoop, DataFlow::CallNode e) { |
| 49 | + exists(LoopingCall innerLoop | |
| 50 | + happensInLoop(outerLoop, innerLoop) and |
| 51 | + happensInLoop(innerLoop, e) |
| 52 | + ) |
| 53 | +} |
| 54 | + |
| 55 | +predicate happensInInnermostLoop(LoopingCall loop, DataFlow::CallNode e) { |
| 56 | + happensInLoop(loop, e) and |
| 57 | + not happensInOuterLoop(loop, e) |
| 58 | +} |
| 59 | + |
| 60 | +// The ActiveRecord instance is used to potentially control the loop |
| 61 | +predicate usedInLoopControlGuard(ActiveRecordInstance ar, DataFlow::Node guard) { |
| 62 | + TaintTracking::localTaint(ar, guard) and |
| 63 | + guard = guardForLoopControl(_, _) |
| 64 | +} |
| 65 | + |
| 66 | +// A guard for controlling the loop |
| 67 | +DataFlow::Node guardForLoopControl(ConditionalExpr cond, Stmt control) { |
| 68 | + result.asExpr().getAstNode() = cond.getCondition().getAChild*() and |
| 69 | + ( |
| 70 | + control.(MethodCall).getMethodName() = "raise" |
| 71 | + or |
| 72 | + control instanceof NextStmt |
| 73 | + ) and |
| 74 | + control = cond.getBranch(_).getAChild() |
| 75 | +} |
| 76 | + |
| 77 | +from LoopingCall loop, DataFlow::CallNode call |
| 78 | +where |
| 79 | + // Disregard loops over constants |
| 80 | + not isArrayConstant(loop.getReceiver().asExpr(), _) and |
| 81 | + // Disregard tests |
| 82 | + not call.getLocation().getFile().getAbsolutePath().matches("%test%") and |
| 83 | + // Disregard cases where the looping is influenced by the query result |
| 84 | + not usedInLoopControlGuard(call, _) and |
| 85 | + // Only report the inner most loop |
| 86 | + happensInInnermostLoop(loop, call) and |
| 87 | + // Only report calls that are likely to be expensive |
| 88 | + call instanceof ActiveRecordModelFinderCall and |
| 89 | + not call.getMethodName() in ["new", "create"] |
| 90 | +select call, "This call happens inside $@, and could be hoisted.", loop, "this loop" |
0 commit comments