Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/json: optimizing memory allocation of json.RawMessage #58657

Closed
hafus opened this issue Feb 23, 2023 · 2 comments
Closed

encoding/json: optimizing memory allocation of json.RawMessage #58657

hafus opened this issue Feb 23, 2023 · 2 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@hafus
Copy link

hafus commented Feb 23, 2023

What version of Go are you using (go version)?

$ go version
All versions

Does this issue reproduce with the latest release?

Yes, It does

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
All OS/CPU arch

What did you do?

This is the first time I open an issue in Golang repo and I don't know if it's under the right category. I would like to propose an optimization to json.RawMessage struct to get rid of an extract memory allocation on call of UnmarshalJSON func of json.RawMessage struct as stated below:

Current

func (m *RawMessage) UnmarshalJSON(data []byte) error {
	if m == nil {
		return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
	}
	*m = append((*m)[0:0], data...)
	return nil
}

Proposal

func (m *RawMessage) UnmarshalJSON(data []byte) error {
	if m == nil {
		return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
	}

	*m = data
	return nil
}

To verify the above optimization, I run below benchmark.

type msg struct {
	RawData json.RawMessage
}

var raw = []byte(`{"RawData":{"name":"test", "age":22}}`)

func BenchmarkRawMsg(b *testing.B) {

	for i := 0; i < b.N; i++ {
		var m msg
		json.Unmarshal(raw, &m)
	}

}

What did you expect to see?

BenchmarkRawMsg-16 1476358 797.3 ns/op 256 B/op 6 allocs/op

What did you see instead?

BenchmarkRawMsg-16 1467776 842.1 ns/op 288 B/op 7 allocs/op

@mateusz834
Copy link
Member

https://pkg.go.dev/encoding/json#Unmarshaler

UnmarshalJSON must copy the JSON data if it wishes to retain the data after returning.

@thanm thanm added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 23, 2023
@seankhliao
Copy link
Member

see above, working as intended

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Feb 23, 2023
@golang golang locked and limited conversation to collaborators Feb 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants