Re: Isn't it time to make libxslt multi-threaded?

Дмитрий Грибов <[email protected]> Thu, 26 Jul 2012 02:51:20 +0400
Newsgroups gmane.comp.gnome.lib.xslt
Message-ID <CAE+B=xmLTf6BY_LuvmMMhvyAnT2d1=PN3AWyPvTDO_SCC-BQRQ@mail.gmail.com>
Uff... Sory for double-posting, but it took a week and 6 mailboxes to get
the message delivered to the list. Something is wrong, perhaps .ru domain
seem to be banned.
Nevertheless, in the attachment is an idea I believe to be good. It needs
persistent threads and other real-world-fixes, perl shows only obvious
easy-to-read abstract algorithm of the three-parser threading.

Unfortunately our company have no C-programmers to do the job right, but we
can provide whatever support is needed and make some (reasonable :)
donations if needed. And this will at once make libxslt the fastest
xslt-processing engine, that's quite a good thing by itself. Any ideas?

ps. When it rains it poors, three messages are already here. I hope my
week-old mails will not pop up now... I'm really sorry but I've lost any
hope today :)

On Thu, Jul 26, 2012 at 1:59 AM, Дмитрий Грибов <[email protected]> wrote:

>   Isn’t it a time to do some threading?
>

_______________________________________________
xslt mailing list, project page http://xmlsoft.org/XSLT/
[email protected]
https://mail.gnome.org/mailman/listinfo/xslt
Threaded XSLT transformation.pl (application/octet-stream, 2 KB)
use threads;
use strict;

my $MaxThreads = 10;
my $UsedThreads = 0;

sub xsltApplyXSLTTemplate {
  my $node = shift;                       # Add whatever params you need

  my @TransformResult;                    # Here we gonna put our results.
                                          # Must be shared between threads
                                          # for write

  my @Nodes = $node->childNodes();        # We are going to recurse on them,
                                          # whoever they are

  my $NodesToProcess = scalar(@Nodes);    # How many childs do we posess?

  push @TransformResult, 'smth at start'; # We can't delegate everything,
                                          # here is the job we are doing\
                                          # ourselves

  my $i = 1;                              # 1 because we have done something
  for my $Child (@Nodes){
    if ($UsedThreads++ < $MaxThreads - 1  # If we haven't reached threadslimit
          && $i < $NodesToProcess - 1){   # limit and there is more, than one
      $TransformResult[$i] = undef;       # node left, we make new thread
                                          # note $UsedThreads write-lock on
                                          # check

      threads->create(sub{$TransformResult[$i] =
                          xsltApplyXSLTTemplate($Child)});

    } else {                              # if we only have one node to proc,
      $UsedThreads--;                     # we are doing it here. Same if
                                          # threads limit reached

      $TransformResult[$i] = xsltApplyXSLTTemplate($Child);
    }
    $i++;
  }
  my $AllChildsAreDone = 0;               # Now we make sure all threads
  do {                                    # finished their jobs
    $AllChildsAreDone = 1;
    for (@TransformResult){
      $AllChildsAreDone = 0 if !defined($_);
    }
  } while (!$AllChildsAreDone);

  push @TransformResult, 'something at end';

  return @TransformResult;                # that's it - we are done.
}