Skip to content

Check for BinaryValue to allow types to use a different binary encoding#1278

Merged
arp242 merged 1 commit intomasterfrom
bin
Mar 11, 2026
Merged

Check for BinaryValue to allow types to use a different binary encoding#1278
arp242 merged 1 commit intomasterfrom
bin

Conversation

@arp242
Copy link
Collaborator

@arp242 arp242 commented Mar 11, 2026

The driver.Valuer is used for both text and binary representation, which isn't necessarily desired as the binary representation can be different.

So also check for "BinaryValue() ([]byte, error)" in CheckNamedValue(), and use that if it exists.

For the time being this is intentionally unexported and undocumented. I think this should be right, but I want to implement some more binary types before I'm sure.

Also make the Hstore.Value() a bit faster:

goos: linux
goarch: amd64
pkg: github.com/lib/pq/hstore
cpu: AMD Ryzen 7 PRO 7840U w/ Radeon 780M Graphics

BenchmarkHstore/Value-16                 1228444               979.5 ns/op           904 B/op         11 allocs/op
BenchmarkHstore/BinaryValue-16           3443780               362.9 ns/op          1008 B/op          3 allocs/op

Previous for Value:
BenchmarkHstore/Value-16                  514468              2084 ns/op            2288 B/op         46 allocs/op

Could make it faster still, but this was quick, fast, and obvious to do, and gets us more than double the performance.

Fixes #705

The driver.Valuer is used for both text and binary representation, which
isn't necessarily desired as the binary representation can be different.

So also check for "BinaryValue() ([]byte, error)" in CheckNamedValue(),
and use that if it exists.

For the time being this is intentionally unexported and undocumented. I
think this should be right, but I want to implement some more binary
types before I'm sure.

Also make the Hstore.Value() a bit faster:

	goos: linux
	goarch: amd64
	pkg: github.com/lib/pq/hstore
	cpu: AMD Ryzen 7 PRO 7840U w/ Radeon 780M Graphics

	BenchmarkHstore/Value-16                 1228444               979.5 ns/op           904 B/op         11 allocs/op
	BenchmarkHstore/BinaryValue-16           3443780               362.9 ns/op          1008 B/op          3 allocs/op

	Previous for Value:
	BenchmarkHstore/Value-16                  514468              2084 ns/op            2288 B/op         46 allocs/op

Could make it faster still, but this was quick, fast, and obvious to do,
and gets us more than double the performance.

Fixes #705
@arp242 arp242 merged commit 5db0f0d into master Mar 11, 2026
13 checks passed
@arp242 arp242 deleted the bin branch March 11, 2026 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hstore doesn't work with binary_parameters

1 participant