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

Remove defers from getTypeInfo functions #227

Merged
merged 2 commits into from
Oct 15, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,10 @@ func (cdc *Codec) setTypeInfo_nolock(info *TypeInfo) {
}

func (cdc *Codec) getTypeInfo_wlock(rt reflect.Type) (info *TypeInfo, err error) {
// We do not use defer cdc.mtx.Unlock() here due to performance overhead of
// defer in go1.11 (and prior versions). Ensure new code paths unlock the
// mutex.
cdc.mtx.Lock() // requires wlock because we might set.
defer cdc.mtx.Unlock()

// Dereference pointer type.
for rt.Kind() == reflect.Ptr {
Expand All @@ -370,55 +372,70 @@ func (cdc *Codec) getTypeInfo_wlock(rt reflect.Type) (info *TypeInfo, err error)
if !ok {
if rt.Kind() == reflect.Interface {
err = fmt.Errorf("Unregistered interface %v", rt)
cdc.mtx.Unlock()
return
}

info = cdc.newTypeInfoUnregistered(rt)
cdc.setTypeInfo_nolock(info)
}
cdc.mtx.Unlock()
return info, nil
}

// iinfo: TypeInfo for the interface for which we must decode a
// concrete type with prefix bytes pb.
func (cdc *Codec) getTypeInfoFromPrefix_rlock(iinfo *TypeInfo, pb PrefixBytes) (info *TypeInfo, err error) {
// We do not use defer cdc.mtx.Unlock() here due to performance overhead of
// defer in go1.11 (and prior versions). Ensure new code paths unlock the
// mutex.
cdc.mtx.RLock()
defer cdc.mtx.RUnlock()

infos, ok := iinfo.Implementers[pb]
if !ok {
err = fmt.Errorf("unrecognized prefix bytes %X", pb)
cdc.mtx.RUnlock()
return
}
if len(infos) > 1 {
err = fmt.Errorf("conflicting concrete types registered for %X: e.g. %v and %v", pb, infos[0].Type, infos[1].Type)
cdc.mtx.RUnlock()
return
}
info = infos[0]
cdc.mtx.RUnlock()
return
}

func (cdc *Codec) getTypeInfoFromDisfix_rlock(df DisfixBytes) (info *TypeInfo, err error) {
// We do not use defer cdc.mtx.Unlock() here due to performance overhead of
// defer in go1.11 (and prior versions). Ensure new code paths unlock the
// mutex.
cdc.mtx.RLock()
defer cdc.mtx.RUnlock()

info, ok := cdc.disfixToTypeInfo[df]
if !ok {
err = fmt.Errorf("unrecognized disambiguation+prefix bytes %X", df)
cdc.mtx.RUnlock()
return
}
cdc.mtx.RUnlock()
return
}

func (cdc *Codec) getTypeInfoFromName_rlock(name string) (info *TypeInfo, err error) {
// We do not use defer cdc.mtx.Unlock() here due to performance overhead of
// defer in go1.11 (and prior versions). Ensure new code paths unlock the
// mutex.
cdc.mtx.RLock()
defer cdc.mtx.RUnlock()

info, ok := cdc.nameToTypeInfo[name]
if !ok {
err = fmt.Errorf("unrecognized concrete type name %s", name)
cdc.mtx.RUnlock()
return
}
cdc.mtx.RUnlock()
return
}

Expand Down