Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize codecs context cacheing #1195

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions packages/driver/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,10 @@ export class Options {
/** @internal */
readonly annotations: ReadonlyMap<string, string> = new Map<string, string>();

private cachedCodecContext: CodecContext | null = null;
private cachedCodecContextVer = -1;
/** @internal */
cachedCodecContext: CodecContext | null = null;
/** @internal */
cachedCodecContextVer = -1;

get tag(): string | null {
return this.annotations.get(TAG_ANNOTATION_KEY) ?? null;
Expand Down Expand Up @@ -269,30 +271,36 @@ export class Options {
clone.moduleAliases = this.moduleAliases;
}

let originalCodecsMap = false;
if (mergeOptions.codecs != null) {
clone.codecs = new Map([
...this.codecs,
...Object.entries(mergeOptions.codecs),
]) as ReadonlyCodecMap;
} else {
clone.codecs = this.codecs;
originalCodecsMap = true;
// It makes sense to just inherit the cached codec context
// as it's expensive to build. The only case when it can't be
// cached is when .withCodecs() was called, but caching will
// work great for all other .withSomething() methods we have.
clone.cachedCodecContext = this.cachedCodecContext;
clone.cachedCodecContextVer = this.cachedCodecContextVer;
Comment on lines +285 to +286
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clone.cachedCodecContext = this.makeCodecContext();

If we want maximum caching™ we should make sure the cachedCodecContext is actually initalised before we copy it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a check when the cache is used, I think that check is enough.

}

if (mergeOptions._dropSQLRowCodec && clone.codecs.has("_private_sql_row")) {
// This is an optimization -- if "sql_row" is the only codec defined
// and it's set to "object mode", the we want the codec mapping to be
// empty instead. Why? Empty codec mapping short circuits a lot of
// custom codec code, and object is the default behavior anyway.
if (originalCodecsMap) {
if (clone.codecs === this.codecs) {
// "codecs" map isn't a full-blown read-only mapping, we're just
// treating it as such and protecting from the user to mutate
// directly. This allows for optimizing away pointless copies.
// However, in this case, if we just "inherited" our codecs
// mapping from another options object, we have to clone it
// before deleting any keys or mutating it in any way.
clone.codecs = new Map(clone.codecs);
clone.cachedCodecContext = null;
clone.cachedCodecContextVer = -1;
}
(clone.codecs as MutableCodecMap).delete("_private_sql_row");
}
Expand Down