On Wed, Oct 02, 2002 at 01:50:56PM +0200, H.Merijn Brand wrote:
>  SYNOPSIS
>         use Devel::Internals;

A little broad.  Perhaps Devel::Memory?


>         my $end = sbrk ();
> 
>         my @array = (1..10000);
>         print "The creation of this array used ",
>             sbrk () - $end, "bytes\n";
> 
>         # Easier
>         use Devel::Internals;
> 
>         my @array = (1..10000);
>         MemUsed "creation of array";
> 
>         my @dup = @array;
>         MemUsed "duplication of array";
> 
>  DESCRIPTION
>       sbrk
>           returns the current memory top. See sbrk(2) manual page

I realize that sbrk() is a familiar concept to deep C programmers on
BSD-style systems, but in order for this to be useful to Perl programmers,
or even users not familiar with BSD, you're going to have to name it
something more descriptive than sbrk() and explain better what it does.

It can still do the same thing as sbrk(), and you might even want to leave
in an alias to sbrk() so BSD core hackers can feel all warm and fuzzy.


>       MemUsed ($msg)
>           Used in void context reports the number of bytes used since the
>           previous call to MemUsed with the message passed

Global state is a Bad Thing.  Consider when you have two different,
unrelated subroutines both calling MemUsed() in the same program.  foo()
calls MemUsed() then bar() then foo() calls MemUsed() again expecting to get
the memory used since it's last call but it's actually getting the memory
used since bar() last called it.

If you're going to keep state like this, better just have an object to
represent various views on the memory state.

I can see two interfaces for this, possibly parallel.  One is similar to
your MemUsed() except the state is stored perl object.  It simply returns
the memory used since the last check on that object.

          package A;
          my $mem_state = Devel::Memory->new;

          sub foo {
              printf "%d has been used since the last call to foo()\n",
                     $mem_state->used_since_last;
          }

The other one allows you to more easily track the memory usage of a block of
code by saying "track from this point to this point".  Here's an example of
tracking the memory used inside a subroutine.

          # meanwhile, in another file
          package B;
          my $mem_state = Devel::Memory->new;

          sub bar {
              $mem_state->start;
              ...do some stuff...
              printf "%d was used inside bar()\n", $mem_state->stop;
          }

For convinence, stop() returns the amount of memory used so you can do it in
two steps (start, stop/report) rather than three (start, stop, report).

The other nice part about having an object is you can then hang lots of
methods off it without polluting the caller's namespace.  So you can have
your methods which print neatly formatted diagnostics seperate from your
methods which simply return values.


>           Used in scalar context returns the current sbrk value

Shouldn't sbrk() do that?  Is this the same value as MemUsed() would print
in void context?


>           Used in list context returns the values saved on every call

?


-- 

Michael G. Schwern   <[EMAIL PROTECTED]>    http://www.pobox.com/~schwern/
Perl Quality Assurance      <[EMAIL PROTECTED]>         Kwalitee Is Job One
    And God was pleased.
    And Dog was happy and wagged his tail.
    And Adam was greatly improved.
    And Cat did not care one way or the other.
        -- http://www.catsarefrommars.com/creationist.htm

Reply via email to