►
Description
The Importance of Not Over-Optimizing in Rust by Lily Mara
The Rust programming language has a lot of advanced features that enable some fantastic optimizations. For new Rust developers these can feel both exciting and constricting. Because Rust allows us to write highly-optimized code that we as experienced Rust developers can understand, it's tempting to do this all the time.
In this talk I will show that these optimizations are often unnecessary to get performance that beats highly dynamic languages like Python. For new Rust developers, breaking the temptation to over-optimize can lead to higher productivity and satisfaction with Rust.
A
Hi,
my
name
is
lily.
I've
been
using
rust
for
side
projects
since
2015
and
professionally
at
one
signal
since
2019.,
I'd
like
to
tell
a
short
story
about
a
new
rust
user
who
runs
into
some
stumbling
blocks
during
their
learning
journey
and
use
that
to
jump
into
a
discussion
about
how
the
community
can
better
serve
users
like
them
meet
jean
jean
is
a
python
developer.
Who's
been
writing
software
for
several
years.
They're
working
on
some
json
aggregation
code
in
python
and
they've
noticed
that
it's
a
performance
bottleneck
for
their
application.
A
A
The
experienced
developer
tells
gene
that
the
code
is
okay,
but
it
has
some
unnecessary
allocations
in
it.
It
uses
an
own
string
on
the
data
struct.
It
could
be
improved
with
the
addition
of
lifetimes.
Cere
has
support
for
deserializing
borrowed
strings
instead
of
own
strings,
so
you
can
save
on
a
lot
of
allocations.
They
tell
gene
since
they're
excited
about
the
prospect
of
writing
high
performance
code.
Gene
leaves
this
conversation
with
the
motivation
to
dive
deep.
A
They
haven't
used
lifetimes
yet,
but
they
do
a
lot
of
reading
to
try
and
figure
it
out
after
a
lot
of
time
and
frustration.
They
have
a
solution
that
should
be
much
more
performant
they've
added
a
lifetime
to
the
data
struct.
That
certainly
is
using
for
parsing
gene
benchmarks,
the
performance
of
the
new
borrowing
version
against
the
original
owning
version.
They
run
both
of
them
against
1000
lines
of
json.
A
It
seems
like
they
made
some
meaningful
performance
improvements.
The
bars
here
represent
the
runtimes
of
the
solutions
relative
to
the
fastest
version.
The
fastest
version
is
the
rust
code
using
references
and
the
slower
version
is
the
rust
code
using
own
strings.
This
runs
in
around
1.5
x.
The
time
of
the
faster
version
out
of
curiosity.
A
Oh
after
seeing
these
benchmarking
results,
gene
is
a
bit
confused.
They
just
wanted
to
write
something
that
was
faster
than
python.
After
all,
their
original
slow
version
is
nearly
20
times
faster
than
the
python
code,
so
why
did
they
have
to
spend
all
that
time?
Learning
these
new
topics
now
it
may
not
seem
like
it
if
you've
been
writing
rust
for
a
while,
but
there
are
a
lot
of
new
ideas
hiding
in
this
struct
definition.
A
A
A
This
is
also
an
extremely
simplified
example
where
gene
parses
the
data
into
a
struct
and
immediately
uses
and
discards
it.
If
this
were
a
larger
example,
they
would
need
to
add
lifetime
annotations
to
function,
parameters
and
return,
types
info
blocks
and
add
them
to
any
structs
which
contain
instances
of
data
they
may
spend
a
long
time.
Threading.
A
A
A
These
borrows
can
be
added
on
when
it's
really
necessary.
Now
an
experienced
developer
might
be
able
to
determine
when
and
where
these
kind
of
borrows
can
be
effectively
used
without
having
large
effects
on
the
program's
complexity.
A
newer
developer
might
try
to
add
lifetimes
and
borrows
to
every
heap
allocated
type
in
their
code
and
get
extremely
frustrated.
A
A
New
developers
can
sidestep
a
lot
of
the
difficulty
of
the
ownership
and
borrowing
system
by
writing
code
that
is
slower
to
run
but
easier
to
understand.
This
trade-off
between
performance
and
ease
of
use
is
not
unique
to
rust
at
all,
but
rust
does
have
the
unique
benefit
of
naive
code,
often
being
fast
enough.
A
A
A
These
are
reference
counted
pointer
types
that
allow
you
to
share
ownership
between
multiple
variables,
similar
to
how
values
work
in
garbage
collected
languages.
Finally,
and
this
one's
important,
if
we
want
to
hide
the
performance
costs
of
the
previous
points,
compile
your
code
with
the
release
flag.
If
you
spent
any
time
on
russ
communities
like
the
subreddit
or
discord
servers,
you've
probably
seen
someone
complaining
about
rust
being
slow
only
to
then
become
impressed
with
their
performance
after
recompiling
with
optimizations
turned
on.