From e31a91affed8fa9969da96ee8aec760a827a004b Mon Sep 17 00:00:00 2001 From: Jan Steemann Date: Thu, 5 Jun 2014 01:14:38 +0200 Subject: [PATCH] calculate lengths only once during dump() --- lib/ShapedJson/Legends.cpp | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/ShapedJson/Legends.cpp b/lib/ShapedJson/Legends.cpp index 8ea44778fa..48db9961db 100644 --- a/lib/ShapedJson/Legends.cpp +++ b/lib/ShapedJson/Legends.cpp @@ -107,14 +107,14 @@ int JsonLegend::addAttributeId (TRI_shape_aid_t aid) { } char const* p = _shaper->lookupAttributeId(_shaper, aid); - if (0 == p) { + if (nullptr == p) { return TRI_ERROR_AID_NOT_FOUND; } - _have_attribute.insert(aid); + _have_attribute.insert(it, aid); size_t len = strlen(p); _attribs.emplace_back(aid, _att_data.length()); - _att_data.appendText(p, len+1); // including the zero byte + _att_data.appendText(p, len + 1); // including the zero byte return TRI_ERROR_NO_ERROR; } @@ -133,7 +133,7 @@ int JsonLegend::addShape (TRI_shape_sid_t sid, int res = TRI_ERROR_NO_ERROR; - TRI_shape_t const* shape = 0; + TRI_shape_t const* shape = nullptr; // First the trivial cases: if (sid < TRI_FirstCustomShapeIdShaper()) { @@ -141,13 +141,13 @@ int JsonLegend::addShape (TRI_shape_sid_t sid, } else { shape = _shaper->lookupShapeId(_shaper, sid); - if (0 == shape) { + if (nullptr == shape) { return TRI_ERROR_LEGEND_INCOMPLETE; } unordered_set::const_iterator it = _have_shape.find(sid); if (it == _have_shape.end()) { - _have_shape.insert(sid); + _have_shape.insert(it, sid); Shape sh(sid, _shape_data.length(), shape->_size); _shapes.push_back(sh); _shape_data.appendText( reinterpret_cast(shape), @@ -303,13 +303,15 @@ void JsonLegend::dump (void* buf) { } // Add the length of the string data to socle for second table: - socle += roundup8(_att_data.length()); + size_t const attDataLength = _att_data.length(); + socle += roundup8(attDataLength); // shape table: + size_t const n = _shapes.size(); p = reinterpret_cast(a); - *p++ = _shapes.size(); + *p++ = n; Shape* s = reinterpret_cast(p); - for (i = 0; i < _shapes.size(); i++) { + for (i = 0; i < n; i++) { _shapes[i].offset += socle; *s++ = _shapes[i]; _shapes[i].offset -= socle; @@ -317,17 +319,18 @@ void JsonLegend::dump (void* buf) { // Attribute ID string data: char* c = reinterpret_cast(s); - memcpy(c, _att_data.c_str(), _att_data.length()); - i = roundup8(_att_data.length()); - if (i > _att_data.length()) { - memset( c + _att_data.length(), 0, i-_att_data.length()); + memcpy(c, _att_data.c_str(), attDataLength); + i = roundup8(attDataLength); + if (i > attDataLength) { + memset(c + attDataLength, 0, i - attDataLength); } c += i; // Shape data: - memcpy(c, _shape_data.c_str(), _shape_data.length()); - i = roundup8(_shape_data.length()); - if (i > _shape_data.length()) { - memset( c + _shape_data.length(), 0, i-_shape_data.length()); + size_t const shapeDataLength = _shape_data.length(); + memcpy(c, _shape_data.c_str(), shapeDataLength); + i = roundup8(shapeDataLength); + if (i > shapeDataLength) { + memset(c + shapeDataLength, 0, i - shapeDataLength); } }