comparison libgaim/ft.c @ 15219:ec96d6d2fa6d

[gaim-migrate @ 18008] Patch from Graham Booker to slowly scale up the size of our read or write buffer when transfer files over fast networks. This should hopefully reduce CPU load a tiny bit when transfer large files over fast connections. committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Fri, 15 Dec 2006 08:39:53 +0000
parents 59189ce09f10
children 5176a9f30ba3
comparison
equal deleted inserted replaced
15218:717935e035ca 15219:ec96d6d2fa6d
29 #include "prefs.h" 29 #include "prefs.h"
30 #include "proxy.h" 30 #include "proxy.h"
31 #include "request.h" 31 #include "request.h"
32 #include "util.h" 32 #include "util.h"
33 33
34 #define FT_INITIAL_BUFFER_SIZE 4096
35 #define FT_MAX_BUFFER_SIZE 65535
36
34 static GaimXferUiOps *xfer_ui_ops = NULL; 37 static GaimXferUiOps *xfer_ui_ops = NULL;
35 38
36 static int gaim_xfer_choose_file(GaimXfer *xfer); 39 static int gaim_xfer_choose_file(GaimXfer *xfer);
37 40
38 GaimXfer * 41 GaimXfer *
51 xfer->type = type; 54 xfer->type = type;
52 xfer->account = account; 55 xfer->account = account;
53 xfer->who = g_strdup(who); 56 xfer->who = g_strdup(who);
54 xfer->ui_ops = gaim_xfers_get_ui_ops(); 57 xfer->ui_ops = gaim_xfers_get_ui_ops();
55 xfer->message = NULL; 58 xfer->message = NULL;
59 xfer->current_buffer_size = FT_INITIAL_BUFFER_SIZE;
56 60
57 ui_ops = gaim_xfer_get_ui_ops(xfer); 61 ui_ops = gaim_xfer_get_ui_ops(xfer);
58 62
59 if (ui_ops != NULL && ui_ops->new_xfer != NULL) 63 if (ui_ops != NULL && ui_ops->new_xfer != NULL)
60 ui_ops->new_xfer(xfer); 64 ui_ops->new_xfer(xfer);
779 g_return_if_fail(xfer != NULL); 783 g_return_if_fail(xfer != NULL);
780 784
781 xfer->ops.cancel_recv = fnc; 785 xfer->ops.cancel_recv = fnc;
782 } 786 }
783 787
788 static void
789 gaim_xfer_increase_buffer_size(GaimXfer *xfer)
790 {
791 xfer->current_buffer_size = MIN(xfer->current_buffer_size * 1.5,
792 FT_MAX_BUFFER_SIZE);
793 }
794
784 gssize 795 gssize
785 gaim_xfer_read(GaimXfer *xfer, guchar **buffer) 796 gaim_xfer_read(GaimXfer *xfer, guchar **buffer)
786 { 797 {
787 gssize s, r; 798 gssize s, r;
788 799
789 g_return_val_if_fail(xfer != NULL, 0); 800 g_return_val_if_fail(xfer != NULL, 0);
790 g_return_val_if_fail(buffer != NULL, 0); 801 g_return_val_if_fail(buffer != NULL, 0);
791 802
792 if (gaim_xfer_get_size(xfer) == 0) 803 if (gaim_xfer_get_size(xfer) == 0)
793 s = 4096; 804 s = xfer->current_buffer_size;
794 else 805 else
795 s = MIN(gaim_xfer_get_bytes_remaining(xfer), 4096); 806 s = MIN(gaim_xfer_get_bytes_remaining(xfer), xfer->current_buffer_size);
796 807
797 if (xfer->ops.read != NULL) 808 if (xfer->ops.read != NULL)
798 r = (xfer->ops.read)(buffer, xfer); 809 r = (xfer->ops.read)(buffer, xfer);
799 else { 810 else {
800 *buffer = g_malloc0(s); 811 *buffer = g_malloc0(s);
809 gaim_xfer_set_completed(xfer, TRUE); 820 gaim_xfer_set_completed(xfer, TRUE);
810 else if (r == 0) 821 else if (r == 0)
811 r = -1; 822 r = -1;
812 } 823 }
813 824
825 if (r == xfer->current_buffer_size)
826 /*
827 * We managed to read the entire buffer. This means our this
828 * network is fast and our buffer is too small, so make it
829 * bigger.
830 */
831 gaim_xfer_increase_buffer_size(xfer);
832
814 return r; 833 return r;
815 } 834 }
816 835
817 gssize 836 gssize
818 gaim_xfer_write(GaimXfer *xfer, const guchar *buffer, gsize size) 837 gaim_xfer_write(GaimXfer *xfer, const guchar *buffer, gsize size)
855 return; 874 return;
856 } 875 }
857 } 876 }
858 877
859 if (condition & GAIM_INPUT_WRITE) { 878 if (condition & GAIM_INPUT_WRITE) {
860 size_t s = MIN(gaim_xfer_get_bytes_remaining(xfer), 4096); 879 size_t s = MIN(gaim_xfer_get_bytes_remaining(xfer), xfer->current_buffer_size);
861 880
862 /* this is so the prpl can keep the connection open 881 /* this is so the prpl can keep the connection open
863 if it needs to for some odd reason. */ 882 if it needs to for some odd reason. */
864 if (s == 0) { 883 if (s == 0) {
865 if (xfer->watcher) { 884 if (xfer->watcher) {
881 g_free(buffer); 900 g_free(buffer);
882 return; 901 return;
883 } else if (r < s) { 902 } else if (r < s) {
884 /* We have to seek back in the file now. */ 903 /* We have to seek back in the file now. */
885 fseek(xfer->dest_fp, r - s, SEEK_CUR); 904 fseek(xfer->dest_fp, r - s, SEEK_CUR);
905 } else {
906 /*
907 * We managed to write the entire buffer. This means our
908 * network is fast and our buffer is too small, so make it
909 * bigger.
910 */
911 gaim_xfer_increase_buffer_size(xfer);
886 } 912 }
887 } 913 }
888 914
889 if (r > 0) { 915 if (r > 0) {
890 if (gaim_xfer_get_size(xfer) > 0) 916 if (gaim_xfer_get_size(xfer) > 0)