Skip to content

Allocating memory for cache seems very slow #315

@Hakkin

Description

@Hakkin

Hi, I was recently testing for query performance on a large database compared to the driver I currently use (mattn), and I noticed that performance was very bad until the specified cache amount was completely filled.
Here is a graph showing the issue, both drivers have cache_size=-524288 specified in the connection URI and I am using a 4 connection pool size, so the total memory allocation was 2GiB total:

Image

You can see this driver gets stuck at around 500 queries/second for a bit until suddenly jumping to over 20,000/second, while mattn has no such issue. This jump in performance corresponds exactly to when the cache is entirely filled.

While I'm not 100% certain it's allocating the memory that's slow, it makes the most logical sense.

One thing I tried was running this:

func init() {
	sqlite3.RuntimeConfig = wazero.NewRuntimeConfig()
	sqlite3.RuntimeConfig = sqlite3.RuntimeConfig.WithCoreFeatures(api.CoreFeaturesV2)
	sqlite3.RuntimeConfig = sqlite3.RuntimeConfig.WithMemoryLimitPages(65536)
	sqlite3.RuntimeConfig = sqlite3.RuntimeConfig.WithMemoryCapacityFromMax(true)
	sqlite3.Initialize()
}

WithMemoryCapacityFromMax documentation says it eagerly allocates max memory, unless max is not defined. The default is false, which means minimum memory is allocated and any call to grow memory results in re-allocations.. This seems like it should pre-allocate the memory, but the behavior doesn't seem to change at all, the memory size gradually grows during executing rather than just once at the start.

Apologies for not having anything to reproduce this behavior, but the database I'm testing this on is very large, so it's not really viable to share it. I believe this behavior should be reproducible by any program that gradually fills the cache over many queries, as long as the database is larger than the allocated cache size.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions