Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
don’t keep query results in memory, write them directly to disk
For queries that fit in memory, the page cache will give us the necessary performance. Queries that don’t fit in memory are enabled by this commit :).
- Loading branch information
1 parent
fdae522
commit d2922fe
Showing
2 changed files
with
183 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,39 @@ | |||
// stringpool provides a pool of string pointers, ensuring that each string is | |||
// stored only once in memory. This is useful for queries that have many | |||
// results, as the amount of source packages is limited. So, as soon as | |||
// len(results) > len(sourcepackages), you save memory using a stringpool. | |||
package stringpool | |||
|
|||
import ( | |||
"sync" | |||
) | |||
|
|||
type StringPool struct { | |||
sync.RWMutex | |||
strings map[string]*string | |||
} | |||
|
|||
func NewStringPool() *StringPool { | |||
return &StringPool{ | |||
strings: make(map[string]*string)} | |||
} | |||
|
|||
func (pool *StringPool) Get(s string) *string { | |||
// Check if the entry is already in the pool with a slightly cheaper | |||
// (read-only) mutex. | |||
pool.RLock() | |||
stored, ok := pool.strings[s] | |||
pool.RUnlock() | |||
if ok { | |||
return stored | |||
} | |||
|
|||
pool.Lock() | |||
defer pool.Unlock() | |||
stored, ok = pool.strings[s] | |||
if ok { | |||
return stored | |||
} | |||
pool.strings[s] = &s | |||
return &s | |||
} |