houseabsolute / Devel-StackTrace

An object representing a stack trace
https://metacpan.org/release/Devel-StackTrace/
Other
7 stars 13 forks source link

Consider adding boolean overload that just returns true #23

Closed miyagawa closed 6 months ago

miyagawa commented 6 months ago

re https://github.com/plack/Plack/pull/697

if ($trace && some_other_condition()) {
  $trace->as_string;
}

this code looks as if $trace->as_string doesn't get called if some_other_condition() is false, or just once if the condition is true. But actually, the first if ($trace) triggers as_string due to the string overloading, and how perl's overload automatically uses the string value for boolean check. As a result, $trace->as_string is called one extra time which could cause some slow down in the app when the stacktrace is huge.

Consider adding bool => sub { 1 } to the overload list to avoid this. I've worked around in the caller code to change the if statement to if (ref $trace) for now.

autarch commented 6 months ago

Thanks for the suggestion. This is done in v2.05.