Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 13 Sep 2024 07:21:02 -0600
From:      Alan Somers <asomers@freebsd.org>
To:        Jason Bacon <bacon4000@gmail.com>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: The Case for Rust (in any system)
Message-ID:  <CAOtMX2j4c9%2Br3vuwhm3k1EL3EL8BLrP8VWxBYmxPnWMvaDWcng@mail.gmail.com>
In-Reply-To: <ce457b53-f1c2-4a95-917a-c17bcb1c81ed@gmail.com>
References:  <CAOzHqcJ0rOR4CoL84WgZQNcgY2G9vuiHccE4XT_otJ2R51KJ3Q@mail.gmail.com> <2EE309BF-CE1D-48AD-9C53-D4C87998B4A0@freebsd.org> <e12a3441-3143-426b-b921-7c3357ed4738@gmail.com> <ce457b53-f1c2-4a95-917a-c17bcb1c81ed@gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, Sep 13, 2024 at 7:15=E2=80=AFAM Jason Bacon <bacon4000@gmail.com> w=
rote:
>
> On 9/13/24 05:33, Paul Floyd wrote:
> >
> >
> > On 13-09-24 06:17, David Chisnall wrote:
> >> On 13 Sep 2024, at 02:34, Joe Schaefer <joesuf4@gmail.com> wrote:
> >>>
> >>> I just completed a month long project to port a C++ codebase that
> >>> used vectors for array allocations back to using C=E2=80=98s calloc. =
For a
> >>> 15% increase in memory footprint, batch jobs that took three days to
> >>> complete now finish in 10-12 hours.
> >>
> >> This sounds highly dubious given that std::vector is a very thin
> >> wrapper around malloc. From your description, I would expect the same
> >> speedup with some judicial use of .reserve().
> >
> > I was going to say exactly the same thing.
> >
> > Considering the reply to this, another one to be plonked so that I wast=
e
> > less time.
> >
> > A+
> > Paul
> >
> >
> >
>
> Some years ago, I wrote a script to time a simple selection sort coded
> in various languages.  Here's an example of the results:
>
> https://github.com/outpaddling/Lang-speed/blob/master/Results/coral-amd64=
-100000
>
> Note: The clang array/pointer performance is currently regressed due to
> changes in the optimization parameters since clang 8.  That's why it's
> noticeably slower than GCC in these results.
>
> https://github.com/llvm/llvm-project/issues/53205#issuecomment-2318697322
>
> In general, I have not seen a significant difference between arrays and
> vectors in all my years running this benchmark.
>
> This benchmark is anecdotal, as it only measures performance for one
> algorithm.  But in my experience, C++ shows marginally slower
> performance and noticeably more memory use than C.
>
> Coming back to Rust: The results above, showing about double the runtime
> of C and C++, is the best I've seen from it.  It was taking 4x as long
> as C/C++ a few years ago.  That's one reason I don't use it.  I do
> mostly scientific computing, where runtime can be costly.  This is not
> *always* an issue in systems code, but it should be examined before
> choosing a language for a particular implementation.  The other reason
> is the impact of a Rust dependency on FreeBSD ports and pkgsrc packages:
> Frequent changes to the Rust port/package lead to long build times and
> frequent breakage of dependents.

Right away, I can see that while your C program mallocs the array to
the list's full size, your Rust program doesn't.  It grows the list
one element at a time, with Vec::push .  I bet that if you change the
Rust program to use Vec::reserve or Vec::with_capacity, so that it
only has to allocate once, you'll find the results are different.
-Alan



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