vmops

package module
v0.0.0-...-d7e641f Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2023 License: BSD-2-Clause Imports: 3 Imported by: 0

README

go-vmops

Go's regexp package is sufficient for many use cases. However, in cases where performance in paramount, switching to code generation can offer a significant speed increase. See for example, this article on speeding up regexs using the ragel state machine compiler.

However, large goto-based matchers, while significantly faster on native platforms, run into trouble on WebAssembly due the limitations of structured control flow. Compilers can take an exceedingly long time to turn the large generated code into something without any gotos.

The VMOPS output format from libfsm generates a large table of opcodes for a regular expression matcher that can be evaluated with much smaller inner loop. While slower than generating native code, it has the advantage that it can be used successfully on WebAssebmly. Due to libfsm's DFA-based matcher it can still end up being faster than Go's native regexp package.

The opcodes can also be serialized to a binary format to use with go:embed directives or loading from files at runtime with any additional parsing overhead.

Documentation

Overview

Package vmops is an interpreter for the vmops output from libfsm.

For more information, please see: https://github.com/katef/libfsm

Index

Constants

This section is empty.

Variables

View Source
var ErrBadLength = errors.New("vmops: bad data length")

ErrBadLength indicates the binary encoding had a bad length

View Source
var ErrCorrupt = errors.New("vmops: bad data")

ErrCorrupt indicatest the binary encoding was corrupt

Functions

This section is empty.

Types

type Action

type Action byte
const (
	ActionRET Action = iota
	ActionGOTO
)

type Op

type Op byte
const (
	OpEOF Op = iota
	OpLT
	OpLE
	OpEQ
	OpNE
	OpGE
	OpGT
	OpALWAYS
)

type Opcode

type Opcode struct {
	Op     Op
	C      byte
	Action Action
	// padding byte here
	Arg int32
}

type VM

type VM []Opcode

VM is a set of opcodes encoding a DFA matching a regular expression.

func (VM) MarshalBinary

func (vm VM) MarshalBinary() (data []byte, err error)

MarshalBinary implements encoding.BinaryMarshaler.

func (VM) MarshalBinaryUnsafe

func (vm VM) MarshalBinaryUnsafe() (data []byte, err error)

MarshalBinaryUnsafe marshals a vm to a byte slice directly using unsafe.

func (VM) Match

func (vm VM) Match(input []byte) int

Match runs the vm against the input byte slice and returns result.

func (VM) MatchString

func (vm VM) MatchString(input string) int

Match runs the vm against the input string and returns result.

func (*VM) UnmarshalBinary

func (vm *VM) UnmarshalBinary(data []byte) error

UnmarshalBinary implements encoding.BinaryUnmarshaler.

func (*VM) UnmarshalBinaryUnsafe

func (vm *VM) UnmarshalBinaryUnsafe(data []byte) error

UnmarshalBinaryUnsafe unmarshals a byte slice directly using unsafe.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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