-
-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #330 from alpineiq/v5
small optimizations.
- Loading branch information
Showing
14 changed files
with
198 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package msgpack | ||
|
||
import ( | ||
"reflect" | ||
"sync" | ||
) | ||
|
||
var cachedValues struct { | ||
m map[reflect.Type]chan reflect.Value | ||
sync.RWMutex | ||
} | ||
|
||
func cachedValue(t reflect.Type) reflect.Value { | ||
cachedValues.RLock() | ||
ch := cachedValues.m[t] | ||
cachedValues.RUnlock() | ||
if ch != nil { | ||
return <-ch | ||
} | ||
|
||
cachedValues.Lock() | ||
defer cachedValues.Unlock() | ||
if ch = cachedValues.m[t]; ch != nil { | ||
return <-ch | ||
} | ||
|
||
ch = make(chan reflect.Value, 256) | ||
go func() { | ||
for { | ||
ch <- reflect.New(t) | ||
} | ||
}() | ||
if cachedValues.m == nil { | ||
cachedValues.m = make(map[reflect.Type]chan reflect.Value, 8) | ||
} | ||
cachedValues.m[t] = ch | ||
return <-ch | ||
} | ||
|
||
func (d *Decoder) newValue(t reflect.Type) reflect.Value { | ||
if d.flags&usePreallocateValues == 0 { | ||
return reflect.New(t) | ||
} | ||
|
||
return cachedValue(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.