archives of rust-dev@mozilla.org (2010-2015)
 help / color / mirror / Atom feed
* [rust-dev] Overflow when benchmarking
@ 2014-11-27  4:22 Ben Wilson
  2014-11-27  4:28 ` Steven Fackler
  0 siblings, 1 reply; 8+ messages in thread
From: Ben Wilson @ 2014-11-27  4:22 UTC (permalink / raw)
  To: rust-dev

[-- Attachment #1: Type: text/plain, Size: 603 bytes --]

Hey folks, I've started writing some rust code lately and run into weird
behavior when benchmarking. When running

https://gist.github.com/benwilson512/56f84ffffd4625f11feb

#[bench]
fn test_overflow(b: &mut Bencher) {
  let nums = [0i, ..1000000];
  b.iter(|| {
    let mut x = 0i;
    for i in range(0, nums.len()) {
      x = nums[i];
    }
  });
}

 I get "task '<main>' has overflowed its stack" pretty much
immediately when running cargo bench. Ordinarily I'd expect to see
that error when doing recursion, but I can't quite figure out why it's
showing up here. What am I missing?

Thanks!

- Ben

[-- Attachment #2: Type: text/html, Size: 5467 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [rust-dev] Overflow when benchmarking
  2014-11-27  4:22 [rust-dev] Overflow when benchmarking Ben Wilson
@ 2014-11-27  4:28 ` Steven Fackler
  2014-11-27  4:32   ` Ben Wilson
  2014-11-27 16:50   ` Diggory Hardy
  0 siblings, 2 replies; 8+ messages in thread
From: Steven Fackler @ 2014-11-27  4:28 UTC (permalink / raw)
  To: Ben Wilson, rust-dev

[-- Attachment #1: Type: text/plain, Size: 959 bytes --]

The `nums` array is allocated on the stack and is 8 MB (assuming you're on
a 64 bit platform).

On Wed Nov 26 2014 at 8:23:08 PM Ben Wilson <benwilson512@gmail.com> wrote:

> Hey folks, I've started writing some rust code lately and run into weird
> behavior when benchmarking. When running
>
> https://gist.github.com/benwilson512/56f84ffffd4625f11feb
>
> #[bench]
> fn test_overflow(b: &mut Bencher) {
>   let nums = [0i, ..1000000];
>   b.iter(|| {
>     let mut x = 0i;
>     for i in range(0, nums.len()) {
>       x = nums[i];
>     }
>   });
> }
>
>  I get "task '<main>' has overflowed its stack" pretty much immediately when running cargo bench. Ordinarily I'd expect to see that error when doing recursion, but I can't quite figure out why it's showing up here. What am I missing?
>
> Thanks!
>
> - Ben
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>

[-- Attachment #2: Type: text/html, Size: 5246 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [rust-dev] Overflow when benchmarking
  2014-11-27  4:28 ` Steven Fackler
@ 2014-11-27  4:32   ` Ben Wilson
  2014-11-27 16:50   ` Diggory Hardy
  1 sibling, 0 replies; 8+ messages in thread
From: Ben Wilson @ 2014-11-27  4:32 UTC (permalink / raw)
  To: Steven Fackler; +Cc: rust-dev

[-- Attachment #1: Type: text/plain, Size: 1155 bytes --]

Doh, of course. Thanks, it's been a while since I've written low level
stuff.

On Wed, Nov 26, 2014 at 10:28 PM, Steven Fackler <sfackler@gmail.com> wrote:

> The `nums` array is allocated on the stack and is 8 MB (assuming you're on
> a 64 bit platform).
>
> On Wed Nov 26 2014 at 8:23:08 PM Ben Wilson <benwilson512@gmail.com>
> wrote:
>
>> Hey folks, I've started writing some rust code lately and run into weird
>> behavior when benchmarking. When running
>>
>> https://gist.github.com/benwilson512/56f84ffffd4625f11feb
>>
>> #[bench]
>> fn test_overflow(b: &mut Bencher) {
>>   let nums = [0i, ..1000000];
>>   b.iter(|| {
>>     let mut x = 0i;
>>     for i in range(0, nums.len()) {
>>       x = nums[i];
>>     }
>>   });
>> }
>>
>>  I get "task '<main>' has overflowed its stack" pretty much immediately when running cargo bench. Ordinarily I'd expect to see that error when doing recursion, but I can't quite figure out why it's showing up here. What am I missing?
>>
>> Thanks!
>>
>> - Ben
>>
>> _______________________________________________
>> Rust-dev mailing list
>> Rust-dev@mozilla.org
>> https://mail.mozilla.org/listinfo/rust-dev
>>
>

[-- Attachment #2: Type: text/html, Size: 5786 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [rust-dev] Overflow when benchmarking
  2014-11-27  4:28 ` Steven Fackler
  2014-11-27  4:32   ` Ben Wilson
@ 2014-11-27 16:50   ` Diggory Hardy
  2014-11-27 16:58     ` Steve Klabnik
  2014-11-28  3:40     ` Manish Goregaokar
  1 sibling, 2 replies; 8+ messages in thread
From: Diggory Hardy @ 2014-11-27 16:50 UTC (permalink / raw)
  To: rust-dev

[-- Attachment #1: Type: text/plain, Size: 1322 bytes --]

Shouldn't the compiler automatically put large arrays on the heap? I thought this was a common 
thing to do beyond a certain memory size.

On Thursday 27 November 2014 04:28:03 Steven Fackler wrote:


The `nums` array is allocated on the stack and is 8 MB (assuming you're on a 64 bit platform).



On Wed Nov 26 2014 at 8:23:08 PM Ben Wilson <benwilson512@gmail.com[1]> wrote:


Hey folks, I've started writing some rust code lately and run into weird behavior when benchmarking. 
When running


https://gist.github.com/benwilson512/56f84ffffd4625f11feb[2]

#[bench]
fn test_overflow(b: &mut Bencher) {
  let nums = [0i, ..1000000];
  b.iter(|| {
    let mut x = 0i;
    for i in range(0, nums.len()) {
      x = nums[i];
    }
  });
}


 I get "task '<main>' has overflowed its stack" pretty much immediately when running cargo bench. 
Ordinarily I'd expect to see that error when doing recursion, but I can't quite figure out why it's 
showing up here. What am I missing?



Thanks!


- Ben
_______________________________________________Rust-dev mailing list

Rust-dev@mozilla.org[3]
https://mail.mozilla.org/listinfo/rust-dev[4]




--------
[1] mailto:benwilson512@gmail.com
[2] https://gist.github.com/benwilson512/56f84ffffd4625f11feb
[3] mailto:Rust-dev@mozilla.org
[4] https://mail.mozilla.org/listinfo/rust-dev

[-- Attachment #2: Type: text/html, Size: 12706 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [rust-dev] Overflow when benchmarking
  2014-11-27 16:50   ` Diggory Hardy
@ 2014-11-27 16:58     ` Steve Klabnik
  2014-11-28  3:40     ` Manish Goregaokar
  1 sibling, 0 replies; 8+ messages in thread
From: Steve Klabnik @ 2014-11-27 16:58 UTC (permalink / raw)
  To: Diggory Hardy; +Cc: rust-dev

Rust is not interested in putting anything automatically on the heap. :)

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [rust-dev] Overflow when benchmarking
  2014-11-27 16:50   ` Diggory Hardy
  2014-11-27 16:58     ` Steve Klabnik
@ 2014-11-28  3:40     ` Manish Goregaokar
  2014-11-28 17:18       ` Matthieu Monrocq
  1 sibling, 1 reply; 8+ messages in thread
From: Manish Goregaokar @ 2014-11-28  3:40 UTC (permalink / raw)
  To: Diggory Hardy; +Cc: rust-dev

[-- Attachment #1: Type: text/plain, Size: 2019 bytes --]

C++/C has a lot of "features" which seem tantalizing at first; but end up
being against the point of a systems language.

Putting large arrays on the heap (not sure if C++ does this, but it sounds
like something C++ would do) is one -- there are plenty of cases where you
explicitly want stack-based arrays in systems programming.

Another is the alloca-like behavior of dynamically sized stack-based arrays
(just learned about this recently).

You always want to be clear of what the compiler is doing. Such
optimizations can easily be implemented as a library :)

-Manish Goregaokar

On Thu, Nov 27, 2014 at 10:20 PM, Diggory Hardy <lists@dhardy.name> wrote:

>  Shouldn't the compiler automatically put large arrays on the heap? I
> thought this was a common thing to do beyond a certain memory size.
>
>
> On Thursday 27 November 2014 04:28:03 Steven Fackler wrote:
>
> The `nums` array is allocated on the stack and is 8 MB (assuming you're on
> a 64 bit platform).
>
> On Wed Nov 26 2014 at 8:23:08 PM Ben Wilson <benwilson512@gmail.com>
> wrote:
>
> Hey folks, I've started writing some rust code lately and run into weird
> behavior when benchmarking. When running
>
>
> https://gist.github.com/benwilson512/56f84ffffd4625f11feb
>
> #[bench]
>
> fn test_overflow(b: &mut Bencher) {
>
>   let nums = [0i, ..1000000];
>
>   b.iter(|| {
>
>     let mut x = 0i;
>
>     for i in range(0, nums.len()) {
>
>       x = nums[i];
>
>     }
>
>   });
>
> }
>
>
>  I get "task '<main>' has overflowed its stack" pretty much immediately when running cargo bench. Ordinarily I'd expect to see that error when doing recursion, but I can't quite figure out why it's showing up here. What am I missing?
>
>
> Thanks!
>
>
> - Ben
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>
>
>
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>
>

[-- Attachment #2: Type: text/html, Size: 13230 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [rust-dev] Overflow when benchmarking
  2014-11-28  3:40     ` Manish Goregaokar
@ 2014-11-28 17:18       ` Matthieu Monrocq
  2014-11-28 17:32         ` Daniel Micay
  0 siblings, 1 reply; 8+ messages in thread
From: Matthieu Monrocq @ 2014-11-28 17:18 UTC (permalink / raw)
  To: Manish Goregaokar; +Cc: rust-dev

[-- Attachment #1: Type: text/plain, Size: 3120 bytes --]

Hello,

To be clear: there is no such thing as stack/heap in C and C++, there are
automatic variable and dynamically allocated variables, the former having
their lifetime known "statically" and the latter not...

Whether a particular compiler chooses to use the stack or heap for either
is its free choice, as long as it maintains the "as-if" rule.

In this case, I have never heard of automatically moving an automatic
variable to the heap, however LLVM routinely uses the stack for dynamically
allocated variables if it can prove their lifetime (probably restricted to
fixed-size variables below a certain threshold).

Regarding "Variable Length Arrays" (C99), they are not valid in C++, and
yes they are traditionally implemented using alloc, for better or worse.

-- Matthieu

On Fri, Nov 28, 2014 at 4:40 AM, Manish Goregaokar <manishsmail@gmail.com>
wrote:

> C++/C has a lot of "features" which seem tantalizing at first; but end up
> being against the point of a systems language.
>
> Putting large arrays on the heap (not sure if C++ does this, but it sounds
> like something C++ would do) is one -- there are plenty of cases where you
> explicitly want stack-based arrays in systems programming.
>
> Another is the alloca-like behavior of dynamically sized stack-based
> arrays (just learned about this recently).
>
> You always want to be clear of what the compiler is doing. Such
> optimizations can easily be implemented as a library :)
>
> -Manish Goregaokar
>
> On Thu, Nov 27, 2014 at 10:20 PM, Diggory Hardy <lists@dhardy.name> wrote:
>
>>  Shouldn't the compiler automatically put large arrays on the heap? I
>> thought this was a common thing to do beyond a certain memory size.
>>
>>
>> On Thursday 27 November 2014 04:28:03 Steven Fackler wrote:
>>
>> The `nums` array is allocated on the stack and is 8 MB (assuming you're
>> on a 64 bit platform).
>>
>> On Wed Nov 26 2014 at 8:23:08 PM Ben Wilson <benwilson512@gmail.com>
>> wrote:
>>
>> Hey folks, I've started writing some rust code lately and run into weird
>> behavior when benchmarking. When running
>>
>>
>> https://gist.github.com/benwilson512/56f84ffffd4625f11feb
>>
>> #[bench]
>>
>> fn test_overflow(b: &mut Bencher) {
>>
>>   let nums = [0i, ..1000000];
>>
>>   b.iter(|| {
>>
>>     let mut x = 0i;
>>
>>     for i in range(0, nums.len()) {
>>
>>       x = nums[i];
>>
>>     }
>>
>>   });
>>
>> }
>>
>>
>>  I get "task '<main>' has overflowed its stack" pretty much immediately when running cargo bench. Ordinarily I'd expect to see that error when doing recursion, but I can't quite figure out why it's showing up here. What am I missing?
>>
>>
>> Thanks!
>>
>>
>> - Ben
>>
>> _______________________________________________
>> Rust-dev mailing list
>> Rust-dev@mozilla.org
>> https://mail.mozilla.org/listinfo/rust-dev
>>
>>
>>
>>
>> _______________________________________________
>> Rust-dev mailing list
>> Rust-dev@mozilla.org
>> https://mail.mozilla.org/listinfo/rust-dev
>>
>>
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>
>

[-- Attachment #2: Type: text/html, Size: 14778 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [rust-dev] Overflow when benchmarking
  2014-11-28 17:18       ` Matthieu Monrocq
@ 2014-11-28 17:32         ` Daniel Micay
  0 siblings, 0 replies; 8+ messages in thread
From: Daniel Micay @ 2014-11-28 17:32 UTC (permalink / raw)
  To: rust-dev

[-- Attachment #1: Type: text/plain, Size: 614 bytes --]

On 28/11/14 12:18 PM, Matthieu Monrocq wrote:
> 
> In this case, I have never heard of automatically moving an automatic
> variable to the heap, however LLVM routinely uses the stack for
> dynamically allocated variables if it can prove their lifetime (probably
> restricted to fixed-size variables below a certain threshold).

LLVM doesn't have escape analysis for dynamic allocations. It only has
primitive dead store elimination for malloc/free. It could be taught to
do escape analysis, but it would probably need to be very conservative
and only do it for <= pointer size allocations by default.


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2014-11-28 17:32 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-27  4:22 [rust-dev] Overflow when benchmarking Ben Wilson
2014-11-27  4:28 ` Steven Fackler
2014-11-27  4:32   ` Ben Wilson
2014-11-27 16:50   ` Diggory Hardy
2014-11-27 16:58     ` Steve Klabnik
2014-11-28  3:40     ` Manish Goregaokar
2014-11-28 17:18       ` Matthieu Monrocq
2014-11-28 17:32         ` Daniel Micay

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox