Skip to content

Commit

Permalink
Reduce memory allocation overhead in update generation code
Browse files Browse the repository at this point in the history
Note that pugixml library allocates memory for a document in increments
of 32KB pages and then manages smaller allocations (nodes or attributes)
using these pages. Calling the reset method on a document frees all the
pages. Hence a 32B page allocation/free happens for each config message
sent to a client. Instead, removing the only child node of the document
ensures that pugixml does only a single 32KB allocation per document and
recycles the same memory when building the tree for each message.

Change-Id: Id56d5ad6c45fc2b6bedfe402d4758b454efc183e
Partial-Bug: 1602347
  • Loading branch information
Nischal Sheth committed Sep 16, 2016
1 parent db7b05d commit ffd723e
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/ifmap/ifmap_encoder.cc
Expand Up @@ -106,8 +106,21 @@ bool IFMapMessage::IsEmpty() {
return ((node_count_ == 0) ? true : false);
}

//
// Reset the IFMapMessage to initial state so that it can be used to build
// the next config message.
//
// Using remove_child to remove the only child of the document is a better
// way to clear the document than using reset. The pugixml library allocates
// memory for a document in increments of 32KB pages and then manages smaller
// allocations (nodes or attributes) using these pages. Calling reset method
// on a document frees all the pages. In contrast, removing the only child
// node of the document returns the smaller allocations to the free pool of
// memory for the document, but doesn't free the pages themselves. This lets
// the library reuse the same memory when building the tree for each message.
//
void IFMapMessage::Reset() {
doc_.reset();
doc_.remove_child("iq");
node_count_ = 0;
op_type_ = NONE;
Open();
Expand Down

0 comments on commit ffd723e

Please sign in to comment.