Skip to content

Ineffective use of CodecCache slows down queries #4180

@packet23

Description

@packet23

Profiling point queries using

var entity = ds.find(Entity.class).filter(eq("id", id)).first();

shows that ~19 % of CPU time is spent caching codecs. When the query is repeated on the same datastore instance,
the cache is effectively emptied.

Flame graph of point query execution, highlighting the stack traces which show ineffective caching:

Image

Root cause seems to be an undesired side-effect of re-setting codec registry on every query:

  • Datastore.find(Class<T>) calls DefaultQueryFactory.createQuery(Datastore, Class<T>, FindOptions, Document)
  • which uses the MorphiaQuery(Datastore, Class<T>, FindOptions, Document) constructor to create a query
  • the constructor calls Datastore.getCollection(Class<T>) to get a reference to the collection
  • which in turn calls MongoCollectionImpl.withCodecRegistry(CodecRegistry)

Result of this call chain is that the MongoDB driver creates a wrapped copy of the CodecRegistry with empty cache.

Not resetting the CodecRegistry on Datastore.getCollection(Class<T>) but instead making sure Morphia's CodecRegistry is always registered at database-level when Datastore is created restores codec cache effectiveness.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions