Skip to content

Forbid using `strong_memoize_attr` on methods with parameters

Peter Leitzen requested to merge pl-strong-memoize-arity into master

What does this MR do and why?

strong_memoize_attr does not support this case yet.

This fixes a problem (#384925 (closed)) with Gitlab::Utils::Override#override run via bin/rake lint:static_verification for the following example:

class A
  include Gitlab::Utils::StrongMemoize

  def do_something
    # ...
  end
  strong_memoize_attr :do_something
end

class EE::A
  extend Gitlab::Utils::Override

  override :do_something
  def do_something
    super
    # ...
  end
end

A.prepend_mod

Note that this fix does not handle method with required parameters such as:

def do_something(required_param)
end

It also side steps a problem where memoized results are wrong due to parameters.

For example:

  # bad
  def expensive_method(limit: 100)
    LoadFromDatabase.with_theta.limit(limit)
  end

  # good
  def expensive_method(limit: 100)
    strong_memoize_with(:expensive_method, limit) do
      LoadFromDatabase.with_theta.limit(limit)
    end
  end

How to set up and validate locally

From #384925 (closed):

  1. Apply diff via git apply
diff --git a/lib/gitlab/ci/build/context/build.rb b/lib/gitlab/ci/build/context/build.rb
index a2b330f19b1a..f054c7f7a7e1 100644
--- a/lib/gitlab/ci/build/context/build.rb
+++ b/lib/gitlab/ci/build/context/build.rb
@@ -16,14 +16,13 @@ def initialize(pipeline, attributes = {})
           end
 
           def variables
-            strong_memoize(:variables) do
-              # This is a temporary piece of technical debt to allow us access
-              # to the CI variables to evaluate rules before we persist a Build
-              # with the result. We should refactor away the extra Build.new,
-              # but be able to get CI Variables directly from the Seed::Build.
-              stub_build.scoped_variables
-            end
+            # This is a temporary piece of technical debt to allow us access
+            # to the CI variables to evaluate rules before we persist a Build
+            # with the result. We should refactor away the extra Build.new,
+            # but be able to get CI Variables directly from the Seed::Build.
+            stub_build.scoped_variables
           end
+          strong_memoize_attr :variables
 
           private
 

CTRL+D

  1. Run $ bin/rake lint:static_verification
  2. See no errors

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Peter Leitzen

Merge request reports