feat: add pg_tiktoken_c - pure C tiktoken for PostgreSQL, 1700x faster than pg_tiktoken#18
Open
fasdwcx wants to merge 1 commit into
Open
feat: add pg_tiktoken_c - pure C tiktoken for PostgreSQL, 1700x faster than pg_tiktoken#18fasdwcx wants to merge 1 commit into
fasdwcx wants to merge 1 commit into
Conversation
…n Rust version - Pure C implementation of OpenAI tiktoken BPE tokenizer for PostgreSQL - 1700x faster than pg_tiktoken (Rust/pgrx) for typical inputs - Encoder cached in TopMemoryContext per backend (no per-call init overhead) - Supports tiktoken_count(), tiktoken_encode(), chunk_text_table() for RAG - Compatible with PostgreSQL 13-17, Apache 2.0 license - GitHub: https://github.com/relytcloud/pg_tiktoken_c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add pg_tiktoken_c (id: 1875) to the RAG category, positioned after pg_tiktoken (1870).
What is pg_tiktoken_c?
A PostgreSQL extension that implements OpenAI's tiktoken BPE tokenizer in pure C, as a high-performance alternative to pg_tiktoken (Rust/pgrx).
Performance vs pg_tiktoken (Rust/pgrx)
Benchmark on Apple M-series · PostgreSQL 17 · cl100k_base · single connection:
Root cause of gap: pg_tiktoken re-initialises the BPE encoder on every call (~220 ms overhead). pg_tiktoken_c caches the encoder in
TopMemoryContextonce per backend.Features
tiktoken_count(encoding, text)— token countingtiktoken_encode(encoding, text)— returns token ID arrayschunk_text_table(text, size, overlap)— document chunking for RAG pipelinescl100k_base,o200k_base,r50k_base,p50k_base,p50k_editgpt-4o,gpt-4,o1, etc.)IMMUTABLE PARALLEL SAFE— works in indexes, generated columns, parallel queriesEntry added
GitHub: https://github.com/relytcloud/pg_tiktoken_c
License: Apache 2.0