From be9989cf3d82659cee4814cce10093880b170faf Mon Sep 17 00:00:00 2001 From: sheeple Date: Fri, 15 Feb 2008 14:16:28 -0600 Subject: [PATCH] More efficient io.unix.select --- extra/io/unix/select/select.factor | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/extra/io/unix/select/select.factor b/extra/io/unix/select/select.factor index 06e257a610..9827d4d54f 100755 --- a/extra/io/unix/select/select.factor +++ b/extra/io/unix/select/select.factor @@ -17,14 +17,18 @@ TUPLE: select-mx read-fdset write-fdset ; FD_SETSIZE 8 * over set-select-mx-read-fdset FD_SETSIZE 8 * over set-select-mx-write-fdset ; +: clear-nth ( n seq -- ? ) + [ nth ] 2keep f -rot set-nth ; + : handle-fd ( fd task fdset mx -- ) - roll munge rot nth [ swap handle-io-task ] [ 2drop ] if ; + roll munge rot clear-nth + [ swap handle-io-task ] [ 2drop ] if ; : handle-fdset ( tasks fdset mx -- ) [ handle-fd ] 2curry assoc-each ; : init-fdset ( tasks fdset -- ) - dup clear-bits + ! dup clear-bits [ >r drop t swap munge r> set-nth ] curry assoc-each ; : read-fdset/tasks @@ -33,13 +37,19 @@ TUPLE: select-mx read-fdset write-fdset ; : write-fdset/tasks { mx-writes select-mx-write-fdset } get-slots ; -: init-fdsets ( mx -- read write except ) +: max-fd dup assoc-empty? [ drop 0 ] [ keys supremum ] if ; + +: num-fds ( mx -- n ) + dup mx-reads max-fd swap mx-writes max-fd max 1+ ; + +: init-fdsets ( mx -- nfds read write except ) + [ num-fds ] keep [ read-fdset/tasks tuck init-fdset ] keep write-fdset/tasks tuck init-fdset f ; M: select-mx wait-for-events ( ms mx -- ) - swap >r FD_SETSIZE over init-fdsets r> make-timeval + swap >r dup init-fdsets r> make-timeval select multiplexer-error dup read-fdset/tasks pick handle-fdset dup write-fdset/tasks rot handle-fdset ;