Skip to content

Commit

Permalink
optimize sort by inlining comparison functions
Browse files Browse the repository at this point in the history
This makes special-cased forms such as sort { $b <=> $a }
even faster.

Also, since this commit removes PL_sort_RealCmp, it fixes the
issue with nested sort calls mentioned in gh Perl#16129
  • Loading branch information
xenu committed Mar 9, 2020
1 parent 982bab8 commit 4051b1c
Show file tree
Hide file tree
Showing 7 changed files with 353 additions and 70 deletions.
21 changes: 15 additions & 6 deletions embed.fnc
Original file line number Diff line number Diff line change
Expand Up @@ -2879,17 +2879,26 @@ i |HV* |opmethod_stash |NN SV* meth
#endif

#if defined(PERL_IN_PP_SORT_C)
S |I32 |sv_ncmp |NN SV *const a|NN SV *const b
S |I32 |sv_i_ncmp |NN SV *const a|NN SV *const b
S |I32 |amagic_ncmp |NN SV *const a|NN SV *const b
S |I32 |amagic_i_ncmp |NN SV *const a|NN SV *const b
S |I32 |amagic_cmp |NN SV *const str1|NN SV *const str2
I |I32 |sv_ncmp |NN SV *const a|NN SV *const b
I |I32 |sv_ncmp_desc |NN SV *const a|NN SV *const b
I |I32 |sv_i_ncmp |NN SV *const a|NN SV *const b
I |I32 |sv_i_ncmp_desc |NN SV *const a|NN SV *const b
I |I32 |amagic_ncmp |NN SV *const a|NN SV *const b
I |I32 |amagic_ncmp_desc |NN SV *const a|NN SV *const b
I |I32 |amagic_i_ncmp |NN SV *const a|NN SV *const b
I |I32 |amagic_i_ncmp_desc |NN SV *const a|NN SV *const b
I |I32 |amagic_cmp |NN SV *const str1|NN SV *const str2
I |I32 |amagic_cmp_desc |NN SV *const str1|NN SV *const str2
I |I32 |cmp_desc |NN SV *const str1|NN SV *const str2
# ifdef USE_LOCALE_COLLATE
S |I32 |amagic_cmp_locale|NN SV *const str1|NN SV *const str2
I |I32 |amagic_cmp_locale |NN SV *const str1|NN SV *const str2
I |I32 |amagic_cmp_locale_desc|NN SV *const str1|NN SV *const str2
I |I32 |cmp_locale_desc|NN SV *const str1|NN SV *const str2
# endif
S |I32 |sortcv |NN SV *const a|NN SV *const b
S |I32 |sortcv_xsub |NN SV *const a|NN SV *const b
S |I32 |sortcv_stacked |NN SV *const a|NN SV *const b
I |void |sortsv_flags_impl |NULLOK SV** array|size_t num_elts|NN SVCOMPARE_t cmp|U32 flags
#endif

#if defined(PERL_IN_PP_SYS_C)
Expand Down
9 changes: 9 additions & 0 deletions embed.h
Original file line number Diff line number Diff line change
Expand Up @@ -1842,15 +1842,24 @@
# endif
# if defined(PERL_IN_PP_SORT_C)
#define amagic_cmp(a,b) S_amagic_cmp(aTHX_ a,b)
#define amagic_cmp_desc(a,b) S_amagic_cmp_desc(aTHX_ a,b)
#define amagic_i_ncmp(a,b) S_amagic_i_ncmp(aTHX_ a,b)
#define amagic_i_ncmp_desc(a,b) S_amagic_i_ncmp_desc(aTHX_ a,b)
#define amagic_ncmp(a,b) S_amagic_ncmp(aTHX_ a,b)
#define amagic_ncmp_desc(a,b) S_amagic_ncmp_desc(aTHX_ a,b)
#define cmp_desc(a,b) S_cmp_desc(aTHX_ a,b)
#define sortcv(a,b) S_sortcv(aTHX_ a,b)
#define sortcv_stacked(a,b) S_sortcv_stacked(aTHX_ a,b)
#define sortcv_xsub(a,b) S_sortcv_xsub(aTHX_ a,b)
#define sortsv_flags_impl(a,b,c,d) S_sortsv_flags_impl(aTHX_ a,b,c,d)
#define sv_i_ncmp(a,b) S_sv_i_ncmp(aTHX_ a,b)
#define sv_i_ncmp_desc(a,b) S_sv_i_ncmp_desc(aTHX_ a,b)
#define sv_ncmp(a,b) S_sv_ncmp(aTHX_ a,b)
#define sv_ncmp_desc(a,b) S_sv_ncmp_desc(aTHX_ a,b)
# if defined(USE_LOCALE_COLLATE)
#define amagic_cmp_locale(a,b) S_amagic_cmp_locale(aTHX_ a,b)
#define amagic_cmp_locale_desc(a,b) S_amagic_cmp_locale_desc(aTHX_ a,b)
#define cmp_locale_desc(a,b) S_cmp_locale_desc(aTHX_ a,b)
# endif
# endif
# if defined(PERL_IN_PP_SYS_C)
Expand Down
1 change: 0 additions & 1 deletion embedvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@
#define PL_sighandlerp (vTHX->Isighandlerp)
#define PL_signalhook (vTHX->Isignalhook)
#define PL_signals (vTHX->Isignals)
#define PL_sort_RealCmp (vTHX->Isort_RealCmp)
#define PL_sortcop (vTHX->Isortcop)
#define PL_sortstash (vTHX->Isortstash)
#define PL_splitstr (vTHX->Isplitstr)
Expand Down
2 changes: 0 additions & 2 deletions intrpvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -793,8 +793,6 @@ PERLVARI(I, known_layers, PerlIO_list_t *, NULL)
PERLVARI(I, def_layerlist, PerlIO_list_t *, NULL)
#endif

PERLVAR(I, sort_RealCmp, SVCOMPARE_t)

PERLVARI(I, checkav_save, AV *, NULL) /* save CHECK{}s when compiling */
PERLVARI(I, unitcheckav_save, AV *, NULL)
/* save UNITCHECK{}s when compiling */
Expand Down
Loading

0 comments on commit 4051b1c

Please sign in to comment.