Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 6 Jan 2019 10:17:04 -0700
From:      Alan Somers <asomers@freebsd.org>
To:        Brian Neal <brian@aceshardware.com>
Cc:        "freebsd-hackers@freebsd.org" <freebsd-hackers@freebsd.org>, Garance A Drosehn <drosih@rpi.edu>
Subject:   Re: Speculative: Rust for base system components
Message-ID:  <CAOtMX2jOeAZfpyH1S1N1euAQQZe00nA9JQNjY2qnRB%2BczS9O2g@mail.gmail.com>
In-Reply-To: <26325c0b-4960-7739-72aa-c31c4e0638d3@aceshardware.com>
References:  <ca76e5f7-6e59-bd67-144a-90ad66f0252e@metricspace.net> <7d7bc47d-04cf-2f9b-00a3-e3d9d92b3623@aceshardware.com> <72922F2C-9D27-47AA-BB1C-2DA8589CF008@rpi.edu> <92bd5362-d898-aa12-8f3d-9fbe23f38e0c@aceshardware.com> <CAOtMX2gPrpO2O70pnwpU80bXMXo6LB9PCkU3bYw2ToMMnhf%2Bww@mail.gmail.com> <26325c0b-4960-7739-72aa-c31c4e0638d3@aceshardware.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, Jan 6, 2019 at 9:54 AM Brian Neal <brian@aceshardware.com> wrote:
>
> I don't have it anymore, but yours is close enough.  When compiling your
> first example, I get 156 instructions, including map iterators and rust
> result assert/unwrap logic.  Your second example produces 21 lines,
> including a loop iterator:
>
> rustc 1.31.0 with -C opt-level=s:
>
> <I as core::iter::traits::IntoIterator>::into_iter:
>          mov     eax, edi
>          mov     edx, esi
>          ret
> example::oddcount2:
>          xor     eax, eax
>          test    edi, edi
>          jle     .LBB1_3
>          xor     ecx, ecx
> .LBB1_2:
>          mov     edx, ecx
>          and     edx, 1
>          neg     edx
>          and     edx, ecx
>          lea     esi, [rcx + 1]
>          add     eax, edx
>          mov     ecx, esi
>          cmp     edi, esi
>          jne     .LBB1_2
> .LBB1_3:
>          ret

Those 21 lines aren't 21 instructions; you're counting labels.  Also,
the first three instructions aren't actually part of the function.
They're dead code, and should be eliminate by LTO.  However, Rust
doesn't do LTO when compiling libraries; only when linking
executables.  The unwrap logic, etc is also not part of the function.
So in this example, Rust produces only a few more instructions than C.
Also, FYI the Rust expression "0..num" is exclusive on the right.
It's equivalent to C's "for (int i = 0; i < num; i++)", though that
doesn't change the instruction count.

-Alan

>
> Converting to C:
>
> int oddcount2(int num) {
>      int sum = 0;
>      for (int i = 0; i <= num; ++i)
>      {
>          if (i % 2 > 0)
>          {
>              sum += i;
>          }
>      }
>      return sum;
> }
>
> I get the following with gcc 8.2 using -Os:
>
> oddcount2(int):
>          xor     edx, edx
>          xor     eax, eax
> .L4:
>          cmp     edx, edi
>          jg      .L1
>          test    dl, 1
>          je      .L3
>          add     eax, edx
> .L3:
>          inc     edx
>          jmp     .L4
> .L1:
>          ret
>
> Cheers,
>
> -Brian
>
> On 1/6/2019 8:01 AM, Alan Somers wrote:
> > I get different results on Godbolt.  I don't know exactly what your
> > program was, but I tried to recreate it from your description.  I
> > wrote it in two ways.  With opt-level=s, I got 13 instructions for one
> > version, and 16 for the other.  With opt-level=3, I got vectorized
> > code for both.  Here's my code:
> >
> > pub fn oddcount(num: i32) -> i32 {
> >      (0..num).map(|i| {
> >          if i % 2 > 0 {
> >              i
> >          } else {
> >              0
> >          }
> >      }).sum()
> > }
> >
> > pub fn oddcount2(num: i32) -> i32 {
> >      let mut sum = 0;
> >      for i in 0..num {
> >          if i % 2 > 0 {
> >              sum += i;
> >          }
> >      }
> >      sum
> > }
> >
> > -Alan
> > _______________________________________________
> > freebsd-hackers@freebsd.org mailing list
> > https://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> > To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"
> >



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAOtMX2jOeAZfpyH1S1N1euAQQZe00nA9JQNjY2qnRB%2BczS9O2g>