From ffd723e4b991064f406d7e2644d67f8f76a193f2 Mon Sep 17 00:00:00 2001 From: Nischal Sheth Date: Fri, 16 Sep 2016 09:09:36 -0700 Subject: [PATCH] Reduce memory allocation overhead in update generation code 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 --- src/ifmap/ifmap_encoder.cc | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/ifmap/ifmap_encoder.cc b/src/ifmap/ifmap_encoder.cc index 034518127f8..f65dba61c5e 100644 --- a/src/ifmap/ifmap_encoder.cc +++ b/src/ifmap/ifmap_encoder.cc @@ -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();