mirror of
https://github.com/Llewellynvdm/fzf.git
synced 2024-06-03 07:50:49 +00:00
2fe1e28220
I profiled fzf and it turned out that it was spending significant amount of time repeatedly converting character arrays into Unicode codepoints. This commit greatly improves search performance after the initial scan by memoizing the converted results. This commit also addresses the problem of unbounded memory usage of fzf. fzf is a short-lived process that usually processes small input, so it was implemented to cache the intermediate results very aggressively with no notion of cache expiration/eviction. I still think a proper implementation of caching scheme is definitely an overkill. Instead this commit introduces limits to the maximum size (or minimum selectivity) of the intermediate results that can be cached.
91 lines
1.6 KiB
Go
91 lines
1.6 KiB
Go
package util
|
|
|
|
// #include <unistd.h>
|
|
import "C"
|
|
|
|
import (
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
// Max returns the largest integer
|
|
func Max(first int, items ...int) int {
|
|
max := first
|
|
for _, item := range items {
|
|
if item > max {
|
|
max = item
|
|
}
|
|
}
|
|
return max
|
|
}
|
|
|
|
// Max32 returns the smallest 32-bit integer
|
|
func Min32(first int32, second int32) int32 {
|
|
if first <= second {
|
|
return first
|
|
}
|
|
return second
|
|
}
|
|
|
|
// Max32 returns the largest 32-bit integer
|
|
func Max32(first int32, second int32) int32 {
|
|
if first > second {
|
|
return first
|
|
}
|
|
return second
|
|
}
|
|
|
|
// Constrain32 limits the given 32-bit integer with the upper and lower bounds
|
|
func Constrain32(val int32, min int32, max int32) int32 {
|
|
if val < min {
|
|
return min
|
|
}
|
|
if val > max {
|
|
return max
|
|
}
|
|
return val
|
|
}
|
|
|
|
// Constrain limits the given integer with the upper and lower bounds
|
|
func Constrain(val int, min int, max int) int {
|
|
if val < min {
|
|
return min
|
|
}
|
|
if val > max {
|
|
return max
|
|
}
|
|
return val
|
|
}
|
|
|
|
// DurWithin limits the given time.Duration with the upper and lower bounds
|
|
func DurWithin(
|
|
val time.Duration, min time.Duration, max time.Duration) time.Duration {
|
|
if val < min {
|
|
return min
|
|
}
|
|
if val > max {
|
|
return max
|
|
}
|
|
return val
|
|
}
|
|
|
|
func Between(val int, min int, max int) bool {
|
|
return val >= min && val <= max
|
|
}
|
|
|
|
// IsTty returns true is stdin is a terminal
|
|
func IsTty() bool {
|
|
return int(C.isatty(C.int(os.Stdin.Fd()))) != 0
|
|
}
|
|
|
|
func TrimRight(runes *[]rune) []rune {
|
|
var i int
|
|
for i = len(*runes) - 1; i >= 0; i-- {
|
|
char := (*runes)[i]
|
|
if char != ' ' && char != '\t' {
|
|
break
|
|
}
|
|
}
|
|
return (*runes)[0 : i+1]
|
|
}
|