Devel:...::Core(3pm)
okay so welcome to the Devel::NYTProf::Core(3pm) manual page, straight outta the User Contributed Perl Documentation π bestie, grab a snack, this one's actually juicy.
β¨ NAME β¨
Devel::NYTProf::Core β the module that loads the internals of Devel::NYTProf. that's the job. that's the whole job π
π¦ DESCRIPTION π¦
so likeβ¦ do not use this module directly, fr fr. it was not built for that, it's not giving "public API", it's giving "backstage pass only" π
if you're looking for the actual thing you wanna use, go read Devel::NYTProf, Devel::NYTProf::Data, and Devel::NYTProf::Reader instead π«Ά
but here's the plot twist β even tho it's not meant to be used directly, it's a handy place to document some internals. and that is exactly what we're doing. periodt π
π₯ SUBROUTINE PROFILER π₯
the subroutine profiler is out here intercepting the entersub opcode β that's the opcode perl uses to invoke a subroutine. and yes, it covers both XS subs (we're calling them xsubs from here on, no takebacks) and pure perl subs π
here's the whole vibe of how it works:
πΈ Before the subroutine call πΈ
the profiler records:
- the current time
- the current value of
cumulative_subr_secs(saved asinitial_subr_secs) - the current
cumulative_overhead_ticks(saved asinitial_overhead_ticks)
meanwhile the statement profiler is measuring time at the start and end of processing for each statement β so time spent inside the profiler itself (like, writing to the file, for example) gets excluded, no cap. it accumulates that measured overhead into the cumulative_overhead_ticks variable π«
and the subroutine profiler does the same kind of thing: it measures the exclusive time spent in subroutines and accumulates it into the cumulative_subr_secs global π
β¨ Make the subroutine call β¨
the call gets made by executing the original perl internal code for the entersub opcode. slay.
π Calling a perl subroutine π
if the sub being called is a perl sub, then when the entersub opcode returns back into the subroutine profiler, the subroutine has been 'entered' β but the first opcode of the subroutine hasn't been executed yet. and here's the crucial bit: a new scope has been entered by the entersub opcode.
so the subroutine profiler then pushes a destructor onto the context stack. the destructor is basically just sitting inside the sub, like a local, which means it'll get triggered when the subroutine exits by any means. also β because it was the first thing pushed onto the context stack β it gets triggered after any activity caused by the subroutine's scope exiting.
when the destructor is invoked, it calls a function that completes the measurement of the time spent in the sub (see below π).
and that's why profiling perl subroutines is very accurate and robust. we love to see it π«Ά
π Calling an xsub π
if the sub being called is an xsub, then control doesn't return from the entersub opcode until the xsub has returned. the profiler detects this and calls the function that completes the measurement of the time spent in the xsub.
so far so good. but there's a problem π what if the xsub doesn't return normally and throws an exception instead?
in that case (currently) the profiler acts as if the xsub was never called. time spent inside the xsub will be allocated to the calling sub. yeah. it's giving oops π
β Completing the measurement β
the function that completes the timing of a subroutine call does the following:
it calculates the time spent in the statement profiler:
overhead_ticks = cumulative_overhead_ticks - initial_overhead_ticks
and subtracts that from the total time spent 'inside' the subroutine:
incl_subr_sec = (time now - time call was made) - overhead_ticks
boom β that gives us an accurate inclusive time β¨ to get the exclusive time, it calculates the time spent in subroutines called from the subroutine call we're measuring:
called_sub_secs = cumulative_subr_secs - initial_subr_secs
and subtracts that from the incl_subr_sec:
excl_subr_sec = incl_subr_sec - called_sub_secs
to make that easier to follow, consider a call to a sub that calls no others. in that case cumulative_subr_secs stays unchanged during the call, so called_sub_secs is zero, and excl_subr_sec is the same as incl_subr_sec. cute, right? π
finally, it adds the exclusive time to the cumulative exclusive time:
cumulative_subr_secs += excl_subr_sec
π AUTHOR π
Tim Bunce, http://blog.timbunce.org π
π COPYRIGHT AND LICENSE π
Copyright (C) 2008, 2009 by Tim Bunce.
this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself β either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available π«Ά
and that's the whole page, bestie β this one came courtesy of perl v5.42.3, dated 2026-08-20 πΈ go forth and profile responsibly, mwah π
π¦ where this page comes from
- ubuntu stonking: libdevel-nytprof-perl 6.15+dfsg-1build1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms