# davisp/jiffy architecture

> Architecture diagram of the GitHub repository davisp/jiffy, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/davisp/jiffy
- Repository: https://github.com/davisp/jiffy

## Overview

##### Main Components

a) Core Components:
- Erlang Interface Layer
- NIF Implementation Layer
- JSON Processing Engine
- UTF-8 Handling System
- Memory Management System

b) Key Processing Components:
- Encoder Pipeline
- Decoder Pipeline
- Double Conversion System

**┌─ Application Layer ─────────────────────┐**
- Erlang Interface (src/jiffy.erl)        │
- ┬────────────────────┘
- ↓
**┌─ NIF Interface Layer ───────────────────┐**
- jiffy.c (Main NIF Implementation)       │
- ┬────────────────┬───────────┘
- ↓                ↓
**┌─ Processing Engine ─┐  ┌─ UTF-8 Handler ─┐**
- Encoder          │  │ UTF-8 Validation │
- Decoder          │  │ String Processing│
- Term Stack       │  └────────────────┘
- ┬─────────┘
- ↓
**┌─ Double Conversion Layer ──────────────┐**
- Double-Conversion Library Integration   │
- ┘

##### Key Relationships to Highlight

a) Data Flow:
- JSON Input → Decoder → Erlang Terms
- Erlang Terms → Encoder → JSON Output

b) Component Interactions:
- How the NIF interface bridges Erlang and C code
- Term stack's role in processing
- UTF-8 validation flow

##### Implementation Details

- The hand-crafted state machine architecture
- UTF-8 handling specifics
- Memory optimization strategies
- Error handling paths

##### Testing Components

A separate section showing:
- Test cases organization
- Property-based testing
- Performance testing points

- Nested components
- Clear connection lines
- Proper layering support

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Style Definitions
    classDef erlang fill:#4B77BE,color:white
    classDef c_cpp fill:#2ECC71,color:white
    classDef processing fill:#F1C40F,color:black
    classDef external fill:#95A5A6,color:white
    classDef utf8 fill:#E74C3C,color:white

    %% Application Layer
    subgraph AppLayer["Application Layer"]
        ErlInterface["Erlang Interface"]:::erlang
    end
    click ErlInterface "https://github.com/davisp/jiffy/blob/master/src/jiffy.erl"

    %% NIF Interface Layer
    subgraph NIFLayer["NIF Implementation Layer"]
        NIFImpl["NIF Implementation"]:::c_cpp
    end
    click NIFImpl "https://github.com/davisp/jiffy/blob/master/c_src/jiffy.c"

    %% Processing Engine Layer
    subgraph ProcessingEngine["Processing Engine"]
        Encoder["Encoder Pipeline"]:::processing
        Decoder["Decoder Pipeline"]:::processing
        TermStack["Term Stack System"]:::processing
    end
    click Encoder "https://github.com/davisp/jiffy/blob/master/c_src/encoder.c"
    click Decoder "https://github.com/davisp/jiffy/blob/master/c_src/decoder.c"
    click TermStack "https://github.com/davisp/jiffy/blob/master/c_src/termstack.c"

    %% UTF-8 Handling Layer
    subgraph UTF8Layer["UTF-8 Handling Layer"]
        UTF8Handler["UTF-8 Handler"]:::utf8
        StringProc["String Processing"]:::utf8
    end
    click UTF8Handler "https://github.com/davisp/jiffy/blob/master/c_src/utf8.c"

    %% Object Handling Layer
    subgraph ObjectLayer["Object Handling Layer"]
        ObjHandler["Object Handler"]:::c_cpp
        UtilFunc["Utility Functions"]:::c_cpp
    end
    click ObjHandler "https://github.com/davisp/jiffy/blob/master/c_src/objects.cc"
    click UtilFunc "https://github.com/davisp/jiffy/blob/master/c_src/util.c"

    %% Double Conversion Layer
    subgraph DoubleLayer["Double Conversion Layer"]
        DoubleConv["Double-Conversion System"]:::external
    end
    click DoubleConv "https://github.com/davisp/jiffy/tree/master/c_src/double-conversion/"

    %% Relationships
    ErlInterface --> NIFImpl
    NIFImpl --> Encoder
    NIFImpl --> Decoder
    Encoder --> TermStack
    Decoder --> TermStack
    Encoder --> UTF8Handler
    Decoder --> UTF8Handler
    UTF8Handler --> StringProc
    Encoder --> ObjHandler
    Decoder --> ObjHandler
    ObjHandler --> UtilFunc
    Encoder --> DoubleConv
    Decoder --> DoubleConv

    %% Legend
    subgraph Legend["Legend"]
        L1["Erlang Components"]:::erlang
        L2["C/C++ Components"]:::c_cpp
        L3["Processing Components"]:::processing
        L4["External Libraries"]:::external
        L5["UTF-8 Components"]:::utf8
    end
```
