term: cache recently decoded images
This commit introduces a small, bounded, LRU cache for recently decoded images. This allows the same image ID to be used in the cache that the same image bits are repeatedly sent to the terminal. This is advantageous because it reduces the amount of texture space required by the gui layer.
This commit is contained in:
1 parent
198c40103b
commit
dab2dd5025
3 files changed
+86
-5
No files matched your search
Generated
+61
@@ -441,6 +441,15 @@ version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a"
|
||||
|
||||
[[package]]
|
||||
name = "block-buffer"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "blocking"
|
||||
version = "1.0.1"
|
||||
@@ -803,6 +812,12 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cpuid-bool"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634"
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.2.0"
|
||||
@@ -920,6 +935,15 @@ version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198"
|
||||
|
||||
[[package]]
|
||||
name = "digest"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs"
|
||||
version = "1.0.5"
|
||||
@@ -1340,6 +1364,16 @@ dependencies = [
|
||||
"slab",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "generic-array"
|
||||
version = "0.14.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817"
|
||||
dependencies = [
|
||||
"typenum",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getopts"
|
||||
version = "0.2.21"
|
||||
@@ -2280,6 +2314,12 @@ version = "1.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad"
|
||||
|
||||
[[package]]
|
||||
name = "opaque-debug"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
|
||||
|
||||
[[package]]
|
||||
name = "open"
|
||||
version = "1.4.0"
|
||||
@@ -3174,6 +3214,19 @@ dependencies = [
|
||||
"pkg-config",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sha2"
|
||||
version = "0.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2933378ddfeda7ea26f48c555bdad8bb446bf8a3d17832dc83e380d444cfb8c1"
|
||||
dependencies = [
|
||||
"block-buffer",
|
||||
"cfg-if",
|
||||
"cpuid-bool",
|
||||
"digest",
|
||||
"opaque-debug",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shared_library"
|
||||
version = "0.1.9"
|
||||
@@ -3606,6 +3659,12 @@ version = "0.6.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3e5d7cd7ab3e47dda6e56542f4bbf3824c15234958c6e1bd6aaa347e93499fdc"
|
||||
|
||||
[[package]]
|
||||
name = "typenum"
|
||||
version = "1.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33"
|
||||
|
||||
[[package]]
|
||||
name = "uds_windows"
|
||||
version = "0.1.5"
|
||||
@@ -4204,12 +4263,14 @@ dependencies = [
|
||||
"bitflags 1.2.1",
|
||||
"image",
|
||||
"log",
|
||||
"lru",
|
||||
"num-traits 0.2.12",
|
||||
"ordered-float",
|
||||
"palette",
|
||||
"pretty_assertions",
|
||||
"pretty_env_logger",
|
||||
"serde",
|
||||
"sha2",
|
||||
"termwiz",
|
||||
"unicode-segmentation",
|
||||
"unicode-width",
|
||||
|
||||
+4
-2
@@ -14,16 +14,18 @@ readme = "README.md"
|
||||
use_serde = ["termwiz/use_serde"]
|
||||
|
||||
[dependencies]
|
||||
bitflags = "1.0"
|
||||
anyhow = "1.0"
|
||||
bitflags = "1.0"
|
||||
image = "0.23"
|
||||
log = "0.4"
|
||||
lru = "0.5"
|
||||
num-traits = "0.2"
|
||||
ordered-float = "1.0"
|
||||
palette = "0.5"
|
||||
serde = {version="1.0", features = ["rc"]}
|
||||
sha2 = "0.9"
|
||||
unicode-segmentation = "1.6"
|
||||
unicode-width = "0.1"
|
||||
serde = {version="1.0", features = ["rc"]}
|
||||
url = "2"
|
||||
|
||||
[dev-dependencies]
|
||||
|
||||
@@ -291,6 +291,8 @@ pub struct TerminalState {
|
||||
term_version: String,
|
||||
|
||||
writer: Box<dyn std::io::Write>,
|
||||
|
||||
image_cache: lru::LruCache<[u8; 32], Arc<ImageData>>,
|
||||
}
|
||||
|
||||
fn encode_modifiers(mods: KeyModifiers) -> u8 {
|
||||
@@ -383,6 +385,7 @@ impl TerminalState {
|
||||
term_program: term_program.to_string(),
|
||||
term_version: term_version.to_string(),
|
||||
writer: Box::new(std::io::BufWriter::new(writer)),
|
||||
image_cache: lru::LruCache::new(16),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1326,11 +1329,26 @@ impl TerminalState {
|
||||
return;
|
||||
}
|
||||
|
||||
// FIXME: cache recent images and avoid assigning a new id for repeated data!
|
||||
let image_data = Arc::new(ImageData::with_raw_data(png_image_data));
|
||||
let image_data = self.raw_image_to_image_data(png_image_data);
|
||||
self.assign_image_to_cells(width, height, image_data);
|
||||
}
|
||||
|
||||
/// cache recent images and avoid assigning a new id for repeated data!
|
||||
fn raw_image_to_image_data(&mut self, raw_data: Vec<u8>) -> Arc<ImageData> {
|
||||
use sha2::Digest;
|
||||
let mut hasher = sha2::Sha256::new();
|
||||
hasher.update(&raw_data);
|
||||
let key = hasher.finalize().into();
|
||||
|
||||
if let Some(item) = self.image_cache.get(&key) {
|
||||
Arc::clone(item)
|
||||
} else {
|
||||
let image_data = Arc::new(ImageData::with_raw_data(raw_data));
|
||||
self.image_cache.put(key, Arc::clone(&image_data));
|
||||
image_data
|
||||
}
|
||||
}
|
||||
|
||||
fn assign_image_to_cells(&mut self, width: u32, height: u32, image_data: Arc<ImageData>) {
|
||||
let physical_cols = self.screen().physical_cols;
|
||||
let physical_rows = self.screen().physical_rows;
|
||||
@@ -1436,7 +1454,7 @@ impl TerminalState {
|
||||
(Some(w), Some(h)) => (w, h),
|
||||
};
|
||||
|
||||
let image_data = Arc::new(ImageData::with_raw_data(image.data));
|
||||
let image_data = self.raw_image_to_image_data(image.data);
|
||||
self.assign_image_to_cells(width as u32, height as u32, image_data);
|
||||
|
||||
// FIXME: check cursor positioning in iterm
|
||||
|
||||
Reference in new issue
Block a user