Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Alibaba MNN Protobuf Parse Context H

From Leeroopedia


Metadata

Attribute Value
Implementation File 3rd_party/protobuf/src/google/protobuf/parse_context.h
Line Count 938
Domains Serialization, Parsing
Key Classes ParseContext, EpsCopyInputStream
Created 2026-02-10
Knowledge Sources Repo, Doc

Overview

Parse context header for wire format parsing. Declares the high-performance streaming parse infrastructure including buffer management and recursion depth tracking.

Usage note: Vendored dependency used internally by MNN for parsing protobuf-based model formats. Not directly imported by end users.

Description

This header declares two primary classes:

  • EpsCopyInputStream -- A buffered input stream that maintains an epsilon (slop) region past the logical end of available data. This design eliminates per-byte bounds checking for multi-byte reads (varints, fixed32/64), significantly improving parse throughput. When the read pointer enters the slop region, the stream refills from the underlying data source.
  • ParseContext -- Extends EpsCopyInputStream with parse state management including recursion depth tracking, group-end detection, and end-of-stream signaling. It is threaded through all generated _InternalParse methods as the second parameter.

Key design aspects:

  • The pointer-return convention (const char*) replaces boolean success/failure with a combined position-and-status return. A null return indicates parse failure.
  • Depth limits prevent stack overflow from deeply nested messages.
  • The kSlopBytes constant (typically 16) determines the safety margin size.

Usage

#include "google/protobuf/parse_context.h"

Included by generated protobuf code and internal runtime files that implement wire-format deserialization.

Code Reference

// Buffered input with epsilon safety margin.
class PROTOBUF_EXPORT EpsCopyInputStream {
 public:
  static constexpr int kSlopBytes = 16;

  EpsCopyInputStream(const char* data, int size, ParseContext* ctx);

  // Advance the read pointer, refilling if necessary.
  const char* Next(int overrun, int chunk_size);

  // Check if parsing is complete.
  bool DoneWithCheck(const char** ptr, int d);

  // Current position in the buffer.
  const char* cur() const;

 protected:
  const char* buffer_;
  const char* buffer_end_;
  int overall_limit_;
};

// Parse context extending EpsCopyInputStream with depth tracking.
class PROTOBUF_EXPORT ParseContext : public EpsCopyInputStream {
 public:
  ParseContext(int depth, bool aliasing, const char** start, int size);

  // Parse a nested message.
  template <typename T>
  const char* ParseMessage(T* msg, const char* ptr);

  // Check end-of-stream.
  bool Done(const char** ptr);

  int depth() const { return depth_; }

 private:
  int depth_;
};

I/O Contract

Direction Type Description
Input const char* buffer Raw wire-format bytes to parse
Output const char* advanced pointer Updated position after consuming data; nullptr on error
State depth_ Recursion depth counter to prevent stack overflow

Usage Examples

// Internal parse entry point (generated code pattern)
const char* MyMessage::_InternalParse(const char* ptr,
                                       ParseContext* ctx) {
  while (!ctx->Done(&ptr)) {
    uint32_t tag = *ptr;
    ptr = ReadTag(ptr, &tag);
    switch (tag >> 3) {
      case 1:
        ptr = ctx->ParseMessage(mutable_sub_message(), ptr);
        break;
      // ... other fields
    }
  }
  return ptr;
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment