Skip to content

Commit

Permalink
catalog: optimize adding all entries from a catalog
Browse files Browse the repository at this point in the history
Previously, when copying from different catalog objects incurred
unnecessary overhead by copying values. To address this, this patch will
move references over between them.

Before this and the previous patch applied:
BenchmarkORMQueries/asyncpg_type  13252306945 ns/op 5442736128 B/op 29586155 allocs/op
After this patch:
BenchmarkORMQueries/asyncpg_types 12399752839 ns/op 4602288096 B/op 27474078 allocs/op

Informs: #124750

Release note: None
  • Loading branch information
fqazi committed May 28, 2024
1 parent 83f12b1 commit e38c88d
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions pkg/sql/catalog/nstree/catalog_mutable.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,26 @@ func (mc *MutableCatalog) ensureForID(id descpb.ID) *byIDEntry {
newEntry := &byIDEntry{
id: id,
}
if replaced := mc.byID.upsert(newEntry); replaced != nil {
*newEntry = *(replaced.(*byIDEntry))
if replaced := mc.ensureForIDWithEntry(newEntry); replaced != nil {
*newEntry = *replaced
} else {
mc.byteSize += newEntry.ByteSize()
}
return newEntry
}

// ensureForWithEntry upserts the entry and returns either the current entry or
// the one that has replaced.
func (mc *MutableCatalog) ensureForIDWithEntry(newEntry *byIDEntry) *byIDEntry {
mc.maybeInitialize()
if replaced := mc.byID.upsert(newEntry); replaced != nil {
return replaced.(*byIDEntry)
} else {
mc.byteSize += newEntry.ByteSize()
}
return nil
}

func (mc *MutableCatalog) maybeGetByID(id descpb.ID) *byIDEntry {
if !mc.IsInitialized() {
return nil
Expand All @@ -73,12 +85,22 @@ func (mc *MutableCatalog) ensureForName(key catalog.NameKey) *byNameEntry {
parentSchemaID: key.GetParentSchemaID(),
name: key.GetName(),
}
if replaced := mc.ensureForNameWithEntry(newEntry); replaced != nil {
*newEntry = *replaced
}
return newEntry
}

// ensureForNameWithEntry upserts the entry and returns either the current entry or
// the one that has replaced.
func (mc *MutableCatalog) ensureForNameWithEntry(newEntry *byNameEntry) *byNameEntry {
mc.maybeInitialize()
if replaced := mc.byName.upsert(newEntry); replaced != nil {
*newEntry = *(replaced.(*byNameEntry))
return replaced.(*byNameEntry)
} else {
mc.byteSize += newEntry.ByteSize()
}
return newEntry
return nil
}

// DeleteByName removes all by-name mappings from the MutableCatalog.
Expand Down Expand Up @@ -197,17 +219,23 @@ func (mc *MutableCatalog) AddAll(c Catalog) {
return
}
_ = c.byName.ascend(func(entry catalog.NameEntry) error {
e := mc.ensureForName(entry)
mc.byteSize -= e.ByteSize()
*e = *(entry.(*byNameEntry))
mc.byteSize += e.ByteSize()
ne := entry.(*byNameEntry)
e := mc.ensureForNameWithEntry(ne)
if e != nil {
// Update the size since the entry was replaced.
mc.byteSize -= e.ByteSize()
mc.byteSize += ne.ByteSize()
}
return nil
})
_ = c.byID.ascend(func(entry catalog.NameEntry) error {
e := mc.ensureForID(entry.GetID())
mc.byteSize -= e.ByteSize()
*e = *(entry.(*byIDEntry))
mc.byteSize += e.ByteSize()
ne := entry.(*byIDEntry)
e := mc.ensureForIDWithEntry(ne)
if e != nil {
// Update the size since the entry was replaced.
mc.byteSize -= e.ByteSize()
mc.byteSize += ne.ByteSize()
}
return nil
})
}

0 comments on commit e38c88d

Please sign in to comment.