Class: DiverDown::Trace::CallStack

Inherits:
Object
  • Object
show all
Defined in:
lib/diver_down/trace/call_stack.rb

Overview

To handle call stacks obtained by TracePoint more efficiently. TracePoint also acquires calls that are not trace targets, but for dependency extraction, we want to acquire only a list of targets. In this class, push/pop is performed on all call/return, but it should always be possible to trace back to the caller of the target dependency at high speed.

Defined Under Namespace

Classes: StackEmptyError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCallStack

Returns a new instance of CallStack.



14
15
16
17
18
# File 'lib/diver_down/trace/call_stack.rb', line 14

def initialize
  @ignored_stack_size = nil
  @stack_size = 0
  @context_stack = {}
end

Instance Attribute Details

#stack_sizeObject (readonly)



12
13
14
# File 'lib/diver_down/trace/call_stack.rb', line 12

def stack_size
  @stack_size
end

Instance Method Details

#context_stackArray<Object>

Returns:

  • (Array<Object>)


31
32
33
# File 'lib/diver_down/trace/call_stack.rb', line 31

def context_stack
  @context_stack.values
end

#context_stack_sizeArray<Integer>

Returns:

  • (Array<Integer>)


26
27
28
# File 'lib/diver_down/trace/call_stack.rb', line 26

def context_stack_size
  @context_stack.keys
end

#empty_context_stack?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/diver_down/trace/call_stack.rb', line 21

def empty_context_stack?
  @context_stack.empty?
end

#ignored?Boolean

If a stack is not already a tracing target, some conditions are unnecessary so that it can be easily ignored.

Returns:

  • (Boolean)


46
47
48
# File 'lib/diver_down/trace/call_stack.rb', line 46

def ignored?
  !@ignored_stack_size.nil?
end

#popvoid

This method returns an undefined value.

Raises:



51
52
53
54
55
56
57
# File 'lib/diver_down/trace/call_stack.rb', line 51

def pop
  raise StackEmptyError if @stack_size.zero?

  @context_stack.delete(@stack_size) if @context_stack.key?(@stack_size)
  @ignored_stack_size = nil if @ignored_stack_size && @ignored_stack_size == @stack_size
  @stack_size -= 1
end

#push(context = nil, ignored: false) ⇒ void

This method returns an undefined value.

Parameters:

  • context (Object, nil) (defaults to: nil)

    User defined stack context.



37
38
39
40
41
# File 'lib/diver_down/trace/call_stack.rb', line 37

def push(context = nil, ignored: false)
  @stack_size += 1
  @context_stack[@stack_size] = context unless context.nil?
  @ignored_stack_size ||= @stack_size if ignored
end