# Iterator codegen regression

**URL:** <https://internals.rust-lang.org/t/iterator-codegen-regression/3024>\
**Category:** Uncategorized\
**Created:** [December 22, 2015, 11:51am UTC](https://internals.rust-lang.org/t/iterator-codegen-regression/3024 "2015-12-22T11:51:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [December 22, 2015, 11:52am UTC](https://internals.rust-lang.org/t/iterator-codegen-regression/3024/1 "2015-12-22T11:52:00Z")

</div>

We’ve regressed in removing null checks. Some performance oriented code like itertools::Stride and [itertools::ZipSlices](http://bluss.github.io/rust-itertools/doc/itertools/struct.ZipSlices.html) now fall prey to llvm being doubtful of nonnullity and inserting extra checks.

The end effect is that loops that otherwise looked good to llvm now are not recognized. When loop transformations like unrolling and autovectorization are not available, the impact on performance may be 5x or more.

libstd works around this by liberal use of `assume`, which is an unstable intrinsic, so regular stable rust has no real recourse. I suppose it’s a bug that we can fix though.

Most of the affected code uses `<[_]>::get_unchecked()` or similar. I’ve also seen some cases of having an iterator derived from `&Vec` compile file while derived from `&[]` it fell into this null check trap [(code link)](https://play.rust-lang.org/?gist=d147f198626f668225f4&version=nightly)

---

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [December 22, 2015, 12:11pm UTC](https://internals.rust-lang.org/t/iterator-codegen-regression/3024/2 "2015-12-22T12:11:45Z")

</div>

The brute-force solution would be to spray `assume` calls into the implementations of the functions on slice.

---

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [December 22, 2015, 12:40pm UTC](https://internals.rust-lang.org/t/iterator-codegen-regression/3024/3 "2015-12-22T12:40:46Z")

</div>

Ideally producing a `&T` or `&mut T` should signal the same thing, regardless of how you do it.

---

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [December 22, 2015, 1:53pm UTC](https://internals.rust-lang.org/t/iterator-codegen-regression/3024/4 "2015-12-22T13:53:33Z")

</div>

Which would be equivalent to having the compiler spray the IR automatically. This shouldn’t be that hard to do. Just hard to do without adding `assume` at every usage site of a reference.

---

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [December 22, 2015, 5:15pm UTC](https://internals.rust-lang.org/t/iterator-codegen-regression/3024/5 "2015-12-22T17:15:21Z")

</div>

Anyway, at first try this was a bit hard to reproduce, I’ll go and look closer at my real world use cases. This code exhibits the null check problem in ZipSlices

[playground code, dot](https://play.rust-lang.org/?gist=699e7a664af593a8a0cf&version=nightly) / [reduced version, relevant function sum](https://play.rust-lang.org/?gist=2a9cd9f954187309bd08&version=nightly)

The relevant function is `dot`. Hit release and then ASM. As you can see from the closure call, the code has to resemble the benchmark where I found the regression, the closure is surely part of it “forgetting” the pointer information. So that was a bit obscure, but I’m still sure it’s a regression. Also, closures happen in my real code too, I have to use some abstractions…

@ker there’s already a nonnull attribute, assume shouldn’t be needed

Edit: Assume helps on this testcase. But the regression is also present in current stable, is that possible?

`assert!(!a.as_ptr().is_null());` is an example of a thing that doesn’t optimize out even if `a` is a slice. It also does not act like an assume.

---

<div class="post-metadata">

**Author:** ![alexcrichton](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/alexcrichton/32/4501_2.png) [@alexcrichton](https://internals.rust-lang.org/u/alexcrichton)\
**Post date:** [December 22, 2015, 5:35pm UTC](https://internals.rust-lang.org/t/iterator-codegen-regression/3024/6 "2015-12-22T17:35:13Z")

</div>

This is likely related to the [removal of the `NullCheckEliminationPass`](https://github.com/rust-lang/llvm/commit/3564439515985dc1cc0d77057ed00901635a80ad) in [#30165](https://github.com/rust-lang/rust/pull/30165) due to [#30081](https://github.com/rust-lang/rust/issues/30081)

---

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [March 25, 2019, 8:25am UTC](https://internals.rust-lang.org/t/iterator-codegen-regression/3024/7 "2019-03-25T08:25:36Z")

</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.
