Compiler Optimization of Coroutine Activation Frames

Resolve Bottlenecks,
Find Innovative Solutions
Generate Solutions

Solution Overview

Problem

Conventional approaches to memory allocation for coroutines often lead to excessive heap memory usage, causing systems to run out of memory and making it impractical to execute programs.

Innovation Solution

Allocating coroutine activation frames on the caller's stack instead of the heap, and inlining coroutine code with the caller's code when the coroutine's lifespan is limited to the caller's, to optimize memory usage.

Engineering Contradictions & Design Principles

VSEngineering Contradiction Analysis

1Adaptability or versatility

If coroutine activation frames are allocated on the heap, then memory allocation is flexible and dynamic, but memory usage increases excessively causing systems to run out of memory

Engineering Contradiction:
Improvememory allocation flexibilityVSAvoidmemory usage
Core Design Contradiction:
Adaptability or versatilityVSQuantity of substance

Solution Approach 1:

The patent dynamically selects the allocation strategy based on coroutine characteristics. The compiler analyzes whether a coroutine can be inlined and makes runtime decisions about stack vs heap allocation, transforming a static allocation approach into a dynamic one that adapts to specific coroutine scenarios.

Inventive Principle:
Principle #15Dynamics

Solution Approach 2:

Different allocation strategies are applied to different coroutines based on their specific characteristics. Coroutines that can be inlined use stack allocation, while others use heap allocation. This localized optimization reduces overall memory usage while maintaining flexibility where needed.

Inventive Principle:
Principle #3Local quality

2Quantity of substance

If coroutine code is inlined with caller's code, then memory usage is reduced, but code complexity and compilation difficulty increase

Engineering Contradiction:
Improvememory usageVSAvoidcode complexity
Core Design Contradiction:
Quantity of substanceVSDevice complexity

Solution Approach 1:

The compiler performs preliminary analysis during the compilation phase to determine which coroutines are suitable for inlining. By making this determination beforehand, the complex decision-making process is shifted from runtime to compiletime, reducing runtime complexity while achieving memory savings through inlining.

Inventive Principle:
Principle #10Preliminary action

Solution Approach 2:

The patent replaces the mechanical runtime allocation and management of coroutine frames with a compiletime analysis and transformation process. The compiler automatically performs the complex work of determining inlining opportunities and generating the appropriate code, substituting manual or runtime mechanical processes with automated compiletime operations.

Inventive Principle:
Principle #28Mechanics substitution (Replace mechanical system)

3Quantity of substance

If coroutine activation frames are allocated on the stack, then memory usage is reduced, but the coroutine's lifespan must be limited to the caller's lifespan

Engineering Contradiction:
Improvememory usageVSAvoidcoroutine lifespan
Core Design Contradiction:
Quantity of substanceVSDuration of action of moving object

Solution Approach 1:

The patent changes the lifetime parameter of coroutines based on their allocation strategy. By analyzing coroutine characteristics and transforming them appropriately, the system enables short-lived coroutines to use stack allocation (reducing memory usage) while maintaining the ability to use heap allocation for longer-lived coroutines, thus adapting the lifetime parameter to match the allocation strategy.

Inventive Principle:
Principle #35Parameter changes

Data Source

PatentUS10747511B2Compiler optimization of coroutines
Publication Date: 2020.08.18 MICROSOFT TECHNOLOGY LICENSING LLC
  • US10747511B2 patent drawing
  • US10747511B2 patent drawing
  • US10747511B2 patent drawing

AI summary

As a memory usage optimization, a compiler identifies coroutines whose activation frames can be allocated on a caller's stack instead of allocating the frame on the heap. For example, when the compiler determines that a coroutine C's life cannot extend beyond the life of the routine R that first calls the coroutine C, the compiler generates code to allocate the activation frame for C on the stack of R, instead of generating code to allocate C's frame from heap memory. In some cases, as another optimization, code for coroutine C is also inlined with code for the routine R that calls C. Coroutine activation frame content variations and layout variations are also described.