package Bencher::Scenario::Accessors::Set;

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2021-08-03'; # DATE
our $DIST = 'Bencher-Scenarios-Accessors'; # DIST
our $VERSION = '0.150'; # VERSION

use 5.010001;
use strict;
use warnings;

use Bencher::ScenarioUtil::Accessors;

my $classes = \%Bencher::ScenarioUtil::Accessors::classes;

our $scenario = {
    summary => 'Benchmark attribute write/set',
    modules => {
        # include the generator modules here so we can show their versions in
        # sample benchmark results produced by PWP:Bencher::Scenario
        (map { $_=>0 } grep {defined} map { $classes->{$_}{generator} }
             keys %$classes),
    },
    participants => [
        (map {
            my $spec = $classes->{$_};
            +{
                name => $spec->{generator} || $spec->{name},
                module => $_,
                code_template => "state \$o = do { my \$o = ${_}->new; \$o }; \$o->".($spec->{setter_name} // "attr1")."(42)",
            };
        } grep { !$classes->{$_}{immutable} && ($classes->{$_}{supports_setters} // 1) } keys %$classes),

        # also compare with raw hash & array access
        {
            name => 'raw hash access',
            module => 'Perl::Examples::Accessors::Hash',
            code_template => "state \$o = do { my \$o = Perl::Examples::Accessors::Hash->new; \$o }; \$o->{attr1} = 42",
        },
        {
            name => 'raw array access',
            module => 'Perl::Examples::Accessors::Array',
            code_template => "state \$o = do { my \$o = Perl::Examples::Accessors::Array->new; \$o }; \$o->[0] = 42",
        },
    ],
};

1;
# ABSTRACT: Benchmark attribute write/set

__END__

=pod

=encoding UTF-8

=head1 NAME

Bencher::Scenario::Accessors::Set - Benchmark attribute write/set

=head1 VERSION

This document describes version 0.150 of Bencher::Scenario::Accessors::Set (from Perl distribution Bencher-Scenarios-Accessors), released on 2021-08-03.

=head1 SYNOPSIS

To run benchmark with default option:

 % bencher -m Accessors::Set

To run module startup overhead benchmark:

 % bencher --module-startup -m Accessors::Set

For more options (dump scenario, list/include/exclude/add participants, list/include/exclude/add datasets, etc), see L<bencher> or run C<bencher --help>.

=head1 DESCRIPTION

Packaging a benchmark script as a Bencher scenario makes it convenient to include/exclude/add participants/datasets (either via CLI or Perl code), send the result to a central repository, among others . See L<Bencher> and L<bencher> (CLI) for more details.

=head1 BENCHMARKED MODULES

Version numbers shown below are the versions used when running the sample benchmark.

L<Class::Accessor> 0.51

L<Class::Accessor::Array> 0.032

L<Class::Accessor::PackedString> 0.001

L<Class::Accessor::PackedString::Set> 0.001

L<Class::InsideOut> 1.14

L<Class::Struct> 0.66

L<Class::Tiny> 1.008

L<Class::XSAccessor> 1.19

L<Class::XSAccessor::Array> 1.19

L<Mo> 0.40

L<Mojo::Base>

L<Mojo::Base::XS> 0.07

L<Moo> 2.004004

L<Moops> 0.038

L<Moos> 0.30

L<Moose> 2.2015

L<Mouse> v2.5.10

L<Object::Pad> 0.46

L<Object::Simple> 3.19

L<Object::Tiny> 1.09

L<Object::Tiny::RW> 1.07

L<Object::Tiny::RW::XS> 0.04

L<Object::Tiny::XS> 1.01

L<Perl::Examples::Accessors::Array> 0.132

L<Perl::Examples::Accessors::ClassAccessor> 0.132

L<Perl::Examples::Accessors::ClassAccessorArray> 0.132

L<Perl::Examples::Accessors::ClassAccessorPackedString> 0.132

L<Perl::Examples::Accessors::ClassAccessorPackedStringSet> 0.132

L<Perl::Examples::Accessors::ClassInsideOut> 0.132

L<Perl::Examples::Accessors::ClassStruct> 0.132

L<Perl::Examples::Accessors::ClassTiny> 0.132

L<Perl::Examples::Accessors::ClassXSAccessor> 0.132

L<Perl::Examples::Accessors::ClassXSAccessorArray> 0.132

L<Perl::Examples::Accessors::Hash> 0.132

L<Perl::Examples::Accessors::Mo> 0.132

L<Perl::Examples::Accessors::MojoBase> 0.132

L<Perl::Examples::Accessors::MojoBaseXS> 0.132

L<Perl::Examples::Accessors::Moo> 0.132

L<Perl::Examples::Accessors::Moops> 0.132

L<Perl::Examples::Accessors::Moos> 0.132

L<Perl::Examples::Accessors::Moose> 0.132

L<Perl::Examples::Accessors::Mouse> 0.132

L<Perl::Examples::Accessors::ObjectPad> 0.132

L<Perl::Examples::Accessors::ObjectSimple> 0.132

L<Perl::Examples::Accessors::ObjectTinyRW> 0.132

L<Perl::Examples::Accessors::ObjectTinyRWXS> 0.132

L<Perl::Examples::Accessors::SimpleAccessor> 0.132

L<Simple::Accessor> 1.13

=head1 BENCHMARK PARTICIPANTS

=over

=item * Class::Accessor::PackedString (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::ClassAccessorPackedString->new; $o }; $o->attr1(42)



=item * Class::Accessor::Array (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::ClassAccessorArray->new; $o }; $o->attr1(42)



=item * Mojo::Base (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::MojoBase->new; $o }; $o->attr1(42)



=item * Simple::Accessor (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::SimpleAccessor->new; $o }; $o->attr1(42)



=item * Object::Tiny::RW (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::ObjectTinyRW->new; $o }; $o->attr1(42)



=item * Moose (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::Moose->new; $o }; $o->attr1(42)



=item * Moos (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::Moos->new; $o }; $o->attr1(42)



=item * Object::Pad (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::ObjectPad->new; $o }; $o->set_attr1(42)



=item * Mouse (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::Mouse->new; $o }; $o->attr1(42)



=item * Mo (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::Mo->new; $o }; $o->attr1(42)



=item * Object::Simple (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::ObjectSimple->new; $o }; $o->attr1(42)



=item * Moops (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::Moops->new; $o }; $o->attr1(42)



=item * Class::Struct (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::ClassStruct->new; $o }; $o->attr1(42)



=item * Class::XSAccessor (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::ClassXSAccessor->new; $o }; $o->attr1(42)



=item * Object::Tiny::RW::XS (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::ObjectTinyRWXS->new; $o }; $o->attr1(42)



=item * no generator (array-based) (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::Array->new; $o }; $o->attr1(42)



=item * Moo (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::Moo->new; $o }; $o->attr1(42)



=item * Class::InsideOut (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::ClassInsideOut->new; $o }; $o->attr1(42)



=item * Mojo::Base::XS (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::MojoBaseXS->new; $o }; $o->attr1(42)



=item * Class::Accessor (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::ClassAccessor->new; $o }; $o->attr1(42)



=item * Class::Tiny (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::ClassTiny->new; $o }; $o->attr1(42)



=item * no generator (hash-based) (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::Hash->new; $o }; $o->attr1(42)



=item * Class::Accessor::PackedString::Set (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::ClassAccessorPackedStringSet->new; $o }; $o->attr1(42)



=item * Class::XSAccessor::Array (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::ClassXSAccessorArray->new; $o }; $o->attr1(42)



=item * raw hash access (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::Hash->new; $o }; $o->{attr1} = 42



=item * raw array access (perl_code)

Code template:

 state $o = do { my $o = Perl::Examples::Accessors::Array->new; $o }; $o->[0] = 42



=back

=head1 BENCHMARK SAMPLE RESULTS

=head2 Sample benchmark #1

Run on: perl: I<< v5.34.0 >>, CPU: I<< Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz (4 cores) >>, OS: I<< GNU/Linux LinuxMint version 19 >>, OS kernel: I<< Linux version 5.3.0-68-generic >>.

Benchmark command (default options):

 % bencher -m Accessors::Set

Result formatted as table:

 #table1#
 +------------------------------------+-----------+-----------+-----------------------+-----------------------+---------+---------+
 | participant                        | rate (/s) | time (ns) | pct_faster_vs_slowest | pct_slower_vs_fastest |  errors | samples |
 +------------------------------------+-----------+-----------+-----------------------+-----------------------+---------+---------+
 | Simple::Accessor                   |    922000 |   1090    |                 0.00% |              2929.69% | 4.1e-10 |      20 |
 | Class::Accessor::PackedString::Set |   1730000 |    579    |                87.44% |              1516.37% | 2.5e-10 |      20 |
 | Class::Accessor::PackedString      |   2380000 |    419    |               158.70% |              1071.14% | 1.5e-10 |      31 |
 | Class::Accessor                    |   2590000 |    386    |               181.28% |               977.11% | 9.8e-11 |      20 |
 | Class::InsideOut                   |   3400000 |    290    |               269.54% |               719.86% | 3.1e-10 |      20 |
 | Object::Pad                        |   4640000 |    216    |               403.39% |               501.86% | 2.9e-11 |      24 |
 | Moose                              |   5120000 |    195    |               455.28% |               445.61% | 5.2e-11 |      20 |
 | Object::Tiny::RW                   |   5270000 |    190    |               472.33% |               429.36% |   7e-11 |      20 |
 | Class::Struct                      |   5390000 |    186    |               484.42% |               418.41% | 1.5e-10 |      20 |
 | Class::Accessor::Array             |   5800000 |    170    |               526.56% |               383.54% | 1.7e-10 |      20 |
 | Mojo::Base                         |   5900000 |    170    |               537.32% |               375.38% | 3.1e-10 |      20 |
 | Mo                                 |   6400000 |    160    |               599.49% |               333.13% | 4.1e-10 |      21 |
 | Object::Simple                     |   6600000 |    150    |               611.69% |               325.70% | 2.4e-10 |      20 |
 | Class::Tiny                        |   6700000 |    150    |               625.50% |               317.60% | 2.1e-10 |      20 |
 | no generator (hash-based)          |   7000000 |    140    |               655.64% |               300.94% | 2.1e-10 |      24 |
 | no generator (array-based)         |   7480000 |    134    |               711.82% |               273.20% | 9.8e-11 |      20 |
 | Mouse                              |  11200000 |     89.28 |              1115.36% |               149.28% | 5.5e-12 |      25 |
 | Moops                              |  11000000 |     88    |              1134.75% |               145.37% | 2.2e-10 |      20 |
 | Moos                               |  12000000 |     81    |              1237.15% |               126.58% | 2.2e-10 |      20 |
 | Object::Tiny::RW::XS               |  13000000 |     77    |              1310.03% |               114.87% | 2.4e-10 |      21 |
 | Mojo::Base::XS                     |  13500000 |     74.3  |              1360.26% |               107.48% |   7e-11 |      20 |
 | Moo                                |  13500000 |     74    |              1366.84% |               106.54% | 5.7e-11 |      20 |
 | Class::XSAccessor                  |  15000000 |     68    |              1497.85% |                89.61% | 1.6e-10 |      20 |
 | Class::XSAccessor::Array           |  17000000 |     58    |              1771.16% |                61.91% | 2.1e-10 |      20 |
 | raw hash access                    |  25000000 |     41    |              2579.11% |                13.09% | 2.4e-10 |      20 |
 | raw array access                   |  28000000 |     36    |              2929.69% |                 0.00% |   2e-10 |      26 |
 +------------------------------------+-----------+-----------+-----------------------+-----------------------+---------+---------+


The above result formatted in L<Benchmark.pm|Benchmark> style:

                                            Rate  Simple::Accessor  Class::Accessor::PackedString::Set  Class::Accessor::PackedString  Class::Accessor  Class::InsideOut  Object::Pad  Moose  Object::Tiny::RW  Class::Struct  Class::Accessor::Array  Mojo::Base    Mo  Object::Simple  Class::Tiny  no generator (hash-based)  no generator (array-based)  Mouse  Moops  Moos  Object::Tiny::RW::XS  Mojo::Base::XS   Moo  Class::XSAccessor  Class::XSAccessor::Array  raw hash access  raw array access 
  Simple::Accessor                      922000/s                --                                -46%                           -61%             -64%              -73%         -80%   -82%              -82%           -82%                    -84%        -84%  -85%            -86%         -86%                       -87%                        -87%   -91%   -91%  -92%                  -92%            -93%  -93%               -93%                      -94%             -96%              -96% 
  Class::Accessor::PackedString::Set   1730000/s               88%                                  --                           -27%             -33%              -49%         -62%   -66%              -67%           -67%                    -70%        -70%  -72%            -74%         -74%                       -75%                        -76%   -84%   -84%  -86%                  -86%            -87%  -87%               -88%                      -89%             -92%              -93% 
  Class::Accessor::PackedString        2380000/s              160%                                 38%                             --              -7%              -30%         -48%   -53%              -54%           -55%                    -59%        -59%  -61%            -64%         -64%                       -66%                        -68%   -78%   -78%  -80%                  -81%            -82%  -82%               -83%                      -86%             -90%              -91% 
  Class::Accessor                      2590000/s              182%                                 50%                             8%               --              -24%         -44%   -49%              -50%           -51%                    -55%        -55%  -58%            -61%         -61%                       -63%                        -65%   -76%   -77%  -79%                  -80%            -80%  -80%               -82%                      -84%             -89%              -90% 
  Class::InsideOut                     3400000/s              275%                                 99%                            44%              33%                --         -25%   -32%              -34%           -35%                    -41%        -41%  -44%            -48%         -48%                       -51%                        -53%   -69%   -69%  -72%                  -73%            -74%  -74%               -76%                      -80%             -85%              -87% 
  Object::Pad                          4640000/s              404%                                168%                            93%              78%               34%           --    -9%              -12%           -13%                    -21%        -21%  -25%            -30%         -30%                       -35%                        -37%   -58%   -59%  -62%                  -64%            -65%  -65%               -68%                      -73%             -81%              -83% 
  Moose                                5120000/s              458%                                196%                           114%              97%               48%          10%     --               -2%            -4%                    -12%        -12%  -17%            -23%         -23%                       -28%                        -31%   -54%   -54%  -58%                  -60%            -61%  -62%               -65%                      -70%             -78%              -81% 
  Object::Tiny::RW                     5270000/s              473%                                204%                           120%             103%               52%          13%     2%                --            -2%                    -10%        -10%  -15%            -21%         -21%                       -26%                        -29%   -53%   -53%  -57%                  -59%            -60%  -61%               -64%                      -69%             -78%              -81% 
  Class::Struct                        5390000/s              486%                                211%                           125%             107%               55%          16%     4%                2%             --                     -8%         -8%  -13%            -19%         -19%                       -24%                        -27%   -52%   -52%  -56%                  -58%            -60%  -60%               -63%                      -68%             -77%              -80% 
  Class::Accessor::Array               5800000/s              541%                                240%                           146%             127%               70%          27%    14%               11%             9%                      --          0%   -5%            -11%         -11%                       -17%                        -21%   -47%   -48%  -52%                  -54%            -56%  -56%               -60%                      -65%             -75%              -78% 
  Mojo::Base                           5900000/s              541%                                240%                           146%             127%               70%          27%    14%               11%             9%                      0%          --   -5%            -11%         -11%                       -17%                        -21%   -47%   -48%  -52%                  -54%            -56%  -56%               -60%                      -65%             -75%              -78% 
  Mo                                   6400000/s              581%                                261%                           161%             141%               81%          35%    21%               18%            16%                      6%          6%    --             -6%          -6%                       -12%                        -16%   -44%   -44%  -49%                  -51%            -53%  -53%               -57%                      -63%             -74%              -77% 
  Object::Simple                       6600000/s              626%                                286%                           179%             157%               93%          43%    30%               26%            24%                     13%         13%    6%              --           0%                        -6%                        -10%   -40%   -41%  -46%                  -48%            -50%  -50%               -54%                      -61%             -72%              -76% 
  Class::Tiny                          6700000/s              626%                                286%                           179%             157%               93%          43%    30%               26%            24%                     13%         13%    6%              0%           --                        -6%                        -10%   -40%   -41%  -46%                  -48%            -50%  -50%               -54%                      -61%             -72%              -76% 
  no generator (hash-based)            7000000/s              678%                                313%                           199%             175%              107%          54%    39%               35%            32%                     21%         21%   14%              7%           7%                         --                         -4%   -36%   -37%  -42%                  -44%            -46%  -47%               -51%                      -58%             -70%              -74% 
  no generator (array-based)           7480000/s              713%                                332%                           212%             188%              116%          61%    45%               41%            38%                     26%         26%   19%             11%          11%                         4%                          --   -33%   -34%  -39%                  -42%            -44%  -44%               -49%                      -56%             -69%              -73% 
  Mouse                               11200000/s             1120%                                548%                           369%             332%              224%         141%   118%              112%           108%                     90%         90%   79%             68%          68%                        56%                         50%     --    -1%   -9%                  -13%            -16%  -17%               -23%                      -35%             -54%              -59% 
  Moops                               11000000/s             1138%                                557%                           376%             338%              229%         145%   121%              115%           111%                     93%         93%   81%             70%          70%                        59%                         52%     1%     --   -7%                  -12%            -15%  -15%               -22%                      -34%             -53%              -59% 
  Moos                                12000000/s             1245%                                614%                           417%             376%              258%         166%   140%              134%           129%                    109%        109%   97%             85%          85%                        72%                         65%    10%     8%    --                   -4%             -8%   -8%               -16%                      -28%             -49%              -55% 
  Object::Tiny::RW::XS                13000000/s             1315%                                651%                           444%             401%              276%         180%   153%              146%           141%                    120%        120%  107%             94%          94%                        81%                         74%    15%    14%    5%                    --             -3%   -3%               -11%                      -24%             -46%              -53% 
  Mojo::Base::XS                      13500000/s             1367%                                679%                           463%             419%              290%         190%   162%              155%           150%                    128%        128%  115%            101%         101%                        88%                         80%    20%    18%    9%                    3%              --    0%                -8%                      -21%             -44%              -51% 
  Moo                                 13500000/s             1372%                                682%                           466%             421%              291%         191%   163%              156%           151%                    129%        129%  116%            102%         102%                        89%                         81%    20%    18%    9%                    4%              0%    --                -8%                      -21%             -44%              -51% 
  Class::XSAccessor                   15000000/s             1502%                                751%                           516%             467%              326%         217%   186%              179%           173%                    150%        150%  135%            120%         120%                       105%                         97%    31%    29%   19%                   13%              9%    8%                 --                      -14%             -39%              -47% 
  Class::XSAccessor::Array            17000000/s             1779%                                898%                           622%             565%              400%         272%   236%              227%           220%                    193%        193%  175%            158%         158%                       141%                        131%    53%    51%   39%                   32%             28%   27%                17%                        --             -29%              -37% 
  raw hash access                     25000000/s             2558%                               1312%                           921%             841%              607%         426%   375%              363%           353%                    314%        314%  290%            265%         265%                       241%                        226%   117%   114%   97%                   87%             81%   80%                65%                       41%               --              -12% 
  raw array access                    28000000/s             2927%                               1508%                          1063%             972%              705%         500%   441%              427%           416%                    372%        372%  344%            316%         316%                       288%                        272%   148%   144%  125%                  113%            106%  105%                88%                       61%              13%                -- 
 
 Legends:
   Class::Accessor: participant=Class::Accessor
   Class::Accessor::Array: participant=Class::Accessor::Array
   Class::Accessor::PackedString: participant=Class::Accessor::PackedString
   Class::Accessor::PackedString::Set: participant=Class::Accessor::PackedString::Set
   Class::InsideOut: participant=Class::InsideOut
   Class::Struct: participant=Class::Struct
   Class::Tiny: participant=Class::Tiny
   Class::XSAccessor: participant=Class::XSAccessor
   Class::XSAccessor::Array: participant=Class::XSAccessor::Array
   Mo: participant=Mo
   Mojo::Base: participant=Mojo::Base
   Mojo::Base::XS: participant=Mojo::Base::XS
   Moo: participant=Moo
   Moops: participant=Moops
   Moos: participant=Moos
   Moose: participant=Moose
   Mouse: participant=Mouse
   Object::Pad: participant=Object::Pad
   Object::Simple: participant=Object::Simple
   Object::Tiny::RW: participant=Object::Tiny::RW
   Object::Tiny::RW::XS: participant=Object::Tiny::RW::XS
   Simple::Accessor: participant=Simple::Accessor
   no generator (array-based): participant=no generator (array-based)
   no generator (hash-based): participant=no generator (hash-based)
   raw array access: participant=raw array access
   raw hash access: participant=raw hash access

The above result presented as chart:

=begin html

<img src="data:image/png;base64, " />

=end html


To display as an interactive HTML table on a browser, you can add option C<--format html+datatables>.

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/Bencher-Scenarios-Accessors>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-Bencher-Scenarios-Accessors>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Bencher-Scenarios-Accessors>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 SEE ALSO

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2021, 2017, 2016 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
