simdscan

package
v0.0.18 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package simdscan provides SIMD-accelerated JSON field extraction.

It uses AVX2 instructions to scan 32 bytes at a time, classifying structural characters (quotes, backslashes, braces, brackets, colons, commas) in parallel. This enables finding JSON field values ~5-10x faster than traditional byte-by-byte parsers.

The package handles escaped characters correctly — backslash sequences like \" and \\ are properly tracked across SIMD chunk boundaries.

On non-amd64 architectures or CPUs without AVX2, a scalar fallback is used automatically.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = errors.New("field not found")
	ErrBadJSON  = errors.New("invalid JSON")
)

Functions

func EachField

func EachField(data []byte, fn func(f Field) bool) (int, error)

EachField calls fn for every top-level field of the JSON object in data, in order, without allocating. It returns the index of the closing brace. If fn returns false the scan stops and EachField returns -1.

Types

type Field

type Field struct {
	Key        []byte // raw key: without quotes and not unescaped
	KeyStart   int    // index of the opening quote of the key
	ValueStart int
	ValueEnd   int // exclusive; strings include their quotes
	Type       Type
}

Field describes a top-level field of a JSON object by its byte positions in the original data.

type ObjectScan

type ObjectScan struct {
	Data   []byte
	Keys   []string
	Coords []ValueCoord
}

ObjectScan holds the result of scanning a JSON object. Keys and Coords are parallel slices — Coords[i] describes the value for Keys[i]. Data is a reference to the original JSON bytes (zero-copy).

func ScanObject

func ScanObject(data []byte) (*ObjectScan, error)

ScanObject scans a JSON object and returns coordinates for all top-level fields. This is the SIMD-accelerated equivalent of stonejson.ParseToOffsets. No values are copied — only their positions within the original data.

func (*ObjectScan) Get

func (o *ObjectScan) Get(key string) ([]byte, Type)

Get retrieves the raw bytes and type for a given key. The value is extracted on-demand from the original data using coordinates.

func (*ObjectScan) GetString

func (o *ObjectScan) GetString(key string) (string, bool)

GetString is a convenience method that returns the string value for a key. For string types, the returned value does NOT include JSON quotes.

type Type

type Type int

Type represents the JSON value type.

const (
	TypeNotExist Type = iota
	TypeString
	TypeNumber
	TypeObject
	TypeArray
	TypeBoolean
	TypeNull
)

func GetField

func GetField(json []byte, field string) ([]byte, Type, error)

GetField extracts a top-level field value from a JSON object. For string values, the returned bytes do NOT include the surrounding quotes.

func GetPath

func GetPath(json []byte, path ...string) ([]byte, Type, error)

GetPath extracts a nested field value following the given path. Example: GetPath(data, "user", "address", "city") extracts the "city" field from the nested object at user.address.

type ValueCoord

type ValueCoord struct {
	Offset uint32
	Length uint32
	Type   Type
}

ValueCoord stores the position and type of a JSON value within the original byte slice — no copies, just coordinates.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL