fix window cycling

we were repeatedly free-ing and new-ing input mmap windows ... this made
things like zoom out on large images in nip2 much slower than they
needed to be
This commit is contained in:
John Cupitt 2017-12-10 17:37:07 +00:00
parent e9b7231ac0
commit 3c0a2e4837
5 changed files with 82 additions and 55 deletions

View File

@ -1,3 +1,6 @@
10/12/17 started 8.6.1
- stop window new/free cycling .. faster zoom out on large images in nip2
15/4/17 started 8.6.0
- supports fits images with leading non-image HDUs, thanks benepo
- add vips_image_new_from_image() and vips_image_new_from_image1() ... make a

View File

@ -2,7 +2,7 @@
# also update the version number in the m4 macros below
AC_INIT([vips], [8.6.0], [vipsip@jiscmail.ac.uk])
AC_INIT([vips], [8.6.1], [vipsip@jiscmail.ac.uk])
# required for gobject-introspection
AC_PREREQ(2.62)
@ -18,7 +18,7 @@ AC_CONFIG_MACRO_DIR([m4])
# user-visible library versioning
m4_define([vips_major_version], [8])
m4_define([vips_minor_version], [6])
m4_define([vips_micro_version], [0])
m4_define([vips_micro_version], [1])
m4_define([vips_version],
[vips_major_version.vips_minor_version.vips_micro_version])
@ -38,7 +38,7 @@ VIPS_VERSION_STRING=$VIPS_VERSION-`date`
# binary interface changes not backwards compatible?: reset age to 0
LIBRARY_CURRENT=50
LIBRARY_REVISION=0
LIBRARY_REVISION=1
LIBRARY_AGE=8
# patched into include/vips/version.h

View File

@ -73,7 +73,8 @@ typedef struct {
/* window manager.
*/
VipsWindow *vips_window_ref( struct _VipsImage *im, int top, int height );
VipsWindow *vips_window_ref( VipsWindow *window,
struct _VipsImage *im, int top, int height );
int vips_window_unref( VipsWindow *window );
void vips_window_print( VipsWindow *window );

View File

@ -652,23 +652,21 @@ vips_region_image( VipsRegion *reg, const VipsRect *r )
all.height = image->Ysize;
vips_rect_intersectrect( r, &all, &clipped );
/* Test for empty.
*/
if( vips_rect_isempty( &clipped ) ) {
vips_error( "VipsRegion",
"%s", _( "valid clipped to nothing" ) );
return( -1 );
}
VIPS_FREEF( vips_buffer_unref, reg->buffer );
VIPS_FREEF( vips_window_unref, reg->window );
reg->invalid = FALSE;
if( image->data ) {
/* We have the whole image available ... easy!
*/
VIPS_FREEF( vips_buffer_unref, reg->buffer );
VIPS_FREEF( vips_window_unref, reg->window );
/* We can't just set valid = clipped, since this may be an
/* We can't just set valid = whole image, since this may be an
* incompletely calculated memory buffer. Just set valid to r.
*/
reg->valid = clipped;
@ -677,19 +675,14 @@ vips_region_image( VipsRegion *reg, const VipsRect *r )
reg->type = VIPS_REGION_OTHER_IMAGE;
}
else if( image->dtype == VIPS_IMAGE_OPENIN ) {
VIPS_FREEF( vips_buffer_unref, reg->buffer );
/* No complete image data ... but we can use a rolling window.
*/
if( reg->type != VIPS_REGION_WINDOW ||
!reg->window ||
reg->window->top > clipped.top ||
reg->window->top + reg->window->height <
clipped.top + clipped.height ) {
if( !(reg->window = vips_window_ref( image,
clipped.top, clipped.height )) )
return( -1 );
reg->type = VIPS_REGION_WINDOW;
}
reg->type = VIPS_REGION_WINDOW;
if( !(reg->window = vips_window_ref( reg->window, image,
clipped.top, clipped.height )) )
return( -1 );
/* Note the area the window actually represents.
*/
@ -701,6 +694,9 @@ vips_region_image( VipsRegion *reg, const VipsRect *r )
reg->data = reg->window->data;
}
else {
VIPS_FREEF( vips_buffer_unref, reg->buffer );
VIPS_FREEF( vips_window_unref, reg->window );
vips_error( "VipsRegion", "%s", _( "bad image type" ) );
return( -1 );
}

View File

@ -53,7 +53,6 @@
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#include <assert.h>
#include <vips/vips.h>
#include <vips/internal.h>
@ -96,7 +95,7 @@ vips_window_unmap( VipsWindow *window )
#ifdef DEBUG_TOTAL
g_mutex_lock( vips__global_lock );
total_mmap_usage -= window->length;
assert( total_mmap_usage >= 0 );
g_assert( total_mmap_usage >= 0 );
g_mutex_unlock( vips__global_lock );
#endif /*DEBUG_TOTAL*/
@ -111,13 +110,20 @@ vips_window_unmap( VipsWindow *window )
static int
vips_window_free( VipsWindow *window )
{
assert( window->ref_count == 0 );
VipsImage *im = window->im;
g_assert( window->ref_count == 0 );
#ifdef DEBUG
printf( "** vips_window_free: window top = %d, height = %d (%p)\n",
window->top, window->height, window );
printf( "vips_window_unref: %d windows left\n",
g_slist_length( im->windows ) );
#endif /*DEBUG*/
g_assert( g_slist_find( im->windows, window ) );
im->windows = g_slist_remove( im->windows, window );
if( vips_window_unmap( window ) )
return( -1 );
@ -140,19 +146,11 @@ vips_window_unref( VipsWindow *window )
window->top, window->height, window->ref_count );
#endif /*DEBUG*/
assert( window->ref_count > 0 );
g_assert( window->ref_count > 0 );
window->ref_count -= 1;
if( window->ref_count == 0 ) {
assert( g_slist_find( im->windows, window ) );
im->windows = g_slist_remove( im->windows, window );
#ifdef DEBUG
printf( "vips_window_unref: %d windows left\n",
g_slist_length( im->windows ) );
#endif /*DEBUG*/
if( vips_window_free( window ) ) {
g_mutex_unlock( im->sslock );
return( -1 );
@ -276,22 +274,20 @@ vips_window_new( VipsImage *im, int top, int height )
if( !(window = VIPS_NEW( NULL, VipsWindow )) )
return( NULL );
window->ref_count = 0;
window->ref_count = 1;
window->im = im;
window->top = 0;
window->height = 0;
window->data = NULL;
window->baseaddr = NULL;
window->length = 0;
im->windows = g_slist_prepend( im->windows, window );
if( vips_window_set( window, top, height ) ) {
vips_window_free( window );
return( NULL );
}
im->windows = g_slist_prepend( im->windows, window );
window->ref_count += 1;
#ifdef DEBUG
printf( "** vips_window_new: window top = %d, height = %d (%p)\n",
window->top, window->height, window );
@ -343,34 +339,65 @@ vips_window_find( VipsImage *im, int top, int height )
return( window );
}
/* Return a ref to a window that encloses top/height.
/* Update a ref to a window to make it enclose top/height.
*/
VipsWindow *
vips_window_ref( VipsImage *im, int top, int height )
vips_window_ref( VipsWindow *window, VipsImage *im, int top, int height )
{
VipsWindow *window;
int margin;
/* We have a window and it has the pixels we need.
*/
if( window &&
window->top <= top &&
window->top + window->height >= top + height )
return( window );
g_mutex_lock( im->sslock );
if( !(window = vips_window_find( im, top, height )) ) {
/* No existing window ... make a new one. Ask for a larger
* window than we strictly need. There's no point making tiny
* windows.
*/
int margin = VIPS_MIN( vips__window_margin_pixels,
vips__window_margin_bytes /
VIPS_IMAGE_SIZEOF_LINE( im ) );
top -= margin;
height += margin * 2;
top = VIPS_CLIP( 0, top, im->Ysize - 1 );
height = VIPS_CLIP( 0, height, im->Ysize - top );
if( !(window = vips_window_new( im, top, height )) ) {
/* We have a window and we are the only ref to it ... scroll.
*/
if( window &&
window->ref_count == 1 ) {
if( vips_window_set( window, top, height ) ) {
g_mutex_unlock( im->sslock );
vips_window_unref( window );
return( NULL );
}
g_mutex_unlock( im->sslock );
return( window );
}
/* There's more than one ref to the window. We can just decrement.
* Don't call _unref, since we've inside the lock.
*/
if( window )
window->ref_count -= 1;
/* Is there an existing window we can reuse?
*/
if( (window = vips_window_find( im, top, height )) ) {
g_mutex_unlock( im->sslock );
return( window );
}
/* We have to make a new window. Make it a bit bigger than strictly
* necessary.
*/
margin = VIPS_MIN( vips__window_margin_pixels,
vips__window_margin_bytes / VIPS_IMAGE_SIZEOF_LINE( im ) );
top -= margin;
height += margin * 2;
top = VIPS_CLIP( 0, top, im->Ysize - 1 );
height = VIPS_CLIP( 0, height, im->Ysize - top );
if( !(window = vips_window_new( im, top, height )) ) {
g_mutex_unlock( im->sslock );
return( NULL );
}
g_mutex_unlock( im->sslock );