What is TRIM?

A TRIM command informs a drive, that an operating system no longer expects some blocks to return meaningful data. It was introduced, because solid-state drives (SSD) require a block to be erased before new data can be written to it. A TRIM command allows a drive to erase blocks in advance. Erasure happens in background, and when new data needs to be written, some blocks are already cleaned.

TRIM in Diskache

There are some scenarios, when handling and issuing TRIM commands can increase cached disk performance.

First, if cache or primary storage is located on an SSD, Diskache will be able to pass TRIM command to it, so that it could start erasing in background. Without that, write performance might be lower than optimal: as soon as the disk is filled up once, every block update will have to be preceded with synchronous erasure.

This might not be as bad as it sounds if SSD contains some other mutable data besides cache or primary storage images. Operating system will issue TRIM commands for that data, which might supply enough pre-erased blocks for Diskache updates.

Second, even for ordinary hard drives, TRIM commands coming from an operating system will allow Diskache to reuse dirty cache blocks without writing them to primary storage first. Normally, when a block is evicted from cache, it has to be copied to the primary storage before new block takes its place. However, if a block is marked as "trimmed", there is no need to copy, as the data in it is considered obsolete by the operating system anyway.