Skip to content

Switching to the C stack is slow #1801

Description

@brson

pcwalton saw the C-stack-switching __morestack function show up surprisingly high on profiles. All this function does is change sp and call a function by pointer. Probably this is very bad for branch prediction.

Activity

  1. brson commented on Feb 10, 2012

    @brson
    ContributorAuthor

    Branch prediction is the reason the stack-growth __morestack function has such a bizarre structure.

  2. brson commented on Feb 10, 2012

    @brson
    ContributorAuthor

    Possibly we can just use the stack-growth __morestack function to do stack switching as well by setting the stack boundary to a value that is guaranteed to trip the call to __morestack and setting a flag in the task structure to put it into a different 'mode'. This would make branch prediction work and eliminate the need to marshall arguments through a struct on the stack.

  3. ghost assigned on Apr 12, 2012
  4. brson commented on Jul 31, 2012

    @brson
    ContributorAuthor

    We tried using __morestack for this and it had too much overhead from other stack growth code to make it worthwhile.

  5. emberian commented on Jul 12, 2013

    @emberian
    Contributor

    Visiting for triage; still relevant.

  6. huonw commented on Aug 28, 2013

    @huonw
    Contributor

    Triage: #8535 changed (some of) the way extern fns work, so I have no idea if this is valid, and I can't create a testcase that reproduces it (possibly because segmented stacks are disabled in the new rt?).

    @nikomatsakis would your changes have affected this?

  7. nikomatsakis commented on Aug 28, 2013

    @nikomatsakis
    Contributor

    @huonw yes and no. We no longer use the old C stack switching mechanism, we only use the "stack-growth" variant of __morestack. Moreover, users can now move the stack switch so it occurs earlier, which helps to eliminate overhead. However, the mechanism itself is still probably too slow -- though whether it can be further optimized is unclear, @pcwalton thought no. Certainly we can't eliminate the megamorphic call site that Brian referred to.

  8. catamorphism commented on Oct 17, 2013

    @catamorphism
    Contributor

    Obsolete

  9. added 2 commits that reference this issue on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationA-runtimeArea: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflowsI-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions