Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 641159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:01:57+00:00 2026-05-13T21:01:57+00:00

I have a complex network of objects being spawned from a sqlite database using

  • 0

I have a complex network of objects being spawned from a sqlite database using sqlalchemy ORM mappings. I have quite a few deeply nested:

for parent in owner.collection: 
    for child in parent.collection: 
        for foo in child.collection: 
            do lots of calcs with foo.property 

My profiling is showing me that the sqlalchemy instrumentation is taking a lot of time in this use case.

The thing is: I don’t ever change the object model (mapped properties) at runtime, so once they are loaded I don’t NEED the instrumentation, or indeed any sqlalchemy overhead at all. After much research, I’m thinking I might have to clone a ‘pure python’ set of objects from my already loaded ‘instrumented objects’, but that would be a pain.

Performance is really crucial here (it’s a simulator), so maybe writing those layers as C extensions using sqlite api directly would be best. Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:01:57+00:00Added an answer on May 13, 2026 at 9:01 pm

    If you reference a single attribute of a single instance lots of times, a simple trick is to store it in a local variable.

    If you want a way to create cheap pure python clones, share the dict object with the original object:

    class CheapClone(object):
        def __init__(self, original):
            self.__dict__ = original.__dict__
    

    Creating a copy like this costs about half of the instrumented attribute access and attribute lookups are as fast as normal.

    There might also be a way to have the mapper create instances of an uninstrumented class instead of the instrumented one. If I have some time, I might take a look how deeply ingrained is the assumption that populated instances are of the same type as the instrumented class.


    Found a quick and dirty way that seems to at least somewhat work on 0.5.8 and 0.6. Didn’t test it with inheritance or other features that might interact badly. Also, this touches some non-public API’s, so beware of breakage when changing versions.

    from sqlalchemy.orm.attributes import ClassManager, instrumentation_registry
    
    class ReadonlyClassManager(ClassManager):
        """Enables configuring a mapper to return instances of uninstrumented 
        classes instead. To use add a readonly_type attribute referencing the
        desired class to use instead of the instrumented one."""
        def __init__(self, class_):
            ClassManager.__init__(self, class_)
            self.readonly_version = getattr(class_, 'readonly_type', None)
            if self.readonly_version:
                # default instantiation logic doesn't know to install finders
                # for our alternate class
                instrumentation_registry._dict_finders[self.readonly_version] = self.dict_getter()
                instrumentation_registry._state_finders[self.readonly_version] = self.state_getter()
    
        def new_instance(self, state=None):
            if self.readonly_version:
                instance = self.readonly_version.__new__(self.readonly_version)
                self.setup_instance(instance, state)
                return instance
            return ClassManager.new_instance(self, state)
    
    Base = declarative_base()
    Base.__sa_instrumentation_manager__ = ReadonlyClassManager
    

    Usage example:

    class ReadonlyFoo(object):
        pass
    
    class Foo(Base, ReadonlyFoo):
        __tablename__ = 'foo'
        id = Column(Integer, primary_key=True)
        name = Column(String(32))
    
        readonly_type = ReadonlyFoo
    
    assert type(session.query(Foo).first()) is ReadonlyFoo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a complex network of objects connected with QSharedPointers and QWeakPointers. Is there
I have a smallish, complex database (a few millions of records split over very
I am using WCF and REST, and I have complex types, which are working
We have a complex app that serves AJAX JSON streams (using ADO to grab
I have a complex MySQL problem. SELECT * FROM banners, content_id_country, languages WHERE content_id_country.content_id
I'm working on a complex network software and I have trouble determining how to
I have written complex library for managing network communication based on iocp mechanism. Problem
I have to send through the network a complex object which references are almost
I need to send a number of complex objects over the network to a
I have complex GUI application written in Python and wxPython. I want it to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.