ReusableViewProvider manages object pools for reusable views like code blocks and tables, improving performance when rendering large markdown documents with many complex elements.
Class Definition
Overview
The provider maintains separate pools for different view types (CodeView and TableView) and handles view acquisition, recycling, and ordering. This reduces memory allocation overhead and improves scrolling performance.Initialization
Usage
Basic Usage
MarkdownTextView during initialization. You can share a single provider across multiple markdown views if needed.
Shared Provider Pattern
Internal Architecture
View Pooling
The provider maintains two internal object pools:- Code View Pool: Recycles
CodeViewinstances for syntax-highlighted code blocks - Table View Pool: Recycles
TableViewinstances for markdown tables
View Lifecycle
- Acquisition: When
MarkdownTextViewneeds a view, it acquires one from the pool - Usage: The view is configured and displayed in the markdown content
- Stashing: When content updates, unused views are returned to the pool
- Reordering: Views are reordered to match document structure, minimizing visual glitches
Thread Safety
The provider uses internal locking to ensure thread-safe access to view pools.Performance Benefits
View recycling provides significant performance improvements:
- Reduces memory allocations for complex views
- Minimizes view hierarchy changes during updates
- Improves scrolling performance in long documents
- Reduces frame drops during content updates
Default Behavior
If you don’t provide a customReusableViewProvider, MarkdownTextView creates its own:
Recycled View Types
CodeView
Used for rendering fenced code blocks with syntax highlighting:TableView
Used for rendering markdown tables:Best Practices
Single View
Use the default provider when rendering a single markdown view
Multiple Views
Share a provider across multiple views to maximize pooling efficiency
Memory Management
Providers automatically manage pool sizes - no manual cleanup needed
Thread Safety
Safe to pass the same provider to views on different threads