Coder Social home page Coder Social logo

Comments (5)

Fatal1ty avatar Fatal1ty commented on July 19, 2024

Hi @gshank

But the performance was substantially worse (2x slower)

How did you measure performance? You were running your json_schema method for the same dataclass sequentially, right? If this is the case, then you can simply cache the result once, since it will not change. The easiest way with lru_cache:

from functools import lru_cache

def foo():
    print('run foo!')
    return 42

class Bar:
    @classmethod
    @lru_cache(maxsize=1)
    def json_schema(cls):
        return foo()

print(Bar.json_schema())
print(Bar.json_schema())

# run foo!
# 42
# 42

I see hologram uses a class-level cache when you apply JsonSchemaMixin to the class. This is a wise decision but it will make it impossible to use the same dataclass in multiple root dataclasses with overriden serialization.

from mashumaro.

gshank avatar gshank commented on July 19, 2024

This is working. I will compare the performance and see how it does.

from mashumaro.

Fatal1ty avatar Fatal1ty commented on July 19, 2024

@gshank to make lru_cache do its job for unknown number of subclasses I can suggest you set max_size=None explicitly since it's limited to 128 by default. In my original example it was set to 1, so it didn't work for you.

from mashumaro.

Fatal1ty avatar Fatal1ty commented on July 19, 2024

@gshank looks like you’ve sorted out the caching problem? Can we close this issue?

from mashumaro.

Fatal1ty avatar Fatal1ty commented on July 19, 2024

I’m going to close this issue as stale. Feel free to reopen it if needed.

from mashumaro.

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.