# sumerc/yappi architecture

> Architecture diagram of the GitHub repository sumerc/yappi, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sumerc/yappi
- Repository: https://github.com/sumerc/yappi

## Overview

##### Main Components of the System
- Profiler Engine (Core C Modules):
- Lower-level C implementation (files like _yappi.c, callstack.c, freelist.c, hashtab.c, timing.c, tls.c, etc.).
- This component is responsible for capturing function calls, maintaining call stacks, and measuring CPU or wall time.
- Python Interface Layer:
- This is represented by “yappi/yappi.py” that exposes the C profiler’s functionality via a Python API.
- It also contains functions to start, stop, and report the profiling data.
- Context Adapters:
- Layers that handle multithreading, asyncio (coroutine profiling), and gevent (greenlet profiling) integration.
- Integration & Utilization:
- The application’s runtime environment where Yappi is integrated – for example, integration with IDEs like PyCharm (as noted in the README) and other Python applications using pip installation.
- Testing & Examples:
- Although not part of the core runtime, the “tests” and “doc” directories are also important. They illustrate usage scenarios (including examples of multithreaded, asyncio, and gevent profiling), ensuring correctness and guiding developers.

##### Relationships and Interactions
- The user application (or a test harness) calls the Python API functions from yappi/yappi.py.
- The Python layer delegates profiling tasks to the underlying C engine.
- The C engine captures execution details (function calls, timings, call stack) and stores the profiling data.
- Depending on the clock type (“cpu” vs. “wall”) and context backend (threads, asyncio, gevent), different adapters or hooks kick in.
- Upon stopping the profiler, data is marshalled back from the C engine to the Python layer for formatting and display (or to be saved in callgrind/pstat formats).

##### Important Architectural Patterns and Design Principles
- Separation of Concerns:
- The design distinctly separates the fast C-based core from the more user-friendly Python interface.
- Modular Design:
- Components (such as context adapters for different concurrency models) are modular so that they can be extended or modified independently.
- High Performance & Low Overhead:
- The use of C for core functionalities ensures the profiler introduces minimal overhead in capturing execution data.
- Extensibility:
- The API includes callback and tag mechanisms for advanced filtering/sorting of profiler data, allowing for flexible integration into various profiling scenarios.

##### Relevant Technologies, Frameworks, or Libraries
- Languages: C (for the core engine) and Python (for the interface and user-level API).
- Concurrency Libraries: Python’s threading, asyncio, and third-party gevent.
- Build & Deployment: Configuration files such as pyproject.toml, setup.py, and GitHub workflows indicate CI/CD and packaging practices.
- Testing Frameworks: The “tests” directory including many integration tests, provides examples for different usage scenarios.

##### Final Assembly
- Provide side views or insets for specialized workflows like coroutine profiling or multithreading, ensuring that the unique aspects (like handling context switches in asyncio) are clearly captured.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core vertical flow
    UA["User Application/Integration"]:::external
    API["Python API Layer"]:::api
    CORE["C Profiler Core"]:::core

    UA -->|"callsAPI"| API
    API -->|"delegates"| CORE
    CORE -->|"returnsData"| API
    API -->|"sendsResults"| UA

    %% Context Adapters Subgraph
    subgraph "Context Adapters (Threads/Asyncio/Gevent)"
        AIO["Asyncio Adapter"]:::adapter
        GE["Gevent Adapter"]:::adapter
        ACM["Async Context Managers"]:::adapter
        AG["Async Generators"]:::adapter
    end
    API -->|"hooks"| AIO
    API -->|"hooks"| GE
    API -->|"hooks"| ACM
    API -->|"hooks"| AG
    AIO -->|"captures"| CORE
    GE -->|"captures"| CORE
    ACM -->|"captures"| CORE
    AG -->|"captures"| CORE

    %% Integration & Utilization Subgraph
    subgraph "Integration & Utilization"
        SETUP["Setup & Build"]:::integration
        PYPROJ["PyProject.toml"]:::integration
    end
    SETUP -->|"integration"| UA
    PYPROJ -->|"integration"| UA

    %% Testing & Documentation Subgraph
    subgraph "Testing & Documentation"
        TEST["Test Suite"]:::test
        DOC["Documentation"]:::test
    end
    TEST -->|"validates"| API
    DOC -->|"guides"| API

    %% Click Events
    click CORE "https://github.com/sumerc/yappi/tree/master/yappi"
    click API "https://github.com/sumerc/yappi/blob/master/yappi/yappi.py"
    click AIO "https://github.com/sumerc/yappi/blob/master/tests/test_asyncio.py"
    click GE "https://github.com/sumerc/yappi/blob/master/tests/test_gevent.py"
    click ACM "https://github.com/sumerc/yappi/blob/master/tests/test_async_context_managers.py"
    click AG "https://github.com/sumerc/yappi/blob/master/tests/test_async_generators.py"
    click SETUP "https://github.com/sumerc/yappi/blob/master/setup.py"
    click PYPROJ "https://github.com/sumerc/yappi/blob/master/pyproject.toml"
    click TEST "https://github.com/sumerc/yappi/tree/master/tests"
    click DOC "https://github.com/sumerc/yappi/tree/master/doc"

    %% Styles
    classDef external fill:#ffdead,stroke:#000,stroke-width:2px;
    classDef api fill:#add8e6,stroke:#000,stroke-width:2px;
    classDef core fill:#90ee90,stroke:#000,stroke-width:2px;
    classDef adapter fill:#f0e68c,stroke:#000,stroke-width:2px;
    classDef integration fill:#d3d3d3,stroke:#000,stroke-width:2px;
    classDef test fill:#ffc0cb,stroke:#000,stroke-width:2px;
```
