Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

go fields: explicity support non-pointer marshalers #185

Merged
merged 2 commits into from
Oct 10, 2018

Conversation

berfarah
Copy link
Contributor

@berfarah berfarah commented Oct 9, 2018

Because we sometimes might want to use non-pointers for protobuf, check our non-pointer types.

Test plan:

  • Create benchmark for the above - maybe we should have benchmarks per supported value
  • Run simulation traffic if the benchmarks look significantly different (more than 5%)

@berfarah berfarah requested review from stephen and willhug October 9, 2018 22:41
@coveralls
Copy link

coveralls commented Oct 9, 2018

Pull Request Test Coverage Report for Build 1142

  • 10 of 10 (100.0%) changed or added relevant lines in 1 file are covered.
  • 5 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.08%) to 65.788%

Files with Coverage Reduction New Missed Lines %
reactive/graph.go 1 94.07%
graphql/schemabuilder/reflect.go 4 71.55%
Totals Coverage Status
Change from base Build 1136: 0.08%
Covered Lines: 3517
Relevant Lines: 5346

💛 - Coveralls

@@ -19,6 +19,18 @@ type Valuer struct {
value reflect.Value
}

var marshalerType = reflect.TypeOf((*marshaler)(nil)).Elem()

func nonPointerMarshal(d *Descriptor, val reflect.Value) (reflect.Value, bool) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: add a comment here explaining exactly what this is doing

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1
Naming the return types might help too (especially the bool)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

@stephen stephen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm with nits


func nonPointerMarshal(d *Descriptor, val reflect.Value) (reflect.Value, bool) {
if !d.Ptr && reflect.PtrTo(d.Type).Implements(marshalerType) {
fmt.Printf("typ: %+v, intf %v, intftyp: %T\n", d, val.Interface(), val.Interface())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove print?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoooops

@@ -229,8 +229,7 @@ func TestField_ValidateSQLType(t *testing.T) {
{In: ifaceBinaryMarshal{}, Error: true},
{In: ifaceBinaryMarshal{}, Tags: []string{"binary"}, Error: false},
{In: &ifaceBinaryMarshal{}, Tags: []string{"binary"}, Error: false},
// Non-pointer proto does not work because the Marshal() method has a pointer receiver.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love it

@berfarah berfarah force-pushed the bf/non-ptr-proto-support branch from 0ff8421 to 420cf3e Compare October 10, 2018 17:59
@berfarah
Copy link
Contributor Author

As noted in my second commit with the benchmark, we see an increase of 1 allocation on the write path here (5 vs the typical 4). We're explicitly ok with this cost, given that:

  1. We prefer being able to support non-pointers
  2. It's opt-in (sql:",binary")
  3. It's on the write path

What we see here is an additional allocation for the non-ptr proto to
the heap. We expect this, since we have to create a new pointer in order
to satisfy the marshaler interface.

We deem this cost worth the additional safety of not having to deal with
pointers. Since people have to opt-in to use this feature there isn't a
noticeable difference to existing code-paths.
@berfarah berfarah force-pushed the bf/non-ptr-proto-support branch from 420cf3e to 25dfb08 Compare October 10, 2018 18:03
@berfarah berfarah merged commit d409814 into master Oct 10, 2018
@berfarah berfarah deleted the bf/non-ptr-proto-support branch October 10, 2018 18:06
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants