perl5 icon indicating copy to clipboard operation
perl5 copied to clipboard

Dedicated SV copying code in place of Perl_sv_setsv_flags

Open richardleach opened this issue 8 months ago • 2 comments

Perl_sv_setsv_flags is the heavyweight function for assigning the value(s) of a source SV to a destination SV. It contains many branches for preparing the destination SV prior to assignment. However:

  • If the destination SV has just been created, much of that logic isn't needed.
  • When cloning a SV, simple assignments (particularly IVs and PVs) dominate.

This set of commits:

  • Extracts the "is this CoWable?" test from Perl_sv_setsv_flags into a macro.
  • Adds Perl_sv_freshcopy_flags and two static helper functions.
  • Modifies Perl_newSVsv_flags and Perl_sv_mortalcopy_flags to use them.
  • Standardizes a number of call sites that did their own things but really should use Perl_newSVsv_flags or Perl_sv_mortalcopy_flags.

Using perl's test harness as a guide:

  • Bodyless code handles 45% of calls to Perl_newSVsv_flags and 57% of calls to Perl_sv_mortalcopy_flags.
  • The SVt_PV/SVp_POK code handles 32% of calls to Perl_newSVsv_flags and 36% of calls to Perl_sv_mortalcopy_flags.
  • S_sv_freshcopy_flags code handles 95% of the remainder in Perl_newSVsv_flags and 91% of the remainder in to Perl_sv_mortalcopy_flags.

With these changes compared with a build of blead:

  • perl -e 'for (1..100_000) { my $x = [ (1) x 1000 ]; }' runs 10% faster

  • perl -e 'for (1..100_000) { my $x = [ ("Perl") x 250 ]; }' runs 45% faster


  • This set of changes does require a perldelta entry and I'll write one post-merge.

richardleach avatar Apr 15 '25 23:04 richardleach

Cloning is rather unfortunate choice of words, given that it has a very specific meaning in our codebase that is quite different from what this PR is about. Renaming the PR may be helpful.

Leont avatar Apr 29 '25 20:04 Leont

I've made a lot of changes following earlier comments - thanks for those - and have finally force-pushed.

These changes aren't complete. For example:

  • Measured performance seems worse than in the PR version, so I need to look into that
  • Might change sflag handling/ SvFLAGS(dsv) setting
  • Not settled on struct membet initialisation
  • Might still rename the function that is currently Perl_newSVsv_flags and have newSVsv_flags be a macro that checks (ssv) before calling the sv.c function.

richardleach avatar May 08 '25 17:05 richardleach

The last commit could use a better commit message than "bulk88 comments" :)

tonycoz avatar Jul 21 '25 05:07 tonycoz

I've squashed recent comment-addressing commits. Will have a final read over the source code comments at the weekend. Hopefully then, apart from any clarifications on the open conversations or further review comments, this will be ready to merge.

richardleach avatar Jul 25 '25 00:07 richardleach

Rebased to clear merge conflicts.

richardleach avatar Jul 28 '25 22:07 richardleach

Rebased for perldelta conflict.

Ready for any final reviews.

richardleach avatar Aug 09 '25 23:08 richardleach

the last commit's message indicates it should be squashed

tonycoz avatar Aug 13 '25 01:08 tonycoz

Thanks, I've hopefully addressed those comments and squashed all trailing commits.

richardleach avatar Aug 13 '25 20:08 richardleach

Many thanks for all the reviews @tonycoz & @bulk88 - it's a relief to finally get this in before the github-actions bot adds the hasConflicts flag again!

richardleach avatar Aug 23 '25 16:08 richardleach