# Needs more Cow-bell?

**URL:** <https://internals.rust-lang.org/t/needs-more-cow-bell/2612>\
**Category:** Uncategorized\
**Created:** [September 8, 2015, 8:00am UTC](https://internals.rust-lang.org/t/needs-more-cow-bell/2612 "2015-09-08T08:00:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![llogiq](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/llogiq/32/13692_2.png) [@llogiq](https://internals.rust-lang.org/u/llogiq)\
**Post date:** [September 8, 2015, 8:00am UTC](https://internals.rust-lang.org/t/needs-more-cow-bell/2612/1 "2015-09-08T08:00:04Z")

</div>

Recently I had the good fortune to get a good look at the Rust codebase. Fun times! 😄

One thing I noticed is that there are many places where owned `String`s are used, even though in the majority of cases, a `&'static str` would suffice. Many of those strings are just allocated to be used in `format!`ting or to be written out somewhere.

Since I am [a fan](https://llogiq.github.io/2015/07/09/cow.html) of `Cow`s, I wonder why we don’t use them in the compiler more often? So I wrote a [microbenchmark](https://gist.github.com/llogiq/4557032bdf18a2cff136) to see how the performance differs. The results? Allocating is very fast, but just using a Cow is even faster (to the tune of 2ns per `Cow` vs. 33ns per allocation, this benchmark does not include e.g. cache effects, so the actual effect is likely underreported).

So, Rust developers, next time you reach for a `.to_owned()` or worse, `.to_string()`, consider using `Cow`.

(Of course, this likely has little effect on the actual runtime of the compiler, because those strings are usually not on hot code paths. Still, with allocation you never know if you tread on other code’s toes).

---

<div class="post-metadata">

**Author:** ![sinistersnare](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sinistersnare/32/18_2.png) [@sinistersnare](https://internals.rust-lang.org/u/sinistersnare)\
**Post date:** [September 8, 2015, 3:21pm UTC](https://internals.rust-lang.org/t/needs-more-cow-bell/2612/2 "2015-09-08T15:21:29Z")

</div>

Are you planning on sending a PR for changing the `String`s to `Cow`s?

---

<div class="post-metadata">

**Author:** ![llogiq](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/llogiq/32/13692_2.png) [@llogiq](https://internals.rust-lang.org/u/llogiq)\
**Post date:** [September 8, 2015, 3:54pm UTC](https://internals.rust-lang.org/t/needs-more-cow-bell/2612/3 "2015-09-08T15:54:24Z")

</div>

Not yet. That’d be potentially a lot of work. I currently wonder if I can write a [lint](https://github.com/Manishearth/rust-clippy/issues/320) that identifies places where Cow might be beneficial. That would automate the hardest part of it.

---

<div class="post-metadata">

**Author:** ![ehiggs](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ehiggs/32/3224_2.png) [@ehiggs](https://internals.rust-lang.org/u/ehiggs)\
**Post date:** [September 10, 2015, 7:25pm UTC](https://internals.rust-lang.org/t/needs-more-cow-bell/2612/4 "2015-09-10T19:25:58Z")

</div>

Nice. If I understand correctly, the pattern exemplified in your microbenchmark works because strings in the code are already implicitly `'static &str` before `to_owned()` receives them. It adds some noise to the code, but if you’re used to seeing the Perl-like symbol spew, you can get used to it.

It using `to_owned` or allocations a bottleneck in rustc?

---

<div class="post-metadata">

**Author:** ![llogiq](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/llogiq/32/13692_2.png) [@llogiq](https://internals.rust-lang.org/u/llogiq)\
**Post date:** [September 10, 2015, 8:17pm UTC](https://internals.rust-lang.org/t/needs-more-cow-bell/2612/5 "2015-09-10T20:17:49Z")

</div>

No. It’s not a bottleneck. Just noise in the profile (but think of the caches!). And yes, it’s the allocation, `to_owned()` just allocates and makes a copy. Also your understanding is only half of the truth: In fact Cow works whenever there is a lifetime bound that all borrowed instances can comply with.

However, for the lint, to keep complexity low and still get a lot of benefit, I’m going to concentrate on 'static first. Otherwise the lint would have to infer a possible lifetime bound. I might be able to do that some day if I think long and hard about it, but I think going for the easy targets first will give us more bang per buck for now.

---

<div class="post-metadata">

**Author:** ![ehiggs](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ehiggs/32/3224_2.png) [@ehiggs](https://internals.rust-lang.org/u/ehiggs)\
**Post date:** [September 11, 2015, 8:50am UTC](https://internals.rust-lang.org/t/needs-more-cow-bell/2612/6 "2015-09-11T08:50:38Z")

</div>

I just profiled rustc when building a package (`rust-cpython`) using `valgrind --tool=callgrind` and found that while `rustc` spends 12% of its time in `je_mallocx` and `je_sdalloc` each, with about 6 million calls apiece. So I think there might be something to squeeze out of here.

---

<div class="post-metadata">

**Author:** ![llogiq](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/llogiq/32/13692_2.png) [@llogiq](https://internals.rust-lang.org/u/llogiq)\
**Post date:** [September 11, 2015, 2:02pm UTC](https://internals.rust-lang.org/t/needs-more-cow-bell/2612/7 "2015-09-11T14:02:26Z")

</div>

Thank you for actually measuring.

Let’s also assume that adding or removing allocation will have non-linear effects on performance (where removing some allocation can give you disproportionate performance benefits).

---

<div class="post-metadata">

**Author:** ![arielb1](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/arielb1/32/3044_2.png) [@arielb1](https://internals.rust-lang.org/u/arielb1)\
**Post date:** [September 17, 2015, 7:11am UTC](https://internals.rust-lang.org/t/needs-more-cow-bell/2612/8 "2015-09-17T07:11:19Z")

</div>

rustc’s malloc problem is because of the terrible implementation and layout of `Substs`. This is annoying to fix because of API problems, but someone should do it.

---

<div class="post-metadata">

**Author:** ![llogiq](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/llogiq/32/13692_2.png) [@llogiq](https://internals.rust-lang.org/u/llogiq)\
**Post date:** [September 17, 2015, 7:57am UTC](https://internals.rust-lang.org/t/needs-more-cow-bell/2612/9 "2015-09-17T07:57:55Z")

</div>

I agree that owned copies of static strings probably account only for a small part of allocations. Still, it’s an easy target for a lint, and a pattern I see over and over in Rust code, so this will not only benefit rustc, but everyone who uses the lint.

---

<div class="post-metadata">

**Author:** ![llogiq](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/llogiq/32/13692_2.png) [@llogiq](https://internals.rust-lang.org/u/llogiq)\
**Post date:** [March 25, 2019, 8:25am UTC](https://internals.rust-lang.org/t/needs-more-cow-bell/2612/10 "2019-03-25T08:25:07Z")

</div>

Closed on March 25, 2019, 8:25am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
