Skip to content

Commit

Permalink
Use a profiler to improve linker performance (#291)
Browse files Browse the repository at this point in the history
After measuring the impact of #286, #287, and #290 and seeing it to be
too modest. I decided to use a memory profiler, and it found "the good
stuff".

These changes had the largest impact on allocations and performance.
When linking inputs that come from descriptor protos (as opposed to
inputs that are compiled from sources and have ASTs), this resulted in
a 23% reduction in latency and 70% reduction in allocations.

This change features the following improvements:
1. `ast.NoSourceNode` now has a pointer receiver, so wrapping one in an
`ast.Node` interface value doesn't incur an allocation to put the value
on the heap. This also updates `parser.ParseResult` to refer to a single
`*ast.NoSourceNode` when it has no AST, instead of allocating one in
each call to get a node value. The `NoSourceNode`'s underlying type is
now `ast.FileInfo` so that it can be allocation-free, even for the
`NodeInfo` method (which previously was allocating a new `FileInfo` each
time).
3. Don't allocate a slice to hold the set of checked files for each
element being resolved. Instead, we allocate a single slice up front,
and re-use that throughout.
4. Don't pro-actively allocate strings that only are used for error
messages; instead defer construction of the change to the construction
of the error.
  • Loading branch information
jhump authored Apr 22, 2024
1 parent 93923d2 commit 016b009
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 107 deletions.
2 changes: 1 addition & 1 deletion ast/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ type EnumValueDeclNode interface {
}

var _ EnumValueDeclNode = (*EnumValueNode)(nil)
var _ EnumValueDeclNode = NoSourceNode{}
var _ EnumValueDeclNode = (*NoSourceNode)(nil)

// EnumValueNode represents an enum declaration. Example:
//
Expand Down
4 changes: 2 additions & 2 deletions ast/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var _ FieldDeclNode = (*FieldNode)(nil)
var _ FieldDeclNode = (*GroupNode)(nil)
var _ FieldDeclNode = (*MapFieldNode)(nil)
var _ FieldDeclNode = (*SyntheticMapField)(nil)
var _ FieldDeclNode = NoSourceNode{}
var _ FieldDeclNode = (*NoSourceNode)(nil)

// FieldNode represents a normal field declaration (not groups or maps). It
// can represent extension fields as well as non-extension fields (both inside
Expand Down Expand Up @@ -394,7 +394,7 @@ type OneofDeclNode interface {

var _ OneofDeclNode = (*OneofNode)(nil)
var _ OneofDeclNode = (*SyntheticOneof)(nil)
var _ OneofDeclNode = NoSourceNode{}
var _ OneofDeclNode = (*NoSourceNode)(nil)

// OneofNode represents a one-of declaration. Example:
//
Expand Down
2 changes: 1 addition & 1 deletion ast/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type FileDeclNode interface {
}

var _ FileDeclNode = (*FileNode)(nil)
var _ FileDeclNode = NoSourceNode{}
var _ FileDeclNode = (*NoSourceNode)(nil)

// FileNode is the root of the AST hierarchy. It represents an entire
// protobuf source file.
Expand Down
2 changes: 1 addition & 1 deletion ast/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type MessageDeclNode interface {
var _ MessageDeclNode = (*MessageNode)(nil)
var _ MessageDeclNode = (*SyntheticGroupMessageNode)(nil)
var _ MessageDeclNode = (*SyntheticMapEntryNode)(nil)
var _ MessageDeclNode = NoSourceNode{}
var _ MessageDeclNode = (*NoSourceNode)(nil)

// MessageNode represents a message declaration. Example:
//
Expand Down
58 changes: 28 additions & 30 deletions ast/no_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,104 +41,102 @@ func (s unknownSpan) End() SourcePos {
// NoSourceNode is a placeholder AST node that implements numerous
// interfaces in this package. It can be used to represent an AST
// element for a file whose source is not available.
type NoSourceNode struct {
filename string
}
type NoSourceNode FileInfo

// NewNoSourceNode creates a new NoSourceNode for the given filename.
func NewNoSourceNode(filename string) NoSourceNode {
return NoSourceNode{filename: filename}
func NewNoSourceNode(filename string) *NoSourceNode {
return &NoSourceNode{name: filename}
}

func (n NoSourceNode) Name() string {
return n.filename
func (n *NoSourceNode) Name() string {
return n.name
}

func (n NoSourceNode) Start() Token {
func (n *NoSourceNode) Start() Token {
return 0
}

func (n NoSourceNode) End() Token {
func (n *NoSourceNode) End() Token {
return 0
}

func (n NoSourceNode) NodeInfo(Node) NodeInfo {
func (n *NoSourceNode) NodeInfo(Node) NodeInfo {
return NodeInfo{
fileInfo: &FileInfo{name: n.filename},
fileInfo: (*FileInfo)(n),
}
}

func (n NoSourceNode) GetSyntax() Node {
func (n *NoSourceNode) GetSyntax() Node {
return n
}

func (n NoSourceNode) GetName() Node {
func (n *NoSourceNode) GetName() Node {
return n
}

func (n NoSourceNode) GetValue() ValueNode {
func (n *NoSourceNode) GetValue() ValueNode {
return n
}

func (n NoSourceNode) FieldLabel() Node {
func (n *NoSourceNode) FieldLabel() Node {
return n
}

func (n NoSourceNode) FieldName() Node {
func (n *NoSourceNode) FieldName() Node {
return n
}

func (n NoSourceNode) FieldType() Node {
func (n *NoSourceNode) FieldType() Node {
return n
}

func (n NoSourceNode) FieldTag() Node {
func (n *NoSourceNode) FieldTag() Node {
return n
}

func (n NoSourceNode) FieldExtendee() Node {
func (n *NoSourceNode) FieldExtendee() Node {
return n
}

func (n NoSourceNode) GetGroupKeyword() Node {
func (n *NoSourceNode) GetGroupKeyword() Node {
return n
}

func (n NoSourceNode) GetOptions() *CompactOptionsNode {
func (n *NoSourceNode) GetOptions() *CompactOptionsNode {
return nil
}

func (n NoSourceNode) RangeStart() Node {
func (n *NoSourceNode) RangeStart() Node {
return n
}

func (n NoSourceNode) RangeEnd() Node {
func (n *NoSourceNode) RangeEnd() Node {
return n
}

func (n NoSourceNode) GetNumber() Node {
func (n *NoSourceNode) GetNumber() Node {
return n
}

func (n NoSourceNode) MessageName() Node {
func (n *NoSourceNode) MessageName() Node {
return n
}

func (n NoSourceNode) OneofName() Node {
func (n *NoSourceNode) OneofName() Node {
return n
}

func (n NoSourceNode) GetInputType() Node {
func (n *NoSourceNode) GetInputType() Node {
return n
}

func (n NoSourceNode) GetOutputType() Node {
func (n *NoSourceNode) GetOutputType() Node {
return n
}

func (n NoSourceNode) Value() interface{} {
func (n *NoSourceNode) Value() interface{} {
return nil
}

func (n NoSourceNode) RangeOptions(func(*OptionNode) bool) {
func (n *NoSourceNode) RangeOptions(func(*OptionNode) bool) {
}
4 changes: 2 additions & 2 deletions ast/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type OptionDeclNode interface {
}

var _ OptionDeclNode = (*OptionNode)(nil)
var _ OptionDeclNode = NoSourceNode{}
var _ OptionDeclNode = (*NoSourceNode)(nil)

// OptionNode represents the declaration of a single option for an element.
// It is used both for normal option declarations (start with "option" keyword
Expand Down Expand Up @@ -410,4 +410,4 @@ var _ NodeWithOptions = RPCDeclNode(nil)
var _ NodeWithOptions = FieldDeclNode(nil)
var _ NodeWithOptions = EnumValueDeclNode(nil)
var _ NodeWithOptions = (*ExtensionRangeNode)(nil)
var _ NodeWithOptions = NoSourceNode{}
var _ NodeWithOptions = (*NoSourceNode)(nil)
2 changes: 1 addition & 1 deletion ast/ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ type RangeDeclNode interface {
}

var _ RangeDeclNode = (*RangeNode)(nil)
var _ RangeDeclNode = NoSourceNode{}
var _ RangeDeclNode = (*NoSourceNode)(nil)

// RangeNode represents a range expression, used in both extension ranges and
// reserved ranges. Example:
Expand Down
2 changes: 1 addition & 1 deletion ast/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ type RPCDeclNode interface {
}

var _ RPCDeclNode = (*RPCNode)(nil)
var _ RPCDeclNode = NoSourceNode{}
var _ RPCDeclNode = (*NoSourceNode)(nil)

// RPCNode represents an RPC declaration. Example:
//
Expand Down
2 changes: 1 addition & 1 deletion ast/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var _ ValueNode = (*SpecialFloatLiteralNode)(nil)
var _ ValueNode = (*SignedFloatLiteralNode)(nil)
var _ ValueNode = (*ArrayLiteralNode)(nil)
var _ ValueNode = (*MessageLiteralNode)(nil)
var _ ValueNode = NoSourceNode{}
var _ ValueNode = (*NoSourceNode)(nil)

// StringValueNode is an AST node that represents a string literal.
// Such a node can be a single literal (*StringLiteralNode) or a
Expand Down
Loading

0 comments on commit 016b009

Please sign in to comment.