From d88a17f3e646cb901dcd6cfd09232f373ad7b052 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sun, 3 Jan 2021 17:04:59 +0100 Subject: [PATCH] Cache Frame#context results --- src/backtracer/backtrace/frame.cr | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/backtracer/backtrace/frame.cr b/src/backtracer/backtrace/frame.cr index cd44cfc..eea7a47 100644 --- a/src/backtracer/backtrace/frame.cr +++ b/src/backtracer/backtrace/frame.cr @@ -1,6 +1,8 @@ module Backtracer # An object representation of a stack frame. struct Backtrace::Frame + @context_cache = {} of Int32 => Context + # The method of this frame (such as `User.find`). getter method : String @@ -109,8 +111,11 @@ module Backtracer # See `Configuration#context_lines` def context(context_lines : Int32? = nil) : Context? context_lines ||= configuration.context_lines - return unless context_lines && (context_lines > 0) + + cached = @context_cache[context_lines]? + return cached if cached + return unless (lineno = @lineno) && (lineno > 0) return unless (path = @path) && File.readable?(path) @@ -121,12 +126,13 @@ module Backtracer pre_context = lines[Math.max(0, lineidx - context_lines), context_lines] post_context = lines[Math.min(lines.size, lineidx + 1), context_lines] - Context.new( - lineno: lineno, - pre: pre_context, - line: context_line, - post: post_context, - ) + @context_cache[context_lines] = + Context.new( + lineno: lineno, + pre: pre_context, + line: context_line, + post: post_context, + ) end end end