+/* Try to share an object against the shared objects pool */
+static robj *tryObjectSharing(robj *o) {
+ struct dictEntry *de;
+ unsigned long c;
+
+ if (server.shareobjects == 0) return o;
+
+ assert(o->type == REDIS_STRING);
+ de = dictFind(server.sharingpool,o);
+ if (de) {
+ robj *shared = dictGetEntryKey(de);
+
+ c = ((unsigned long) dictGetEntryVal(de))+1;
+ dictGetEntryVal(de) = (void*) c;
+ incrRefCount(shared);
+ decrRefCount(o);
+ return shared;
+ } else {
+ /* Here we are using a stream algorihtm: Every time an object is
+ * shared we increment its count, everytime there is a miss we
+ * recrement the counter of a random object. If this object reaches
+ * zero we remove the object and put the current object instead. */
+ if (dictGetHashTableUsed(server.sharingpool) >=
+ server.sharingpoolsize) {
+ de = dictGetRandomKey(server.sharingpool);
+ assert(de != NULL);
+ c = ((unsigned long) dictGetEntryVal(de))-1;
+ dictGetEntryVal(de) = (void*) c;
+ if (c == 0) {
+ dictDelete(server.sharingpool,de->key);
+ }
+ } else {
+ c = 0; /* If the pool is empty we want to add this object */
+ }
+ if (c == 0) {
+ int retval;
+
+ retval = dictAdd(server.sharingpool,o,(void*)1);
+ assert(retval == DICT_OK);
+ incrRefCount(o);
+ }
+ return o;
+ }
+}
+