diff options
author | Petri Lehtinen <petri@digip.org> | 2010-02-08 20:51:09 +0200 |
---|---|---|
committer | Petri Lehtinen <petri@digip.org> | 2010-02-08 20:51:09 +0200 |
commit | 7e8b128740336bad50e32bbe9dc86f47b406ce6a (patch) | |
tree | a22b1fe63b8e85802f935d145bfe55f3b8aded29 /src | |
parent | acec2559a5da3390dcf2728eb37e69ecedf33267 (diff) | |
download | jansson-7e8b128740336bad50e32bbe9dc86f47b406ce6a.zip jansson-7e8b128740336bad50e32bbe9dc86f47b406ce6a.tar.gz jansson-7e8b128740336bad50e32bbe9dc86f47b406ce6a.tar.bz2 |
C++: Optimize PropertyProxy
When the property already exists in the object, we can store an
iterator pointing to that property, instead of duplicating the key.
When the property (key) is not present in the object, we still have to
duplicate the key.
Diffstat (limited to 'src')
-rw-r--r-- | src/jansson.hpp | 3 | ||||
-rw-r--r-- | src/jansson.ipp | 21 |
2 files changed, 18 insertions, 6 deletions
diff --git a/src/jansson.hpp b/src/jansson.hpp index ad6d177..efb4b52 100644 --- a/src/jansson.hpp +++ b/src/jansson.hpp @@ -201,6 +201,9 @@ namespace json { // array object we wrap json_t* _object; + // iterator pointing to property + void* _iter; + // key of property char* _key; }; diff --git a/src/jansson.ipp b/src/jansson.ipp index fd3a1bf..e965ef7 100644 --- a/src/jansson.ipp +++ b/src/jansson.ipp @@ -338,14 +338,17 @@ namespace json { } PropertyProxy::PropertyProxy(json_t* object, const char* key) - : _object(object) { - _key = strdup(key); + : _object(object), _key(0) { + _iter = json_object_iter_at(object, key); + if(!_iter) + _key = strdup(key); json_incref(_object); } PropertyProxy::PropertyProxy(const PropertyProxy& other) - : _object(other._object) { - _key = strdup(other._key); + : _object(other._object), _iter(other._iter), _key(0) { + if(other._key) + _key = strdup(other._key); json_incref(_object); } @@ -356,12 +359,18 @@ namespace json { // assign value to proxied object property PropertyProxy& PropertyProxy::operator=(const Value& value) { - json_object_set(_object, _key, value.as_json()); + if(_iter) + json_object_iter_set(_object, _iter, value.as_json()); + else + json_object_set(_object, _key, value.as_json()); return *this; } json_t* PropertyProxy::as_json() const { - return json_object_get(_object, _key); + if(_iter) + return json_object_iter_value(_iter); + else + return json_object_get(_object, _key); } } // namespace json::detail |