MongoEngine / MongoEngine/mongoengine

Document instantiation performance

Open
#1,137 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement High Priority Performance
Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

This is probably a less common use case. I've got a mongoengine.Document that represents a common data structure in my application, which I store in my database, and it's great for that. But it's also a generally useful class, and in one place, we create it in an inner loop (to make use of its methods) without ever touching the database.

In this case, object instantiation can take up a non-trivial amount of time, especially compared to normal Python objects:

>>> timeit.timeit('Foo()', 'class Foo(object): pass', number=100000)
0.013084888458251953
>>> timeit.timeit('Bar()', 'import mongoengine\nclass Bar(mongoengine.Document): pass', number=100000)
2.403195858001709

Is there any way to make this more efficient? Or perhaps even to signal to mongoengine: "This is never going to be written to disk, so I don't really need all the metaclass stuff"?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files or tests are named. Start by profiling Document instantiation and reviewing the metaclass and initialization paths implicated by the report, using the supplied timeit comparison as a baseline. Done would require an agreed optimization or opt-out mechanism, plus evidence that instantiation is faster without changing persistence behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
database, performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.