improve |sockGetStringFrom|

8 views
Skip to first unread message

Qian Yun

unread,
Nov 15, 2023, 7:05:29 AM11/15/23
to fricas-devel
|sockGetStringFrom| uses a fixed buffer length of 10000,
which is wasteful if the string is small, and is wrong if
the string length exceeds it.

I think we can improve with things like:

char *
sock_get_string_aux(int purpose, char * buf, int buf_len)
{
if (accept_if_needed(purpose) != -1) {
val = fill_buf(purpose_table[purpose], buf, buf_len);
if (val == -1)
return NULL;
return buf;
}
return NULL;
}

====

(defun |sockGetStringFrom| (purpose)
(sockGetStringAux purpose (|sockGetInt| purpose)))

#+(and :clisp :ffi)
(defun sockGetStringAux (purpose len)
(ffi:with-foreign-object (buf '(ffi:c-array-max ffi:character len))
(sock_get_string_aux purpose buf len)
(ffi:foreign-value buf)))

- Qian

Waldek Hebisch

unread,
Nov 18, 2023, 8:56:02 AM11/18/23
to fricas...@googlegroups.com
On Wed, Nov 15, 2023 at 08:05:25PM +0800, Qian Yun wrote:
> |sockGetStringFrom| uses a fixed buffer length of 10000,
> which is wasteful if the string is small, and is wrong if
> the string length exceeds it.

Yes, this is a known problem. Concerning efficiency,
I do not think it is a significant problem for sizes below
10000. But we it would be good to have version correct for
longer strings.

>
> I think we can improve with things like:
>
> char *
> sock_get_string_aux(int purpose, char * buf, int buf_len)
> {
> if (accept_if_needed(purpose) != -1) {
> val = fill_buf(purpose_table[purpose], buf, buf_len);
> if (val == -1)
> return NULL;
> return buf;
> }
> return NULL;
> }
>
> ====
>
> (defun |sockGetStringFrom| (purpose)
> (sockGetStringAux purpose (|sockGetInt| purpose)))
>
> #+(and :clisp :ffi)
> (defun sockGetStringAux (purpose len)
> (ffi:with-foreign-object (buf '(ffi:c-array-max ffi:character len))
> (sock_get_string_aux purpose buf len)
> (ffi:foreign-value buf)))


I am affraid it is a bit more complicated. Sockets may deliver
data in pieces with pieces having limited relation to our
logical structure. So we need to collect incoming data
waiting up to time when data (in this case string) is complete.
And FriCASsys may get data from multiple sources, we do not
want to block waiting for data from one source because
inbetween there may appear complete data from other source.

--
Waldek Hebisch
Reply all
Reply to author
Forward
0 new messages