mirror of
https://github.com/anchore/syft.git
synced 2026-11-03 16:06:36 +01:00
feat: simplify+improve resolver implementation / glob search
Signed-off-by: Keith Zantow <keith.zantow@anchore.com>
This commit is contained in:
+35
-32
@@ -59,21 +59,21 @@ func (l *Limiter) InUse() (memory, disk int64) {
|
||||
return l.memory, l.disk
|
||||
}
|
||||
|
||||
// Charge starts accounting for one archive. Releasing the charge gives back everything it took.
|
||||
func (l *Limiter) Charge() *Charge {
|
||||
// charge starts accounting for one archive. Releasing the charge gives back everything it took.
|
||||
func (l *Limiter) charge() *charge {
|
||||
if l == nil {
|
||||
return nil
|
||||
}
|
||||
return &Charge{limiter: l}
|
||||
return &charge{limiter: l}
|
||||
}
|
||||
|
||||
// Charge is one archive's draw on a Limiter. A nil *Charge admits everything.
|
||||
// charge is one archive's draw on a Limiter. A nil *charge admits everything.
|
||||
//
|
||||
// Bytes are charged as they land, never from the sizes an archive declares, since those can lie.
|
||||
type Charge struct {
|
||||
limiter *Limiter
|
||||
memory int64
|
||||
disk int64
|
||||
type charge struct {
|
||||
limiter *Limiter
|
||||
inMemory int64
|
||||
onDisk int64
|
||||
}
|
||||
|
||||
func admits(limit, held, n int64) bool {
|
||||
@@ -87,9 +87,9 @@ func admits(limit, held, n int64) bool {
|
||||
}
|
||||
}
|
||||
|
||||
// Memory charges n bytes held in memory, or reports false (charging nothing) when the memory limit
|
||||
// memory charges n bytes held in memory, or reports false (charging nothing) when the memory limit
|
||||
// does not admit them.
|
||||
func (c *Charge) Memory(n int64) bool {
|
||||
func (c *charge) memory(n int64) bool {
|
||||
if c == nil || n <= 0 {
|
||||
return true
|
||||
}
|
||||
@@ -100,14 +100,14 @@ func (c *Charge) Memory(n int64) bool {
|
||||
return false
|
||||
}
|
||||
l.memory += n
|
||||
c.memory += n
|
||||
c.inMemory += n
|
||||
l.peakMemory = max(l.peakMemory, l.memory)
|
||||
return true
|
||||
}
|
||||
|
||||
// Disk charges n bytes written to disk, or reports false (charging nothing) when the disk limit does
|
||||
// disk charges n bytes written to disk, or reports false (charging nothing) when the disk limit does
|
||||
// not admit them.
|
||||
func (c *Charge) Disk(n int64) bool {
|
||||
func (c *charge) disk(n int64) bool {
|
||||
if c == nil || n <= 0 {
|
||||
return true
|
||||
}
|
||||
@@ -118,53 +118,56 @@ func (c *Charge) Disk(n int64) bool {
|
||||
return false
|
||||
}
|
||||
l.disk += n
|
||||
c.disk += n
|
||||
c.onDisk += n
|
||||
l.peakDisk = max(l.peakDisk, l.disk)
|
||||
return true
|
||||
}
|
||||
|
||||
// Index charges n bytes of index bookkeeping. The index always lives in memory, but a zero memory
|
||||
// limit is a valid configuration that must still index archives, so it falls back to the disk
|
||||
// budget when memory does not admit it. Reports false only when neither does.
|
||||
func (c *Charge) Index(n int64) bool {
|
||||
return c.Memory(n) || c.Disk(n)
|
||||
// index charges n bytes of index bookkeeping, which lives in memory whatever the limits say. A zero
|
||||
// memory limit is a valid configuration that must still index archives, so only then does the cost
|
||||
// fall back to the disk budget; a bounded memory limit bounds the index too.
|
||||
func (c *charge) index(n int64) bool {
|
||||
if c.memory(n) {
|
||||
return true
|
||||
}
|
||||
return c != nil && c.limiter.limits.MaxMemoryBytes == 0 && c.disk(n)
|
||||
}
|
||||
|
||||
// RefundMemory gives back n bytes no longer held in memory.
|
||||
func (c *Charge) RefundMemory(n int64) {
|
||||
// refundMemory gives back n bytes no longer held in memory.
|
||||
func (c *charge) refundMemory(n int64) {
|
||||
if c == nil || n <= 0 {
|
||||
return
|
||||
}
|
||||
l := c.limiter
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
n = min(n, c.memory)
|
||||
n = min(n, c.inMemory)
|
||||
l.memory -= n
|
||||
c.memory -= n
|
||||
c.inMemory -= n
|
||||
}
|
||||
|
||||
// RefundDisk gives back n bytes no longer on disk.
|
||||
func (c *Charge) RefundDisk(n int64) {
|
||||
// refundDisk gives back n bytes no longer on disk.
|
||||
func (c *charge) refundDisk(n int64) {
|
||||
if c == nil || n <= 0 {
|
||||
return
|
||||
}
|
||||
l := c.limiter
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
n = min(n, c.disk)
|
||||
n = min(n, c.onDisk)
|
||||
l.disk -= n
|
||||
c.disk -= n
|
||||
c.onDisk -= n
|
||||
}
|
||||
|
||||
// Release gives back everything this charge took. Safe to call more than once.
|
||||
func (c *Charge) Release() {
|
||||
// release gives back everything this charge took. Safe to call more than once.
|
||||
func (c *charge) release() {
|
||||
if c == nil {
|
||||
return
|
||||
}
|
||||
l := c.limiter
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
l.memory -= c.memory
|
||||
l.disk -= c.disk
|
||||
c.memory, c.disk = 0, 0
|
||||
l.memory -= c.inMemory
|
||||
l.disk -= c.onDisk
|
||||
c.inMemory, c.onDisk = 0, 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user