Skip to content

Reduce stack use in deeply nested handlers #704

Description

@jfeser

Our current approach to deferring from one handler to another is to call fwd, which is dynamically bound to the next relevant handler. This puts multiple frames on the stack and for deeply nested handlers (as in weighted) we quickly blow the stack on even moderately sized terms.

However, the common calling pattern for fwd is that the call is in tail position, so in principle there's no need to use any additional stack frames in these cases. Python doesn't offer tail-call optimization, so we would need our own solution.

The simplest solution that I can see is to have another pattern for forwarding where the handler would return a marker value instead of calling fwd. This would have the advantage of forcing use only in tail position, but makes the forwarding interface more complex.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Fields

    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions