How to optimize smart match operator in perl5 interpreter.
This entry is just thinking note for me.
% perl -MO=Terse -e '1 ~~ 2'
LISTOP (0x11ba080) leave [1]
OP (0x11ab4f0) enter
COP (0x11b9c30) nextstate
BINOP (0x11ba150) smartmatch
SVOP (0x11ba3e0) const IV (0x1195328) 1
SVOP (0x11ba290) const IV (0x11b0890) 2
% perl -MO=Terse -E 'given(1) { when (2) { } }'
LISTOP (0x1171d30) leave [1]
OP (0x105e0e0) enter
COP (0x11a2970) nextstate
UNOP (0x1171db0) leavegiven
LOGOP (0x1171d70) entergiven [1]
SVOP (0x105df70) const IV (0x11a3c28) 1
LISTOP (0x1171df0) scope
OP (0x10ff2e0) null [177]
UNOP (0x1171e30) leavewhen
LOGOP (0x1171ea0) enterwhen
BINOP (0x1172010) smartmatch
OP (0x11a1880) padsv [1]
SVOP (0x11a28f0) const IV (0x117c750) 2
LISTOP (0x11a28b0) scope
OP (0x11a1940) stub
OP (0x1065770) break
I think following heuristic knowledge.
- usage of smart match operator is mostly given-when.
- the argument of when () is mostly constants.
- then, the right operand of smart match operator is mostly constant.
If perl5 adds optimized pp_* such as pp_smartmatch_right_operand_is_array, given-when will even fast.
And, I think following four op codes might be useful.
- pp_smartmatch_arayref
- pp_smartmatch_scalar
- pp_smartmatch_refgexp
- pp_smartmatch_undef
「smart match operator の右辺値はほとんどの場合で定数値」だとおもう。
よって、その知識をいかして最適化したらどうかなーとおもったという話。
いろいろな種類の型にあわせて処理しなきゃいけないのが遅いという面があるけど、右辺値が定数であるというアノテーションがあれば、もろもろの処理をとばせるので速いとおもう。
でもいろいろめんどくさいので、実装するのはやめた。けどせっかく思いついたのでメモしといたという具合。
Published: 2010-02-21(Sun) 11:45