Issue #1262 Avoid reflection in isMappedBuffer

This commit is contained in:
Greg Wilkins 2017-01-17 15:23:26 +11:00
parent 3b49239407
commit 76e9849fcd
2 changed files with 33 additions and 37 deletions

View File

@ -354,8 +354,9 @@ public class ResourceCache implements HttpContent.Factory
} }
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
protected ByteBuffer getDirectBuffer(Resource resource) protected ByteBuffer getMappedBuffer(Resource resource)
{ {
// Only use file mapped buffers for cached resources, otherwise too much virtual memory commitment for // Only use file mapped buffers for cached resources, otherwise too much virtual memory commitment for
// a non shared resource. Also ignore max buffer size // a non shared resource. Also ignore max buffer size
@ -363,14 +364,26 @@ public class ResourceCache implements HttpContent.Factory
{ {
if (_useFileMappedBuffer && resource.getFile()!=null && resource.length()<Integer.MAX_VALUE) if (_useFileMappedBuffer && resource.getFile()!=null && resource.length()<Integer.MAX_VALUE)
return BufferUtil.toMappedBuffer(resource.getFile()); return BufferUtil.toMappedBuffer(resource.getFile());
}
catch(IOException|IllegalArgumentException e)
{
LOG.warn(e);
}
return null;
}
/* ------------------------------------------------------------ */
protected ByteBuffer getDirectBuffer(Resource resource)
{
try
{
return BufferUtil.toBuffer(resource,true); return BufferUtil.toBuffer(resource,true);
} }
catch(IOException|IllegalArgumentException e) catch(IOException|IllegalArgumentException e)
{ {
LOG.warn(e); LOG.warn(e);
return null;
} }
return null;
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@ -595,15 +608,15 @@ public class ResourceCache implements HttpContent.Factory
ByteBuffer buffer = _directBuffer.get(); ByteBuffer buffer = _directBuffer.get();
if (buffer==null) if (buffer==null)
{ {
ByteBuffer buffer2=ResourceCache.this.getDirectBuffer(_resource); ByteBuffer mapped = ResourceCache.this.getMappedBuffer(_resource);
ByteBuffer direct = mapped==null?ResourceCache.this.getDirectBuffer(_resource):mapped;
if (buffer2==null) if (direct==null)
LOG.warn("Could not load "+this); LOG.warn("Could not load "+this);
else if (_directBuffer.compareAndSet(null,buffer2)) else if (_directBuffer.compareAndSet(null,direct))
{ {
buffer=buffer2; buffer=direct;
if (mapped==null && _cachedSize.addAndGet(BufferUtil.length(buffer))>_maxCacheSize)
if (!BufferUtil.isMappedBuffer(buffer) && _cachedSize.addAndGet(BufferUtil.length(buffer))>_maxCacheSize)
shrinkCache(); shrinkCache();
} }
else else

View File

@ -913,39 +913,22 @@ public class BufferUtil
} }
} }
static final Field fdMappedByteBuffer;
static
{
Field fd = null;
try
{
fd=MappedByteBuffer.class.getDeclaredField("fd");
fd.setAccessible(true);
}
catch(Exception e)
{
}
fdMappedByteBuffer=fd;
}
public static boolean isMappedBuffer(ByteBuffer buffer) public static boolean isMappedBuffer(ByteBuffer buffer)
{ {
if (!(buffer instanceof MappedByteBuffer)) if (!(buffer instanceof MappedByteBuffer))
return false; return false;
MappedByteBuffer mapped = (MappedByteBuffer) buffer; MappedByteBuffer mapped = (MappedByteBuffer) buffer;
if (fdMappedByteBuffer!=null) try
{ {
try // Check if it really is a mapped buffer
{ mapped.isLoaded();
if (fdMappedByteBuffer.get(mapped) instanceof FileDescriptor) return true;
return true; }
} catch(UnsupportedOperationException e)
catch(Exception e) {
{ return false;
}
} }
return false;
} }