Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107911 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 41551 invoked from network); 12 Dec 2019 19:15:17 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 12 Dec 2019 19:15:17 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id E42B22D1FE3 for ; Thu, 12 Dec 2019 09:13:59 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp3.php.net X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: X-Spam-Virus: No Received: from mail-lj1-x22f.google.com (mail-lj1-x22f.google.com [IPv6:2a00:1450:4864:20::22f]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp3.php.net (Postfix) with ESMTPS for ; Thu, 12 Dec 2019 09:13:58 -0800 (PST) Received: by mail-lj1-x22f.google.com with SMTP id 21so3159419ljr.0 for ; Thu, 12 Dec 2019 09:13:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=NWS38VWDlluszArE7SjyoYOWMQrCntlGItadwczwpsU=; b=n/vwrfpNPhSBXz8uae/l2go3TGWPbAc/xK/Wh85QHUgoX/rKLetHuBxawag4maI1gd eKDS2w6ERfh4jMxiJYpXiXJQutRXlrP0DR708f/Bdk2V1XsQl4vDoooge7zsHknxZHpd M2+twD3GkZy6td9/KjGD4BqYScnndhW6Le0P1f3Gp9eWOJruUiRLSYJW39tKeDe9wFwf H35Wa4RzRsJYabSQrN8Z9hKF8BgB4CjWIT5x2HBbtkyPeVQ4hU2ECc9cpDVqL6k7Gokj eNjbZp1tgj+Puy+kGtdJfr/a4n/FrU0M3MwCJFD/V3pENPcc+fFti5igw3vHPwR1fzLJ zKwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=NWS38VWDlluszArE7SjyoYOWMQrCntlGItadwczwpsU=; b=EN6iBdUhDQe2sWAh23vki/KJNpKUlp7ZdOcKLmhoPgQ9laVIyCRB2GH7Baa7WUDqJy uSFyHxXt8PPfracdJk3pA8+RbvfKSpSuLkQQHye6KhSZagKZE1sCG1uQM4pVEEjeN06w i+lUS6KaNXeDDQTzYNNVR5Ul2xHlM7xdXJpjCwxAyAj9k9YlY+OWNHKcdiW5dRP9IYxC FYzS4uRG+wxWo1ZIbFFlz9V0CPlJh7yaUS0BBIf/ZTwiL0rdN1XwIIgX4xIro8c2t1p8 sESeVOjMciluo2sUBloWkR4Dry99LuRJMKIr5nnbRim8J/D0ph5RPypIT/2lqM1zXzgK z1LQ== X-Gm-Message-State: APjAAAVTjxTtY5X2QaGaglOmPhesxPbSIMiypV2OevY273jGRehOoByd 35oqd0pblEfmR2yoEm3JOmLCzqYjSxJRmziFiS3k63lIpR8= X-Google-Smtp-Source: APXvYqwg7yof30UM7QYZSK681f2Znwlrzb4eiV6DgQSlNF2EOfOoMDdZnOujEoFVBq1YCBnDfNwfctSCWMuusRtXuCY= X-Received: by 2002:a2e:8606:: with SMTP id a6mr6672999lji.119.1576170837024; Thu, 12 Dec 2019 09:13:57 -0800 (PST) MIME-Version: 1.0 References: <1d2d5c23-3864-a202-92a8-34228798510d@birkholz.biz> In-Reply-To: Date: Thu, 12 Dec 2019 18:13:40 +0100 Message-ID: To: Dennis Birkholz Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000d8a2a4059984de09" X-Envelope-From: Subject: Re: [PHP-DEV] Re: [RFC] Add WeakMap From: nikita.ppv@gmail.com (Nikita Popov) --000000000000d8a2a4059984de09 Content-Type: text/plain; charset="UTF-8" On Tue, Dec 10, 2019 at 12:03 PM Dennis Birkholz wrote: > Hi Nikita, > > On 06.12.19 11:29, Nikita Popov wrote: > > Could you provide some context on why you think serialization support for > > WeakMap is important? As weak maps are essentially caching structures, > > serializing them doesn't seem particularly useful in the first place, but > > when combined with the quite unintuitive behavior the serialization would > > have, I feel that it is better to leave this to the user (same as > > WeakReference). > > structures provided by the PHP core tend to be used in the wild. As PHP > lacks a method to check whether a given object (and all other objects > contained within it) can be serialized (without traversing the complete > object graph), each new always-available data structure that is not > serializable increases the risk to encounter an object that is not > serializable. That is the reason I prefer new data structures to be > serializable. > > What I see coming is something like this: some kind of object that can > contain other object and attach some meta information to that objects > (that is stored as the value in a weak map). When an object is removed > from the collection, the meta information gets removed eventually, no > need to manually clear it in the remove-object method. If there are many > different kinds of meta information this would save a lot of code in the > remove method! Even though this seems to not be the intended use case, > programmers tend to safe some key strokes here and there. That type of > container object is not serializable unless the programmer takes extra > steps and implements serialization him/herself. > > > Specifically what I mean by uninituitive is this: When you do a $s = > > serialize($weakMap), you'll get back a large payload string, but when you > > then try to do an unserialize($s) you'll get back an empty WeakMap (or > > worse: a weak map that will only become empty on the next GC), because > all > > of those objects will get removed as soon as unserialization is > finalized. > > That "works", but doesn't seem terribly useful and is likely doing to be > a > > wtf moment. > > Ok, my intention was to have a more sophisticated approach: when the > WeakMap is serialized, only objects in the object graph that is > serialized are considered alive, all other objects are not serialized. > So directly serializing a WeakMap would result in an empty map but > serializing an object that contains a list of objects and a WeakMap > containing some of the same child objects would create a meaningful > payload string and unserialize would reconstruct the same object with > only objects from the child list available in the WeakMap any more. > > I understand that this may complicate the implementation a lot (or even > be not possible). This is indeed not possible. When we serialize the WeakMap, we do not know what else will be serialized as well. We can only serialize everything and let unserialization discard objects that are no longer live. > But my just want to repeat my main concern: buildin > data structures that are not serializable are a real problem for users > that use serialization extensively. Maybe the solution to that problem > is a method to check whether a provided object graph can be serialized > (which may not be possible due to throwing an exception in __sleep() or > something like that), some way to ignore unserializable elements or some > way to register callback methods to handle unserializable elements. > I'm must be missing something obvious here: Isn't this a reliable way to detect whether an object graph is serializable? try { $serialized = serialize($value); } catch (\Throwable $e) { // not serializable } Nikita --000000000000d8a2a4059984de09--