Data-UUID-MT

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN


    Returns a reference to the internal UUID generator function. Because
    this avoids method call overhead, it is slightly faster than calling
    "create".

  reseed
      $ug->reseed;

    Reseeds the internal pseudo-random number generator. This happens
    automatically after a fork or thread creation (assuming
    Scalar::Util::weaken), but may be called manually if desired for some
    reason.

    Any arguments provided are passed to Math::Random::MT::Auto::srand() for
    custom seeding.

      $ug->reseed('hotbits' => 250, '/dev/random');

UUID STRING REPRESENTATIONS
    A UUID contains 16 bytes. A hex string representation looks like
    0xb0470602a64b11da863293ebf1c0e05a. A "standard" representation looks

README  view on Meta::CPAN

    the Mersenne Twister algorithm. Math::Random::MT::Auto seeds from system
    sources (including Win32 specific ones on that platform) if available
    and falls back to other less ideal sources if not.

  Fork and thread safety
    Pseudo-random number generators used in generating UUIDs should be
    reseeded if the process forks or if threads are created.

    Data::UUID::MT checks if the process ID has changed before generating a
    UUID and reseeds if necessary. If Scalar::Util is installed and provides
    "weaken()", Data::UUID::MT will also reseed its objects on thread
    creation.

    Data::UUID::LibUUID will reseed on fork on Mac OSX.

    I have not explored further whether other UUID generators are
    fork/thread safe.

  Benchmarks
    The examples/bench.pl program included with this module does some simple
    benchmarking of UUID generation speeds. Here is the output from my

lib/Data/UUID/MT.pm  view on Meta::CPAN


package Data::UUID::MT;
our $VERSION = '1.001'; # VERSION

use Config;
use Math::Random::MT::Auto;
use Scalar::Util 1.10 ();
use Time::HiRes ();

# track objects across threads for reseeding
my ($can_weaken, @objects);
$can_weaken = Scalar::Util->can('weaken');
sub CLONE { defined($_) && $_->reseed for @objects }

# HoH: $builders{$Config{uvsize}}{$version}
my %builders = (
  '8' => {
    '1'   =>  ($] ge 5.010 ? '_build_64bit_v1'  : '_build_64bit_v1_old' ),
    '4'   =>  ($] ge 5.010 ? '_build_64bit_v4'  : '_build_64bit_v4_old' ),
    '4s'  =>  ($] ge 5.010 ? '_build_64bit_v4s' : '_build_64bit_v4s_old'),
  },
  '4' => {

lib/Data/UUID/MT.pm  view on Meta::CPAN


  my $self = {
    _prng => $prng,
    _version => $args{version},
  };

  bless $self, $class;

  $self->{_iterator} = $self->_build_iterator;

  if ($can_weaken) {
    push @objects, $self;
    Scalar::Util::weaken($objects[-1]);
  }

  return $self;
}

sub _build_iterator {
  my $self = shift;
  # get the iterator based on int size and UUID version
  my $int_size = $Config{uvsize};
  my $builder = $builders{$int_size}{$self->{_version}};

lib/Data/UUID/MT.pm  view on Meta::CPAN

  my $uuid = $next->();

Returns a reference to the internal UUID generator function.  Because this
avoids method call overhead, it is slightly faster than calling C<create>.

=head2 reseed

  $ug->reseed;

Reseeds the internal pseudo-random number generator.  This happens
automatically after a fork or thread creation (assuming Scalar::Util::weaken),
but may be called manually if desired for some reason.

Any arguments provided are passed to Math::Random::MT::Auto::srand() for
custom seeding.

  $ug->reseed('hotbits' => 250, '/dev/random');

=for Pod::Coverage method_names_here

=head1 UUID STRING REPRESENTATIONS

lib/Data/UUID/MT.pm  view on Meta::CPAN

(including Win32 specific ones on that platform) if available and falls back to
other less ideal sources if not.

=head2 Fork and thread safety

Pseudo-random number generators used in generating UUIDs should be reseeded if
the process forks or if threads are created.

Data::UUID::MT checks if the process ID has changed before generating a UUID
and reseeds if necessary.  If L<Scalar::Util> is installed and provides
C<weaken()>, Data::UUID::MT will also reseed its objects on thread creation.

Data::UUID::LibUUID will reseed on fork on Mac OSX.

I have not explored further whether other UUID generators are fork/thread safe.

=head2 Benchmarks

The F<examples/bench.pl> program included with this module does some simple
benchmarking of UUID generation speeds.  Here is the output from my desktop
system (AMD Phenom II X6 1045T CPU).  Note that "v?" is used where the choice



( run in 0.378 second using v1.01-cache-2.11-cpan-65fba6d93b7 )