Skip to content

Commit

Permalink
use procs over methods for callbacks
Browse files Browse the repository at this point in the history
better performance
  • Loading branch information
Stephen von Takach committed Nov 19, 2017
1 parent e6c77a0 commit a2a323c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 27 deletions.
25 changes: 9 additions & 16 deletions lib/libuv/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,12 @@ def chown(uid:, gid:, wait: true)
end

def send_file(stream, using: :raw, chunk_size: 4096, wait: true)
@transmit_failure ||= method(:transmit_failure)
@transmit_data ||= method(:transmit_data)
@start_transmit ||= method(:start_transmit)
@next_chunk ||= method(:next_chunk)
@transmit_failure ||= proc { |reason| @sending_file.reject(reason) }
@start_transmit ||= proc { |stats|
@file_stream_total = stats[:st_size]
next_chunk
}
@transmit_data ||= proc { |data| transmit_data(data) }

@sending_file = @reactor.defer
@file_stream = stream
Expand All @@ -162,7 +164,7 @@ def send_file(stream, using: :raw, chunk_size: 4096, wait: true)
stat(wait: false).then @start_transmit, @transmit_failure

promise = @sending_file.promise
promise.finally &method(:clean_up_send)
promise.finally { clean_up_send }
respond wait, promise
end

Expand All @@ -172,11 +174,6 @@ def send_file(stream, using: :raw, chunk_size: 4096, wait: true)

##
# File transmit functions -------------
def start_transmit(stats)
@file_stream_total = stats[:st_size]
next_chunk
end

def transmit_data(data)
@file_chunk_count += 1
if @file_stream_type == :http
Expand All @@ -186,11 +183,11 @@ def transmit_data(data)
resp << CRLF
data = resp
end
@file_stream.write(data, wait: :promise).then @next_chunk, @transmit_failure
@file_stream.write(data, wait: :promise).then(proc { next_chunk }, @transmit_failure)
nil
end

def next_chunk(*args)
def next_chunk
next_size = @file_chunk_size
next_offset = @file_chunk_size * @file_chunk_count

Expand All @@ -211,10 +208,6 @@ def next_chunk(*args)
nil
end

def transmit_failure(reason)
@sending_file.reject(reason)
end

def clean_up_send
@sending_file = nil
@file_stream = nil
Expand Down
4 changes: 2 additions & 2 deletions lib/libuv/pipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def initialize(reactor, ipc, acceptor = nil)
def bind(name, &callback)
return if @closed
@on_accept = callback
@on_listen = method(:accept)
@on_listen = proc { accept }

assert_type(String, name, "name must be a String")
name = windows_path name if FFI::Platform.windows?
Expand Down Expand Up @@ -158,7 +158,7 @@ def getsockname
private


def accept(_)
def accept
pipe = nil
begin
raise RuntimeError, CLOSED_HANDLE_ERROR if @closed
Expand Down
10 changes: 5 additions & 5 deletions lib/libuv/reactor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ def initialize(pointer) # :notnew:

# Create an async call for scheduling work from other threads
@run_queue = Queue.new
@process_queue = @reactor.async &method(:process_queue_cb)
@process_queue = @reactor.async { process_queue_cb }
@process_queue.unref

# Create a next tick timer
@next_tick = @reactor.timer &method(:next_tick_cb)
@next_tick = @reactor.timer { next_tick_cb }
@next_tick.unref

# Create an async call for ending the reactor
@stop_reactor = @reactor.async &method(:stop_cb)
@stop_reactor = @reactor.async { stop_cb }
@stop_reactor.unref

# Libuv can prevent the application shutting down once the main thread has ended
Expand All @@ -82,7 +82,7 @@ def initialize(pointer) # :notnew:
# LibUV ingnores program interrupt by default.
# We provide normal behaviour and allow this to be overriden
@on_signal = []
sig_callback = method(:signal_cb)
sig_callback = proc { signal_cb }
self.signal(:INT, &sig_callback).unref
self.signal(:HUP, &sig_callback).unref
self.signal(:TERM, &sig_callback).unref
Expand All @@ -108,7 +108,7 @@ def stop_cb
::Libuv::Ext.stop(@pointer)
end

def signal_cb(_)
def signal_cb
if @on_signal.empty?
stop_cb
else
Expand Down
8 changes: 4 additions & 4 deletions lib/libuv/tcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def write(data, wait: false)
alias_method :do_shutdown, :shutdown
def shutdown
if @pending_writes && @pending_writes.length > 0
@pending_writes[-1][0].finally method(:do_shutdown)
@pending_writes[-1][0].finally { do_shutdown }
else
do_shutdown
end
Expand Down Expand Up @@ -232,7 +232,7 @@ def bind(ip, port, **tls_options, &blk)
return self if @closed

@on_accept = blk
@on_listen = method(:accept)
@on_listen = proc { accept }

assert_type(String, ip, IP_ARGUMENT_ERROR)
assert_type(Integer, port, PORT_ARGUMENT_ERROR)
Expand All @@ -253,7 +253,7 @@ def open(fd, binding = true)
return self if @closed

if binding
@on_listen = method(:accept)
@on_listen = proc { accept }
@on_accept = Proc.new
elsif block_given?
@callback = Proc.new
Expand Down Expand Up @@ -382,7 +382,7 @@ def on_connect(req, status)
end
end

def accept(_)
def accept
begin
raise RuntimeError, CLOSED_HANDLE_ERROR if @closed
tcp = TCP.new(reactor, handle, **@tls_options)
Expand Down

0 comments on commit a2a323c

Please sign in to comment.