Git Product home page Git Product logo

Comments (5)

alcaeus avatar alcaeus commented on June 18, 2024 3

This is going to be tricky and, I'm afraid, mostly unpleasant. I created two small sample scripts. Both read data from the same collection containing 10k records, only consisting of an ObjectID and a counter. One script reads from the table using MongoCollection, the other uses MongoDB\Collection:

$client = new MongoClient();
$collection = $client->selectCollection('performance', 'tenthousand');

$documents = $collection->find();
foreach ($documents as $doc)
{
}
$client = new \MongoDB\Client();
$collection = $client->selectCollection('performance', 'tenthousand');

$documents = $collection->find();
foreach ($documents as $doc)
{
}

The baseline results are not promising to begin with, as you've mentioned yourself:

$ time php test-mongo.php

real    0m0.364s
user    0m0.322s
sys 0m0.016s

$ time php test-mongodb.php

real    0m0.087s
user    0m0.052s
sys 0m0.014s

So I profiled the script and as expected, most time is spent doing time conversion:
image

The code to fault here is exactly the kind of code required to get old libraries compatible with the new driver: conversion from the new BSON types to the old legacy types. The problem isn't that it's slow, it just adds up. For example, a single call to MongoId::__construct takes all of 27 microseconds. When it has to be done 10000 times that still adds up to 270ms, or in the case of this run, about 10% of the time spent.
Similarly, every time a BSONDocument or BSONArray instance is spotted, TypeConverter::toLegacy has to iterate over it.

That being said, now that I know what the issues are, I'll try to fix them. Maybe reducing the amount of nested function calls would help make it faster, maybe I can optimize some of the type handling to be generally faster. However, I don't expect to make it significantly faster - that is the penalty associated with that kind of built-on-top conversion.

from mongo-php-adapter.

alcaeus avatar alcaeus commented on June 18, 2024 2

Are there any possible tweaks to get the execution time down to be in the same time range as the mongo-php-library?

I haven't really done any performance testing yet. I suspect the problem being in the type conversion. I'll see if I can create a few performance test cases and start profiling the code to see where it could be optimized.

from mongo-php-adapter.

alcaeus avatar alcaeus commented on June 18, 2024

Closing for now - I'll revisit this once BSON type conversion can be done natively in the PHP driver. It has been implemented in the HHVM driver and should be in the next minor release of ext-mongodb for PHP. Thank you for the patience.

from mongo-php-adapter.

habashyjr avatar habashyjr commented on June 18, 2024

@alcaeus any workaround to this problem? MongoDB ODM 2.0 doesn't seem to have a precise launch date

from mongo-php-adapter.

alcaeus avatar alcaeus commented on June 18, 2024

No - unfortunately, type conversion in the driver was scrapped, so there's nothing I can really do. I'd rather invest my time in finishing ODM 2.0 - we're close to an alpha but there are a couple of open issues.

from mongo-php-adapter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.