From 65e8b6d405273ae7a7334f00da1116641a79a6ef Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sat, 2 Aug 2014 15:37:01 +0200 Subject: [PATCH] Optimized size of JsonObjectBase indexer (-58 bytes) --- JsonGenerator/JsonObjectBase.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/JsonGenerator/JsonObjectBase.cpp b/JsonGenerator/JsonObjectBase.cpp index 98007b75..8bcd9510 100644 --- a/JsonGenerator/JsonObjectBase.cpp +++ b/JsonGenerator/JsonObjectBase.cpp @@ -39,17 +39,22 @@ size_t JsonObjectBase::printTo(Print& p) const JsonValue& JsonObjectBase::operator[](char const* key) { - for (int i = 0; i < count; ++i) + KeyValuePair* p = items; + + for (int i = count; i > 0; --i) { - if (!strcmp(items[i].key, key)) - { - return items[i].value; - } + bool keyMatches = strcmp(p->key, key) == 0; + + if (keyMatches) + return p->value; + + p++; } if (count >= capacity) return JsonValue::null(); - items[count].key = key; - return items[count++].value; + count++; + p->key = key; + return p->value; } \ No newline at end of file