org.eclipse.jgit.storage.file
Class WindowCache

java.lang.Object
  extended by org.eclipse.jgit.storage.file.WindowCache

public class WindowCache
extends Object

Caches slices of a PackFile in memory for faster read access.

The WindowCache serves as a Java based "buffer cache", loading segments of a PackFile into the JVM heap prior to use. As JGit often wants to do reads of only tiny slices of a file, the WindowCache tries to smooth out these tiny reads into larger block-sized IO operations.

Whenever a cache miss occurs, load(PackFile, long) is invoked by exactly one thread for the given (PackFile,position) key tuple. This is ensured by an array of locks, with the tuple hashed to a lock instance.

During a miss, older entries are evicted from the cache so long as isFull() returns true.

Its too expensive during object access to be 100% accurate with a least recently used (LRU) algorithm. Strictly ordering every read is a lot of overhead that typically doesn't yield a corresponding benefit to the application.

This cache implements a loose LRU policy by randomly picking a window comprised of roughly 10% of the cache, and evicting the oldest accessed entry within that window.

Entities created by the cache are held under SoftReferences, permitting the Java runtime's garbage collector to evict entries when heap memory gets low. Most JREs implement a loose least recently used algorithm for this eviction.

The internal hash table does not expand at runtime, instead it is fixed in size at cache creation time. The internal lock table used to gate load invocations is also fixed in size.

The key tuple is passed through to methods as a pair of parameters rather than as a single Object, thus reducing the transient memory allocations of callers. It is more efficient to avoid the allocation, as we can't be 100% sure that a JIT would be able to stack-allocate a key tuple.

This cache has an implementation rule such that:

Therefore, it is safe to perform resource accounting increments during the load(PackFile, long) or createRef(PackFile, long, ByteWindow) methods, and matching decrements during clear(Ref). Implementors may need to override createRef(PackFile, long, ByteWindow) in order to embed additional accounting information into an implementation specific Ref subclass, as the cached entity may have already been evicted by the JRE's garbage collector.

To maintain higher concurrency workloads, during eviction only one thread performs the eviction work, while other threads can continue to insert new objects in parallel. This means that the cache can be temporarily over limit, especially if the nominated eviction thread is being starved relative to the other threads.


Method Summary
static void reconfigure(int packedGitLimit, int packedGitWindowSize, boolean packedGitMMAP, int deltaBaseCacheLimit)
          Deprecated. Use WindowCacheConfig instead.
static void reconfigure(WindowCacheConfig cfg)
          Modify the configuration of the window cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

reconfigure

public static void reconfigure(int packedGitLimit,
                               int packedGitWindowSize,
                               boolean packedGitMMAP,
                               int deltaBaseCacheLimit)
Deprecated. Use WindowCacheConfig instead.

Modify the configuration of the window cache.

The new configuration is applied immediately. If the new limits are smaller than what what is currently cached, older entries will be purged as soon as possible to allow the cache to meet the new limit.

Parameters:
packedGitLimit - maximum number of bytes to hold within this instance.
packedGitWindowSize - number of bytes per window within the cache.
packedGitMMAP - true to enable use of mmap when creating windows.
deltaBaseCacheLimit - number of bytes to hold in the delta base cache.

reconfigure

public static void reconfigure(WindowCacheConfig cfg)
Modify the configuration of the window cache.

The new configuration is applied immediately. If the new limits are smaller than what what is currently cached, older entries will be purged as soon as possible to allow the cache to meet the new limit.

Parameters:
cfg - the new window cache configuration.
Throws:
IllegalArgumentException - the cache configuration contains one or more invalid settings, usually too low of a limit.


Copyright © 2012. All Rights Reserved.