[liblouis-liblouisxml] Re: Python bindings and output buffer size for lou_translate*

  • From: James Teh <jamie@xxxxxxxxxxxx>
  • To: liblouis-liblouisxml@xxxxxxxxxxxxx
  • Date: Wed, 04 Aug 2010 12:56:12 +1000

How about this?

Note that I prefer the name outlenMultiplier because it's not technically a ratio unless it's expressed as a quotient. Also, this makes the terminology easier to follow in that we talk about the length of the input, avoiding the need for the Python programmer to understand what's going on internally.

Jamie

On 4/08/2010 11:59 AM, Michael Whapples wrote:
Hello,
While memory isn't a huge concern I just like being as efficient as
possible. Also could memory be a concern for those using liblouis in
embedded devices (although there may be a question of why they would be
using python in such a case).

My personal view would be go for something like 12 times but allow the
ratio to be configured as desired as it adds very little in complexity
(eg. in the python case it is the difference of having the ratio as
module public variable or module level private, module level seems most
sensible to me as the ratio may be needed in more than one function). An
example of what I am meaning is attached (this patch only does the
translate function it would be easy enough to do the other functions).

I don't like the set it to a large value for the output buffer with no
relation to input buffer as what happens in the case someone chucks a
huge amount of input at liblouis? How do you decide what is really big
enough?

As for jlouis, currently sets the ratio to 2 times as that was what
everyone else seemed to do, but I am probably going to go with the large
default ratio but configurable if desired option.

Michael Whapples

--
James Teh
Vice President
NV Access Inc, ABN 61773362390
Email: jamie@xxxxxxxxxxxx
Web site: http://www.nvaccess.org/
Index: python/louis/__init__.py.in
===================================================================
--- python/louis/__init__.py.in (revision 370)
+++ python/louis/__init__.py.in (working copy)
@@ -35,6 +35,15 @@
 import struct
 import atexit
 
+#{ Module Configuration
+#: Specifies the number by which the input length should be multiplied
+#: to calculate the maximum output length.
+#: @type: int
+# This default will handle the case where every input character is
+# undefined in the translation table.
+outlenMultiplier = 4 + sizeof(c_wchar) * 2
+#}
+
 try:
     # Native win32
     _loader = windll
@@ -102,7 +111,7 @@
     tablesString = ",".join([str(x) for x in tran_tables])
     inbuf = unicode(inbuf)
     inlen = c_int(len(inbuf))
-    outlen = c_int(inlen.value*2)
+    outlen = c_int(inlen.value*outlenMultiplier)
     outbuf = create_unicode_buffer(outlen.value)
     typeformbuf = None
     if typeform:
@@ -137,7 +146,7 @@
     tablesString = ",".join([str(x) for x in tran_tables])
     inbuf = unicode(inbuf)
     inlen = c_int(len(inbuf))
-    outlen = c_int(inlen.value*2)
+    outlen = c_int(inlen.value*outlenMultiplier)
     outbuf = create_unicode_buffer(outlen.value)
     typeformbuf = None
     if typeform:
@@ -173,7 +182,7 @@
     tablestring = ','.join([str(x) for x in tran_tables])
     inbuf = unicode(inbuf)
     inlen = c_int(len(inbuf))
-    outlen = c_int(inlen.value * 2)
+    outlen = c_int(inlen.value * outlenMultiplier)
     outbuf = create_unicode_buffer(outlen.value)
     typeformbuf = None
     if isinstance(typeform, list):
@@ -208,7 +217,7 @@
     tablestring = ','.join([str(x) for x in tran_tables])
     inbuf = unicode(inbuf)
     inlen = c_int(len(inbuf))
-    outlen = c_int(inlen.value * 2)
+    outlen = c_int(inlen.value * outlenMultiplier)
     outbuf = create_unicode_buffer(outlen.value)
     typeformbuf = None
     if isinstance(typeform, list):

Other related posts: