Commits

Anonymous committed d0b9d9f

Optimization/Refactoring: removed the _dir field of ::PathComp .

  • Participants
  • Parent commits bde51c9

Comments (0)

Files changed (3)

+    - Optimization: removed the _dir field of File::Find::Object::PathComp
+    and its _dir_copy copy-accessor, and replaced them all with passing
+    the $dir_str explicitly. This reduced the code considerably, and
+    eliminated a similar symptom to this one:
+        - http://en.wikipedia.org/wiki/Schlemiel_the_painter%27s_Algorithm
+
 0.2.0 - Sun Feb 22 11:52:06 IST 2009
     - Optimization: now not checking for the existence of the callback() after 
     every iteration, in case it doesn't exist. Instead, the default_actions

File lib/File/Find/Object.pm

     my $self = {};
     bless $self, $class;
 
-    $self->_dir([ @{$top->_curr_comps()} ]);
     $self->_stat_ret($top->_top_stat_copy());
 
     my $find = { %{$from->_inodes()} };
 
     $self->_last_dir_scanned(undef);
 
-    $from->_dir($self->_dir_copy());
-
     $top->_fill_actions($self);
 
     push @{$top->_curr_comps()}, "";
 sub _warn_about_loop
 {
     my $self = shift;
-    my $ptr = shift;
+    my $component_idx = shift;
 
     # Don't pass strings directly to the format.
     # Instead - use %s
     warn(
         sprintf(
             "Avoid loop %s => %s\n",
-                $ptr->_dir_as_string(),
-                $self->_curr_path()
+                File::Spec->catdir(
+                    @{$self->_curr_comps()}[0 .. $component_idx]
+                ),
+                $self->_curr_path(),
         )
     );
 
     my $lookup = $self->_current->_inodes;
 
     if (exists($lookup->{$key})) {
-        $self->_warn_about_loop($self->_dir_stack->[$lookup->{$key}]);
+        $self->_warn_about_loop($lookup->{$key});
         return 1;
     }
     else {
 sub _open_dir {
     my $self = shift;
 
-    return $self->_current()->_component_open_dir();
+    return $self->_current()->_component_open_dir(
+        $self->_curr_path()
+    );
 }
 
 sub set_traverse_to
 {
     my $self = shift;
 
-    $self->_current->_dir($self->_curr_comps());
-
     # _open_dir can return undef if $self->_current is not a directory.
     if ($self->_open_dir())
     {

File lib/File/Find/Object/PathComp.pm

             (qw(
                 _actions
                 _curr_file
-                _dir
                 _files
                 _last_dir_scanned
                 _open_dir_ret
 use File::Spec;
 
 __PACKAGE__->_make_copy_methods([qw(
-        _dir
         _files
         _traverse_to
     )]
     return shift->_stat_ret->[1];
 }
 
-sub _dir_as_string
-{
-    my $self = shift;
-
-    return File::Spec->catdir(@{$self->_dir()});
-}
-
 sub _is_same_inode
 {
     my $self = shift;
 sub _should_scan_dir
 {
     my $self = shift;
+    my $dir_str = shift;
 
     if (defined($self->_last_dir_scanned()) &&
-        ($self->_last_dir_scanned() eq $self->_dir_as_string()
+        ($self->_last_dir_scanned() eq $dir_str
        )
     )
     {
     }
     else
     {
-        $self->_last_dir_scanned($self->_dir_as_string());
+        $self->_last_dir_scanned($dir_str);
         return 1;
     }
 }
 sub _set_up_dir
 {
     my $self = shift;
+    my $dir_str = shift;
 
-    $self->_files($self->_calc_dir_files());
+    $self->_files($self->_calc_dir_files($dir_str));
 
     $self->_traverse_to($self->_files_copy());
     
 sub _calc_dir_files
 {
     my $self = shift;
+    my $dir_str = shift;
 
     my $handle;
     my @files;
-    if (!opendir($handle, $self->_dir_as_string()))
+    if (!opendir($handle, $dir_str))
     {
         # Handle this error gracefully.
     }
 sub _component_open_dir
 {
     my $self = shift;
+    my $dir_str = shift;
 
-    if (!$self->_should_scan_dir())
+    if (!$self->_should_scan_dir($dir_str))
     {
         return $self->_open_dir_ret();
     }
 
-    return $self->_set_up_dir();
+    return $self->_set_up_dir($dir_str);
 }
 
 sub _next_traverse_to