[Pkg-owncloud-commits] [php-sabredav] 04/40: Destroying vobject objects after use.

David Prévot taffit at moszumanska.debian.org
Sat Sep 5 15:24:06 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to tag 3.1.0-alpha2
in repository php-sabredav.

commit 4a3cdfd8e93b983ff2c00eaba23fad63ea4843bc
Author: Evert Pot <me at evertpot.com>
Date:   Tue Jul 21 17:22:41 2015 -0400

    Destroying vobject objects after use.
    
    Should save memory for massive address books and calendars.
---
 CHANGELOG.md                           |  3 ++-
 lib/CalDAV/Backend/AbstractBackend.php |  7 ++++++-
 lib/CalDAV/Backend/PDO.php             |  3 +++
 lib/CalDAV/ICSExportPlugin.php         |  9 +++++++--
 lib/CalDAV/Plugin.php                  | 23 ++++++++++++++++++++++-
 lib/CalDAV/Schedule/Inbox.php          |  3 +++
 lib/CalDAV/Schedule/Plugin.php         | 19 +++++++++++++++++++
 lib/CardDAV/Plugin.php                 | 31 +++++++++++++++++++++++++------
 lib/CardDAV/VCFExportPlugin.php        |  7 +++++--
 9 files changed, 92 insertions(+), 13 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index afdde18..2c48e7d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,7 +4,8 @@ ChangeLog
 3.1.0-alpha2 (????-??-??)
 -------------------------
 
-* ...
+* Massive calendar and addressbooks should see a big drop in peak memory
+  usage.
 
 
 3.1.0-alpha1 (2015-07-19)
diff --git a/lib/CalDAV/Backend/AbstractBackend.php b/lib/CalDAV/Backend/AbstractBackend.php
index 3aef7a7..41f20fb 100644
--- a/lib/CalDAV/Backend/AbstractBackend.php
+++ b/lib/CalDAV/Backend/AbstractBackend.php
@@ -142,7 +142,12 @@ abstract class AbstractBackend implements BackendInterface {
         $vObject = VObject\Reader::read($object['calendardata']);
 
         $validator = new CalDAV\CalendarQueryValidator();
-        return $validator->validate($vObject, $filters);
+        $result = $validator->validate($vObject, $filters);
+
+        // Destroy circular references to PHP will GC the object.
+        $vObject->destroy();
+
+        return $result;
 
     }
 
diff --git a/lib/CalDAV/Backend/PDO.php b/lib/CalDAV/Backend/PDO.php
index 8447360..c35ffe2 100644
--- a/lib/CalDAV/Backend/PDO.php
+++ b/lib/CalDAV/Backend/PDO.php
@@ -585,6 +585,9 @@ class PDO extends AbstractBackend implements SyncSupport, SubscriptionSupport, S
             }
         }
 
+        // Destroy circular references to PHP will GC the object.
+        $vObject->destroy();
+
         return [
             'etag'           => md5($calendarData),
             'size'           => strlen($calendarData),
diff --git a/lib/CalDAV/ICSExportPlugin.php b/lib/CalDAV/ICSExportPlugin.php
index 5bde442..3a03955 100644
--- a/lib/CalDAV/ICSExportPlugin.php
+++ b/lib/CalDAV/ICSExportPlugin.php
@@ -235,6 +235,8 @@ class ICSExportPlugin extends DAV\ServerPlugin {
                 // VTIMEZONE.
                 $vtimezoneObj = VObject\Reader::read($tzResult[$tzProp]);
                 $calendarTimeZone = $vtimezoneObj->VTIMEZONE->getTimeZone();
+                // Destroy circular references to PHP will GC the object.
+                $vtimezoneObj->destroy();
                 unset($vtimezoneObj);
             } else {
                 // Defaulting to UTC.
@@ -298,7 +300,7 @@ class ICSExportPlugin extends DAV\ServerPlugin {
                     case 'VEVENT' :
                     case 'VTODO' :
                     case 'VJOURNAL' :
-                        $objects[] = $child;
+                        $objects[] = clone $child;
                         break;
 
                     // VTIMEZONE is special, because we need to filter out the duplicates
@@ -306,13 +308,16 @@ class ICSExportPlugin extends DAV\ServerPlugin {
                         // Naively just checking tzid.
                         if (in_array((string)$child->TZID, $collectedTimezones)) continue;
 
-                        $timezones[] = $child;
+                        $timezones[] = clone $child;
                         $collectedTimezones[] = $child->TZID;
                         break;
 
                 }
 
             }
+            // Destroy circular references to PHP will GC the object.
+            $nodeComp->destroy();
+            unset($nodeComp);
 
         }
 
diff --git a/lib/CalDAV/Plugin.php b/lib/CalDAV/Plugin.php
index 400cdad..f809808 100644
--- a/lib/CalDAV/Plugin.php
+++ b/lib/CalDAV/Plugin.php
@@ -462,6 +462,9 @@ class Plugin extends DAV\ServerPlugin {
                 } else {
                     $objProps[200]['{' . self::NS_CALDAV . '}calendar-data'] = $vObject->serialize();
                 }
+                // Destroy circular references so PHP will garbage collect the
+                // object.
+                $vObject->destroy();
             }
 
             $propertyList[] = $objProps;
@@ -509,7 +512,10 @@ class Plugin extends DAV\ServerPlugin {
                 // VTIMEZONE.
                 $vtimezoneObj = VObject\Reader::read($tzResult[$tzProp]);
                 $calendarTimeZone = $vtimezoneObj->VTIMEZONE->getTimeZone();
-                unset($vtimezoneObj);
+
+                // Destroy circular references so PHP will garbage collect the
+                // object.
+                $vtimezoneObj->destroy();
             } else {
                 // Defaulting to UTC.
                 $calendarTimeZone = new DateTimeZone('UTC');
@@ -572,6 +578,9 @@ class Plugin extends DAV\ServerPlugin {
                     $result = [$properties];
 
                 }
+                // Destroy circular references so PHP will garbage collect the
+                // object.
+                $vObject->destroy();
 
             }
 
@@ -613,6 +622,10 @@ class Plugin extends DAV\ServerPlugin {
                     } else {
                         $properties[200]['{' . self::NS_CALDAV . '}calendar-data'] = $vObject->serialize();
                     }
+
+                    // Destroy circular references so PHP will garbage collect the
+                    // object.
+                    $vObject->destroy();
                 }
                 $result[] = $properties;
 
@@ -659,6 +672,8 @@ class Plugin extends DAV\ServerPlugin {
         if (isset($calendarProps[$tzProp])) {
             $vtimezoneObj = VObject\Reader::read($calendarProps[$tzProp]);
             $calendarTimeZone = $vtimezoneObj->VTIMEZONE->getTimeZone();
+            // Destroy circular references so PHP will garbage collect the object.
+            $vtimezoneObj->destroy();
         } else {
             $calendarTimeZone = new DateTimeZone('UTC');
         }
@@ -898,6 +913,9 @@ class Plugin extends DAV\ServerPlugin {
 
         }
 
+        // Destroy circular references so PHP will garbage collect the object.
+        $vobj->destroy();
+
     }
 
 
@@ -960,6 +978,9 @@ class Plugin extends DAV\ServerPlugin {
         $jsonBody = json_encode($vobj->jsonSerialize());
         $response->setBody($jsonBody);
 
+        // Destroy circular references so PHP will garbage collect the object.
+        $vobj->destroy();
+
         $response->setHeader('Content-Type', 'application/calendar+json');
         $response->setHeader('Content-Length', strlen($jsonBody));
 
diff --git a/lib/CalDAV/Schedule/Inbox.php b/lib/CalDAV/Schedule/Inbox.php
index c3b3662..7fa4ec1 100644
--- a/lib/CalDAV/Schedule/Inbox.php
+++ b/lib/CalDAV/Schedule/Inbox.php
@@ -260,6 +260,9 @@ class Inbox extends DAV\Collection implements IInbox {
             if ($validator->validate($vObject, $filters)) {
                 $result[] = $object['uri'];
             }
+
+            // Destroy circular references to PHP will GC the object.
+            $vObject->destroy();
         }
         return $result;
 
diff --git a/lib/CalDAV/Schedule/Plugin.php b/lib/CalDAV/Schedule/Plugin.php
index 6832bdb..833f67d 100644
--- a/lib/CalDAV/Schedule/Plugin.php
+++ b/lib/CalDAV/Schedule/Plugin.php
@@ -293,6 +293,11 @@ class Plugin extends ServerPlugin {
 
         $this->processICalendarChange($oldObj, $vCal, $addresses, [], $modified);
 
+        if ($oldObj) {
+            // Destroy circular references so PHP will GC the object.
+            $oldObj->destroy();
+        }
+
     }
 
     /**
@@ -457,6 +462,12 @@ class Plugin extends ServerPlugin {
         $broker = new ITip\Broker();
         $newObject = $broker->processMessage($iTipMessage, $currentObject);
 
+        if ($currentObject) {
+            // Destroy circular references so PHP can garbage collect the object.
+            $currentObject->destroy();
+            unset($currentObject);
+        }
+
         $inbox->createFile($newFileName, $iTipMessage->message->serialize());
 
         if (!$newObject) {
@@ -636,6 +647,10 @@ class Plugin extends ServerPlugin {
             $acl && $acl->checkPrivileges($outboxPath, '{' . self::NS_CALDAV . '}schedule-query-freebusy');
             $this->handleFreeBusyRequest($outboxNode, $vObject, $request, $response);
 
+            // Destroy circular references so PHP can GC the object.
+            $vObject->destroy();
+            unset($vObject);
+
         } else {
 
             throw new NotImplemented('We only support VFREEBUSY (REQUEST) on this endpoint');
@@ -817,6 +832,10 @@ class Plugin extends ServerPlugin {
             if (isset($props[$ctz])) {
                 $vtimezoneObj = VObject\Reader::read($props[$ctz]);
                 $calendarTimeZone = $vtimezoneObj->VTIMEZONE->getTimeZone();
+
+                // Destroy circular references so PHP can garbage collect the object.
+                $vtimezoneObj->destroy();
+
             }
 
             // Getting the list of object uris within the time-range
diff --git a/lib/CardDAV/Plugin.php b/lib/CardDAV/Plugin.php
index f7e897a..589097f 100644
--- a/lib/CardDAV/Plugin.php
+++ b/lib/CardDAV/Plugin.php
@@ -372,6 +372,8 @@ class Plugin extends DAV\ServerPlugin {
             $modified = true;
         }
 
+        // Destroy circular references to PHP will GC the object.
+        $vobj->destroy();
     }
 
 
@@ -474,9 +476,9 @@ class Plugin extends DAV\ServerPlugin {
      */
     function validateFilters($vcardData, array $filters, $test) {
 
-        $vcard = VObject\Reader::read($vcardData);
 
         if (!$filters) return true;
+        $vcard = VObject\Reader::read($vcardData);
 
         foreach ($filters as $filter) {
 
@@ -523,14 +525,26 @@ class Plugin extends DAV\ServerPlugin {
             // There are two conditions where we can already determine whether
             // or not this filter succeeds.
             if ($test === 'anyof' && $success) {
+
+                // Destroy circular references to PHP will GC the object.
+                $vcard->destroy();
+
                 return true;
             }
             if ($test === 'allof' && !$success) {
+
+                // Destroy circular references to PHP will GC the object.
+                $vcard->destroy();
+
                 return false;
             }
 
         } // foreach
 
+
+        // Destroy circular references to PHP will GC the object.
+        $vcard->destroy();
+
         // If we got all the way here, it means we haven't been able to
         // determine early if the test failed or not.
         //
@@ -799,17 +813,22 @@ class Plugin extends DAV\ServerPlugin {
             default :
             case 'vcard3' :
                 $data = $data->convert(VObject\Document::VCARD30);
-                return $data->serialize();
+                $newResult = $data->serialize();
+                break;
             case 'vcard4' :
                 $data = $data->convert(VObject\Document::VCARD40);
-                return $data->serialize();
+                $newResult = $data->serialize();
+                break;
             case 'jcard' :
                 $data = $data->convert(VObject\Document::VCARD40);
-                return json_encode($data->jsonSerialize());
+                $newResult = json_encode($data->jsonSerialize());
+                break;
 
-        // @codeCoverageIgnoreStart
         }
-        // @codeCoverageIgnoreEnd
+        // Destroy circular references to PHP will GC the object.
+        $data->destroy();
+
+        return $newResult;
 
     }
 
diff --git a/lib/CardDAV/VCFExportPlugin.php b/lib/CardDAV/VCFExportPlugin.php
index 73d6646..53754b1 100644
--- a/lib/CardDAV/VCFExportPlugin.php
+++ b/lib/CardDAV/VCFExportPlugin.php
@@ -102,8 +102,11 @@ class VCFExportPlugin extends DAV\ServerPlugin {
             $nodeData = $node[200]['{' . Plugin::NS_CARDDAV . '}address-data'];
 
             // Parsing this node so VObject can clean up the output.
-            $output .=
-               VObject\Reader::read($nodeData)->serialize();
+            $vcard = VObject\Reader::read($nodeData);
+            $output .= $vcard->serialize();
+
+            // Destroy circular references to PHP will GC the object.
+            $vcard->destroy();
 
         }
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabredav.git



More information about the Pkg-owncloud-commits mailing list