Skip to content

Commit bdef74c

Browse files
committed
Stream results progressively [FCOM-8]
This change brings back streaming of results, which I think was broken in #639 . Unsurprisingly, given the complexity/overhead added to the implementation, the fix herein does hurt performance somewhat, but not terribly. I tested with the following queries in `david_runger` and got: 1. **with this change:** `time /home/david/code/fcom/exe/fcom javascript` => 15.1s, 14.6s 2. **with latest release:** `time fcom javascript` => 12.9s, 12.7s I think that this is well worthwhile, in order to be able to start seeing streaming results as soon as they begin to become available (which often allows for Ctrl-C to interrupt the command before it finishes, anyway, resulting in a net time savings, even if it is slower if/when run all the way to completion). Private reference link: https://chat.deepseek.com/a/chat/s/6eb15fc5-626f-48b9-b329-a0fd6933d96a
1 parent b798b80 commit bdef74c

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## Unreleased
2-
[no unreleased changes yet]
2+
- Stream results progressively.
33

44
## v0.14.0 (2024-12-10)
55
- Remove upper bounds on versions for all dependencies.

lib/fcom/querier.rb

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require 'pty'
4+
35
require_relative 'options_helpers.rb'
46

57
# This class executes a system command to retrieve the git history, which is passed through `rg`
@@ -63,18 +65,34 @@ def query
6365

6466
commands.each do |command|
6567
Fcom.logger.debug("Executing command: #{command}")
66-
output = `#{command}`
6768

68-
if output.empty?
69-
previous_command_generated_output = false
70-
else
71-
if previous_command_generated_output
72-
puts("\n\n") # print blank lines for spacing
73-
end
69+
# Add spacing if needed
70+
if previous_command_generated_output
71+
print "\n\n"
72+
end
73+
74+
PTY.spawn(command) do |stdout, _stdin, _pid|
75+
any_bytes_seen_for_command = false
76+
77+
# Read first byte to detect any output
78+
first_byte = stdout.read(1)
7479

75-
previous_command_generated_output = true
80+
any_bytes_seen_for_command = true
7681

77-
puts(output)
82+
if first_byte
83+
previous_command_generated_output = true
84+
85+
print(first_byte)
86+
87+
# Now read the rest line by line
88+
stdout.each_line { puts(it) }
89+
else
90+
previous_command_generated_output = false
91+
end
92+
rescue Errno::EIO
93+
if !any_bytes_seen_for_command
94+
previous_command_generated_output = false
95+
end
7896
end
7997
end
8098
end

0 commit comments

Comments
 (0)