in reply to Optimizing non-alphanumeric method calls

Well you asked for non alphanumeric method names but only showed benchmarks and code for normal method names.

It's hard to tell what you really need.

2 ideas

1. You didn't try the option $obj->$c_method with $c_method being a code ref. ( that's what can() returns ) ... this should be fastest after a caching phase along with direct.

2. If your method names are automatically generated try to convert non identifier into some kind of translation.

Have a look at control characters like ^c and so on.

Perl allows them for variables, maybe for method names too?

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re^2: Optimizing non-alphanumeric method calls
by trizen (Hermit) on Sep 27, 2015 at 18:55 UTC

    Thanks for your reply.

    $obj->$c_method is indeed faster, but it cannot be used, unfortunately, on chained method calls, such as $obj->$method_1->$method_2, unless the result returned by $obj->$method_1 is stored somewhere, which I guess it may slow things down quite a bit.

    Regarding your second idea, I haven't tried control characters as method names yet, but it's a good idea. However, I'm not sure how safe it is to rely on this behavior as it may get deprecated in the future and forbidden by Perl (assuming that it currently works).

      >  but it cannot be used, unfortunately, on chained method calls, such as $obj->$method_1->$method_2, unless the result returned by $obj->$method_1 is stored somewhere, which I guess it may slow things down quite a bit. 

      I'm not sure what you mean, $c_method isn't different in this respect to the other approaches you have proposed.

      For chained method calls you need to return the object in intermediate calls, no matter which approach you use.

      Maybe you should show us some real code to make things clearer.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        Here is a real code translation of "(1+7)/4":

        use constant NUM23343688 => Sidef::Types::Number::Number->new('1'); use constant NUM25174720 => Sidef::Types::Number::Number->new('7'); use constant NUM24946728 => Sidef::Types::Number::Number->new('4'); ((main::NUM23343688)->${\'+'}(main::NUM25174720))->${\'/'}(main::NUM24 +946728);

        Note that currently I'm using the reference-dereference technique for calling non-alphanumeric methods. What I'm really looking for, it's an way to avoid this inefficient approach without any other trade-offs.

        Maybe you should show us some real code to make things clearer.

        trizen has done what we ask of nearly every first-time poster. He has prepared a complete executable example. I feel it would be far better to suggest improvements to the example than to discourage this style of question.

        Bill