Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118320 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 7027 invoked from network); 30 Jul 2022 07:59:04 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Jul 2022 07:59:04 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 777A4180380 for ; Sat, 30 Jul 2022 02:58:04 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 30 Jul 2022 02:58:00 -0700 (PDT) Received: by mail-pl1-f177.google.com with SMTP id b22so6534823plz.9 for ; Sat, 30 Jul 2022 02:58:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc; bh=j5G8YhLLAEpjI2hsgGpJvB/22Xh+O1Dc9oQGeVYW6gc=; b=RZvpFbbmy4CiWQpRe+66F9tEEcyAtX2666Q35nNYWPQcux7NLAYAUYKrOiL+0t/eOW WHcKCOdW2HXlvlx29eWnzNWKy8Jf2oHsDI5DZUogYkhFD+LSSXEYSzCAuZVdOQs381Mu MySVx8b8YUoSZ/Iw8StBTYSnIWCDcLqd7zrv9306AxKv0lI0jqYgJbuuJfRktZv4g6Wo jnFrcncnxtbDuSuThvIZ6gB5fRUKK4DlvjPILbEJufTX7H2LAZN4BO5n2K5W1HmMv6QH Kc+EvS+QH4D9mk3fceuwW+g5xfBxJPYlILU01BQtKLhXvI6nzR/2BNo6wH3PPLpq7PNl zV5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=j5G8YhLLAEpjI2hsgGpJvB/22Xh+O1Dc9oQGeVYW6gc=; b=l75YKRr5PA2DNhUNrS13KgpYZ9DWWnYRsjbLJLAZ9C+ofESkTSyP9M2wICugCB7oEJ uuL8aXUBa3lu82GP41h6kCwiIadaNI1k+iucMOO7Niyii2KNKpmnElFBo+DrX66fz30f 0o/ka2RSOMlcjjGSefSv/98n+mxz7phaEKU6DrQyTjr2GZTIRQ562VqiGTgu9ICBzjrL oXwwc7t8nGOxYiTIK6XxFq+qXJle54Q0kwXfN71VDcglsMAq+aZRgc2SB31gBa2WQHSq UP/Dj+02F3khJcVIjaeS6Aif/ozAVX+NIvPRBRoOCO85UmpSUd/nL2JjFS9OBXUaCpFZ P5qQ== X-Gm-Message-State: ACgBeo1bTMXC+UtBlkqGyJv7ybUx2KNwssk1HVRuEsBP6kPO8h/yZbb3 Ciltp8r44zO/PARrW6mtYnJHV7iIMF5FK9uW2fE= X-Google-Smtp-Source: AA6agR4vHIfFfsfSiJp17BdZaCRMsoMf97IgBDFtpQyuhHPbav06FvvC7DL6gzITdbbA+bkqXAuSpXmSGdd3T1Is35c= X-Received: by 2002:a17:902:f641:b0:16d:351d:c1ea with SMTP id m1-20020a170902f64100b0016d351dc1eamr7471899plg.174.1659175079843; Sat, 30 Jul 2022 02:57:59 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 30 Jul 2022 13:57:48 +0400 Message-ID: To: juan carlos morales Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000411abb05e502cde3" Subject: Re: [PHP-DEV] RFC Idea - is_json - looking for feedback From: oleksii.bulba@gmail.com (Oleksii Bulba) --000000000000411abb05e502cde3 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable > $memoryAfter =3D memory_get_usage(true) / 1024 / 1024; I see that you used `memory_get_usage` that shows memory usage at the time of the function call. As far as I understand, your function does not return any value, so I suspect that it is obvious that the memory usage after the function call is the same as before. But what is the actual memory usage during the function call? Can you run the same benchmark but with `memory_get_peak_usage` function to see how your function uses memory? # $memoryAfter =3D memory_get_peak_usage(true) / 1024 / 1024; Also, I'm concerned if it would be better to name the function `is_json_valid`? On Sat, Jul 30, 2022 at 10:37 AM juan carlos morales < dev.juan.morales@gmail.com> wrote: > Just want to clarify that when I mentioned the use of memory, I wrote dow= n > the function "memory_get_usage()", which basically gives us the memory > handle by php that is related to the memory_limit INI setting. > > Now I will provide a benchmark I have done really quick: > > # Code used (I have the implementation of is_json() done already) > > // make sure you set your memory limit to -1 before running this code > // Here we create a very very very big json string, really big > $limit =3D 1000000; > $jsonString =3D '{ "test": { "foo": "bar" },'; > > for ($i=3D0; $i < $limit; $i++) { > $jsonString .=3D " \"test$i\": { \"foo\": { \"test\" : { \"foo\" : { > \"test\" : { \"foo\" : \"bar\" }}}}},"; > } > > $jsonString .=3D ' "testXXXX": { "foo": "replaceme" } }'; > //{ "test" : { "foo" : "bar" }}} > > $memoryBefore =3D memory_get_usage(true) / 1024 / 1024; > echo "Megas used before call: " . $memoryBefore . PHP_EOL; > > $start =3D microtime(true); > > json_decode($jsonString, null, $limit, 0); > //<------------------ un/comment to show/hide results for json_decode() > //is_json($jsonString); > //<------------------ un/comment to show/hide results for is_json() > > $memoryAfter =3D memory_get_usage(true) / 1024 / 1024; > echo "Megas used after call: " . $memoryAfter . PHP_EOL; > > echo "Difference: " . ($memoryAfter - $memoryBefore) . PHP_EOL; > > echo "Time: " . (microtime(true) - $start) . " seconds" . PHP_EOL; > return; > > # Results > ## json_decode() > Megas used before call: 79.23828125 > Megas used after call: 3269.23828125 > Difference: 3190 > Time: 12.091101884842 seconds > > ## is_json() > Megas used before call: 79.23828125 > Megas used after call: 79.23828125 > Difference: 0 > Time: 5.4537169933319 seconds > > > And yes, I am open to share the implementation, but after I write the RFC= . > > Thanks for taking your time to give me a feedback. > > El s=C3=A1b, 30 jul 2022 a las 3:50, Jordan LeDoux () > escribi=C3=B3: > > > > > > > On Fri, Jul 29, 2022 at 7:27 AM juan carlos morales < > > dev.juan.morales@gmail.com> wrote: > > > >> # Why this function ? > >> > >> At the moment the only way to determine if a JSON-string is valid we > have > >> to execute the json_decode() function. > >> > >> The drawback about this, is that json_decode() generates an in memory = an > >> object/array (depending on parameters) while parsing the string; this > >> leads > >> to a memory usage that is not needed (because we use memory for creati= ng > >> the object/array) and also can cause an error for reaching the > >> memory-limit > >> of the php process. > >> > >> Sometimes we just need to know is the string is a valid json or not, a= nd > >> nothing else. > >> > > > > You say that you have a first-pass at the implementation done. I'd be > > curious to see it. My initial thought was that in order to validate the > > string, you likely need to allocate extra memory as part of the > validation > > that depends on the string size. You'd definitely save the overhead of = a > > ZVAL, but for such an object that overhead is likely negligible. > > > > So I guess my question would be: in the actual implementation that land= s, > > how much memory would this actually save compared to json_decode()? Thi= s > > seems like it would make the RFC tricky, as the purported benefit of th= e > > RFC depends very tightly on the implementation that lands. > > > > Jordan > > > --000000000000411abb05e502cde3--