Commit 842e1a9a authored by Rob Pike's avatar Rob Pike

Add a cache to avoid unnecessary parsing of type strings when constructing values

R=rsc
DELTA=12  (9 added, 0 deleted, 3 changed)
OCL=18916
CL=18921
parent 10137e7f
...@@ -695,6 +695,7 @@ func FuncCreator(typ Type, addr Addr) Value { ...@@ -695,6 +695,7 @@ func FuncCreator(typ Type, addr Addr) Value {
} }
var creator *map[int] Creator var creator *map[int] Creator
var typecache *map[string] *Type
func init() { func init() {
creator = new(map[int] Creator); creator = new(map[int] Creator);
...@@ -722,6 +723,8 @@ func init() { ...@@ -722,6 +723,8 @@ func init() {
creator[StructKind] = &StructCreator; creator[StructKind] = &StructCreator;
creator[InterfaceKind] = &InterfaceCreator; creator[InterfaceKind] = &InterfaceCreator;
creator[FuncKind] = &FuncCreator; creator[FuncKind] = &FuncCreator;
typecache = new(map[string] *Type);
} }
func NewValueAddr(typ Type, addr Addr) Value { func NewValueAddr(typ Type, addr Addr) Value {
...@@ -752,10 +755,16 @@ export func NewInitValue(typ Type) Value { ...@@ -752,10 +755,16 @@ export func NewInitValue(typ Type) Value {
export func NewValue(e Empty) Value { export func NewValue(e Empty) Value {
value, typestring := sys.reflect(e); value, typestring := sys.reflect(e);
p, ok := typecache[typestring];
if !ok {
typ := ParseTypeString("", typestring); typ := ParseTypeString("", typestring);
p = new(Type);
*p = typ;
typecache[typestring] = p;
}
// Content of interface is a value; need a permanent copy to take its address // Content of interface is a value; need a permanent copy to take its address
// so we can modify the contents. Values contain pointers to 'values'. // so we can modify the contents. Values contain pointers to 'values'.
ap := new(uint64); ap := new(uint64);
*ap = value; *ap = value;
return NewValueAddr(typ, PtrUint64ToAddr(ap)); return NewValueAddr(*p, PtrUint64ToAddr(ap));
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment