# Cow statics by default

**URL:** <https://internals.rust-lang.org/t/cow-statics-by-default/2632>\
**Category:** libs\
**Created:** [September 11, 2015, 4:08pm UTC](https://internals.rust-lang.org/t/cow-statics-by-default/2632 "2015-09-11T16:08:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stebalien](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/stebalien/32/228_2.png) [@stebalien](https://internals.rust-lang.org/u/stebalien)\
**Post date:** [September 11, 2015, 4:08pm UTC](https://internals.rust-lang.org/t/cow-statics-by-default/2632/1 "2015-09-11T16:08:30Z")

</div>

This may be a crazy idea that will never work but, has anyone considered constructing Box, RawVec (Vec, String, etc...), etc. with pointers into the rodata section and copy (to the heap) on write? Would the extra check (does the pointer point to RO data) in mutating functions add too much overhead.

In response to:

> [@Needs more Cow-bell?](https://internals.rust-lang.org/t/needs-more-cow-bell/2612/6):
>
> 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.

/cc @llogiq

---

<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, 4:28pm UTC](https://internals.rust-lang.org/t/cow-statics-by-default/2632/2 "2015-09-11T16:28:08Z")

</div>

I’d prefer to make such stuff explicit. It’s hard enough to reason about performance without such optimizations; and there may be the stray case where the additional check actually leads to a pessimization.

If you’d like it, a source-to-source converter (perhaps as a macro plugin that would work a module at a time) might work without too much effort, though. But first, I’d like to construct a lint, because that’d give me the experience to determine if automated analysis is good enough to rely on for code transformation.

---

<div class="post-metadata">

**Author:** ![stebalien](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/stebalien/32/228_2.png) [@stebalien](https://internals.rust-lang.org/u/stebalien)\
**Post date:** [September 11, 2015, 5:25pm UTC](https://internals.rust-lang.org/t/cow-statics-by-default/2632/3 "2015-09-11T17:25:11Z")

</div>

For String, I don’t think this would be a problem because strings aren’t usually mutably dereferenced (this feature wasn’t even introduced until recently) and pushing onto a string always requires bounds checking. Vec and Box, however, may be problematic.

Alternatively, we could wait for static allocation and more powerful CTFE (AFAIK, this is needed for static Mutexs anyways). This isn’t quite as memory efficient but it avoids runtime allocation.

---

<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 14, 2015, 8:26am UTC](https://internals.rust-lang.org/t/cow-statics-by-default/2632/4 "2015-09-14T08:26:04Z")

</div>

Long-term, this could be a MIR optimization pass (perhaps even with a compiler plugin); however, it would either need to perform cross crate, or leave the `String`y versions of the API intact.

---

<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, 6:22am UTC](https://internals.rust-lang.org/t/cow-statics-by-default/2632/5 "2015-09-17T06:22:42Z")

</div>

Just a heads-up. I’m making some progress. I’ve got a lint that is Visitor and ExprUseVisitor (the former is used to collect all data before reporting, also to mark public API items as off-limits (for now; I may change this to just set a flag and report under a different lint, e.g. for internal crates or other scenarios where you have no qualms about changing the publicly visible API).

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:25am UTC](https://internals.rust-lang.org/t/cow-statics-by-default/2632/6 "2019-03-25T08:25:08Z")

</div>

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