Skip to content

Commit

Permalink
Merge pull request #1190 from Shopify/uk-disable-trackers-in-commands
Browse files Browse the repository at this point in the history
Disable trackers in commands that don't need them
  • Loading branch information
paracycle authored Sep 28, 2022
2 parents 9db4621 + 8e277cd commit e6f39e1
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 28 deletions.
10 changes: 0 additions & 10 deletions lib/tapioca.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ module Tapioca
class << self
extend T::Sig

sig { params(trace_name: Symbol, block: T.proc.params(arg0: TracePoint).void).void }
def register_trace(trace_name, &block)
@traces << TracePoint.trace(trace_name, &block)
end

sig { void }
def disable_traces
@traces.each(&:disable)
end

sig do
type_parameters(:Result)
.params(blk: T.proc.returns(T.type_parameter(:Result)))
Expand Down
2 changes: 0 additions & 2 deletions lib/tapioca/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,6 @@ def gem(*gems)
option :payload, type: :boolean, desc: "Check shims against Sorbet's payload", default: true
option :workers, aliases: ["-w"], type: :numeric, desc: "Number of parallel workers (default: auto)"
def check_shims
Tapioca.disable_traces

command = Commands::CheckShims.new(
gem_rbi_dir: options[:gem_rbi_dir],
dsl_rbi_dir: options[:dsl_rbi_dir],
Expand Down
1 change: 1 addition & 0 deletions lib/tapioca/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module Tapioca
module Commands
autoload :Command, "tapioca/commands/command"
autoload :CommandWithoutTracker, "tapioca/commands/command_without_tracker"
autoload :Annotations, "tapioca/commands/annotations"
autoload :CheckShims, "tapioca/commands/check_shims"
autoload :Dsl, "tapioca/commands/dsl"
Expand Down
2 changes: 1 addition & 1 deletion lib/tapioca/commands/annotations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Tapioca
module Commands
class Annotations < Command
class Annotations < CommandWithoutTracker
extend T::Sig

sig do
Expand Down
2 changes: 1 addition & 1 deletion lib/tapioca/commands/check_shims.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Tapioca
module Commands
class CheckShims < Command
class CheckShims < CommandWithoutTracker
extend T::Sig
include SorbetHelper
include RBIFilesHelper
Expand Down
18 changes: 18 additions & 0 deletions lib/tapioca/commands/command_without_tracker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# typed: strict
# frozen_string_literal: true

module Tapioca
module Commands
class CommandWithoutTracker < Command
extend T::Helpers

abstract!

sig { void }
def initialize
Tapioca::Runtime::Trackers.disable_all!
super
end
end
end
end
2 changes: 1 addition & 1 deletion lib/tapioca/commands/configure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Tapioca
module Commands
class Configure < Command
class Configure < CommandWithoutTracker
sig do
params(
sorbet_config: String,
Expand Down
2 changes: 1 addition & 1 deletion lib/tapioca/commands/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Tapioca
module Commands
class Dsl < Command
class Dsl < CommandWithoutTracker
include SorbetHelper
include RBIFilesHelper

Expand Down
2 changes: 1 addition & 1 deletion lib/tapioca/commands/require.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Tapioca
module Commands
class Require < Command
class Require < CommandWithoutTracker
sig do
params(
requires_path: String,
Expand Down
2 changes: 1 addition & 1 deletion lib/tapioca/commands/todo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Tapioca
module Commands
class Todo < Command
class Todo < CommandWithoutTracker
include SorbetHelper

sig do
Expand Down
28 changes: 27 additions & 1 deletion lib/tapioca/runtime/trackers.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
# typed: strict
# typed: true
# frozen_string_literal: true

require "tapioca/runtime/trackers/tracker"

module Tapioca
module Runtime
module Trackers
extend T::Sig

@trackers = T.let([], T::Array[Tracker])

class << self
extend T::Sig

sig { void }
def disable_all!
@trackers.each(&:disable!)
end

sig { params(tracker: Tracker).void }
def register_tracker(tracker)
@trackers << tracker
end
end
end
end
end

# The load order below is important:
# ----------------------------------
# We want the mixin tracker to be the first thing that is
Expand Down
3 changes: 3 additions & 0 deletions lib/tapioca/runtime/trackers/autoload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Tapioca
module Runtime
module Trackers
module Autoload
extend Tracker
extend T::Sig

NOOP_METHOD = ->(*_args, **_kwargs, &_block) {}
Expand All @@ -28,6 +29,8 @@ def eager_load_all!

sig { params(constant_name: String).void }
def register(constant_name)
return unless enabled?

@constant_names_registered_for_autoload << constant_name
end

Expand Down
11 changes: 9 additions & 2 deletions lib/tapioca/runtime/trackers/constant_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Trackers
# correspondence between classes/modules and files, as this information isn't
# available in the ruby runtime without extra accounting.
module ConstantDefinition
extend Tracker
extend Reflection
extend T::Sig

Expand All @@ -20,7 +21,7 @@ class ConstantLocation < T::Struct
@class_files = {}.compare_by_identity

# Immediately activated upon load. Observes class/module definition.
Tapioca.register_trace(:class) do |tp|
@class_tracepoint = TracePoint.trace(:class) do |tp|
next if tp.self.singleton_class?

key = tp.self
Expand All @@ -40,7 +41,7 @@ class ConstantLocation < T::Struct
(@class_files[key] ||= Set.new) << loc
end

Tapioca.register_trace(:c_return) do |tp|
@creturn_tracepoint = TracePoint.trace(:c_return) do |tp|
next unless tp.method_id == :new
next unless Module === tp.return_value

Expand All @@ -50,6 +51,12 @@ class ConstantLocation < T::Struct
end

class << self
def disable!
@class_tracepoint.disable
@creturn_tracepoint.disable
super
end

def build_constant_location(tp, locations)
file = resolve_loc(caller_locations)
lineno = file == File.realpath(tp.path) ? tp.lineno : 0
Expand Down
10 changes: 3 additions & 7 deletions lib/tapioca/runtime/trackers/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ module Tapioca
module Runtime
module Trackers
module Mixin
extend Tracker
extend T::Sig

@constants_to_mixin_locations = {}.compare_by_identity
@mixins_to_constants = {}.compare_by_identity
@enabled = true

class Type < T::Enum
enums do
Expand All @@ -28,11 +28,7 @@ class << self
.returns(T.type_parameter(:Result))
end
def with_disabled_registration(&block)
@enabled = false

block.call
ensure
@enabled = true
with_disabled_tracker(&block)
end

sig do
Expand All @@ -43,7 +39,7 @@ def with_disabled_registration(&block)
).void
end
def register(constant, mixin, mixin_type)
return unless @enabled
return unless enabled?

location = Reflection.resolve_loc(caller_locations)

Expand Down
3 changes: 3 additions & 0 deletions lib/tapioca/runtime/trackers/required_ancestor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ module Tapioca
module Runtime
module Trackers
module RequiredAncestor
extend Tracker
@required_ancestors_map = {}.compare_by_identity

class << self
extend T::Sig

sig { params(requiring: T::Helpers, block: T.proc.void).void }
def register(requiring, block)
return unless enabled?

ancestors = @required_ancestors_map[requiring] ||= []
ancestors << block
end
Expand Down
45 changes: 45 additions & 0 deletions lib/tapioca/runtime/trackers/tracker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# typed: true
# frozen_string_literal: true

module Tapioca
module Runtime
module Trackers
module Tracker
extend T::Sig
extend T::Helpers

abstract!

class << self
extend T::Sig

sig { params(base: T.all(Tracker, Module)).void }
def extended(base)
Trackers.register_tracker(base)
base.instance_exec do
@enabled = true
end
end
end

sig { void }
def disable!
@enabled = false
end

def enabled?
@enabled
end

def with_disabled_tracker(&block)
original_state = @enabled
@enabled = false

block.call
ensure
@enabled = original_state
end
end
end
end
end
1 change: 1 addition & 0 deletions spec/tapioca/commands/annotations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
module Tapioca
module Commands
class AnnotationsSpec < SpecWithProject
include Tapioca::Helpers::Test::Isolation
include WebMock::API

DUMMY_REPO_URI_1 = "https://my-private-repo-1"
Expand Down

0 comments on commit e6f39e1

Please sign in to comment.