Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Case study: binary stream reading/writing: new stream methods vs support stream args for ustruct functions #10

Open
pfalcon opened this issue Jul 28, 2018 · 7 comments

Comments

@pfalcon
Copy link
Owner

pfalcon commented Jul 28, 2018

This is continuation of micropython/micropython#3382 . That PR proposed adding methods like stream.readbin(fmt) and stream.writebin(fmt, val). There was criticism/suggestion to instead implement everything on the level of ustruct module.

Option 0 (original):

  • stream.readbin(fmt)
  • stream.writebin(fmt, val)

Option 1: Extending existing unpack()/pack_into() functions.

Plan:

  1. Introduce ustruct.unpack1(fmt, buf) function which would unpack a single value and thus avoid extra memory allocation for a tuple as returned by standard .unpack().
  2. For ustruct.unpack(), ustruct.unpack1(), allow "buf" argument to also accept a stream object type, in which case read the data from it.

So far so good, and even nice. However, packing is worse:

  1. A natural existing func to extend in ustruct.pack_into(). But it has signature ustruct.pack_into(fmt, buffer, offset, v1, v2, ...). For the stream case, offset would be always 0, and in general would be cumbersome/confusing (this point was raised many times in RFC py/stream: Add .readbin() and .writebin() methods. micropython/micropython#3382). Some way to deal with that would be to allow None as offset. (We reject variants like "if stream is passed as buffer argument, then there's no offset argument" - these raise confusion bar too high.)
  2. Even with None as offset it's still to cumbersome. A solution might be implementing a variant of pack_into() method which wouldn't take offset param. But how to name it? I initially named it pack_s() (from "pack stream") but that's hardly beautiful. pack_into0() us hardly more clear.

Option 2: Have readbin/writebin, but as functions in the ustruct module

As another alternative suggested in micropython/micropython#3382. This would be a kind of compromise, not as convenient as methods, but avoid dealing with baggage like pack_into() offset param above. But the next question is in what order should e.g. ustruct.writebin() take args? More specifically, how natural is to have pack_into's order like ustruct.pack_into("B", stream, None, 100). This would lead to ustruct.writebin("B", stream, 100)

But (surprise) maybe it's not too natural, maybe stream arg should be first? ustruct.writebin(stream, "B", 100) , ustruct.readbin(stream, "B"). But done like that, it would mean that these new functions would round pegs in ustruct's square hole.

Overall, going thru description of options 1, 2 shows that the only reasoning behind them would be the desire to stuff the new functionality into straitjacket of CPython's struct module. But the whole idea behind readbin/writebin is different/opposite - not to make questionable (bad?) compromises with the old API, but add an API which many other languages have, to avoid the situation that Python (don't mix up with CPython) falls short to them.

@pfalcon
Copy link
Owner Author

pfalcon commented Jul 29, 2018

Example code:

Option 0:

def write_fqdn(buf, name):
    parts = name.split(".")
    for p in parts:
        buf.writebin("B", len(p))
        buf.write(p)
    buf.writebin("B", 0)

Option 1, read_into():

def write_fqdn(buf, name):
    parts = name.split(".")
    for p in parts:
        ustruct.pack_into("B", buf, None, len(p))
        buf.write(p)
    ustruct.pack_into("B", buf, None, 0)

Option 1, read_s():

def write_fqdn(buf, name):
    parts = name.split(".")
    for p in parts:
        ustruct.pack_s("B", buf, len(p))
        buf.write(p)
    ustruct.pack_s("B", buf, 0)

Option 2, assuming natural arg order (not compatible with the other funcs in struct):

def write_fqdn(buf, name):
    parts = name.split(".")
    for p in parts:
        ustruct.writebin(buf, "B", len(p))
        buf.write(p)
    ustruct.writebin(buf, "B", 0)

Only option 0 shows the continuity between .write() and .writebin() methods:

        buf.writebin("B", len(p))
        buf.write(p)

Again, that was the whole idea of introducing them as methods. In other cases we have:

        ustruct.writebin(buf, "B", len(p))
        buf.write(p)

which is not pretty on eyes.

pfalcon added a commit that referenced this issue Jul 29, 2018
Comparing to pack_into(fmt, buf, offset, v1, ...), it has signature of:
pack_s(fmt, stream, v1, ...), in other words accepts stream instead of
buffer object, and omits offset param.

See #10 for the discussion.
pfalcon added a commit that referenced this issue Jul 29, 2018
@pfalcon
Copy link
Owner Author

pfalcon commented Jul 29, 2018

Option 0 and Option 1a/1b were implemented for actual practical comparisons:

@pfalcon
Copy link
Owner Author

pfalcon commented Jul 29, 2018

Code sizes:

Option 0:

--- SIZE-v1.9.4-414-g199838743.master-sizable	2018-07-29 16:39:53.052562001 +0300
+++ SIZE-v1.9.4-415-ga4efc2a85.cmp-readbin-writebin	2018-07-29 16:42:06.682989964 +0300
@@ -1,5 +1,5 @@
    text	   data	    bss	    dec	    hex	filename
- 358231	   4992	   1396	 364619	  5904b	micropython.size-x86
+ 358727	   4992	   1396	 365115	  5923b	micropython.size-x86
 
    text	   data	    bss	    dec	    hex	filename
     488	      0	      0	    488	    1e8	build-size-x86/alloc.o
@@ -40,7 +40,7 @@
       0	      0	      0	      0	      0	build-size-x86/extmod/vfs_reader.o
     142	      0	      0	    142	     8e	build-size-x86/extmod/virtpin.o
      63	      0	      0	     63	     3f	build-size-x86/fatfs_port.o
-   1623	      0	      0	   1623	    657	build-size-x86/file.o
+   1631	      0	      0	   1631	    65f	build-size-x86/file.o
     216	      0	      0	    216	     d8	build-size-x86/gccollect.o
     602	      0	      0	    602	    25a	build-size-x86/input.o
     554	      0	      0	    554	    22a	build-size-x86/lib/berkeley-db-1.xx/btree/bt_close.o
@@ -110,7 +110,7 @@
     700	      4	      0	    704	    2c0	build-size-x86/py/modio.o
    3628	      0	      0	   3628	    e2c	build-size-x86/py/modmath.o
    1099	      0	      0	   1099	    44b	build-size-x86/py/modmicropython.o
-   1742	      0	      0	   1742	    6ce	build-size-x86/py/modstruct.o
+   1750	      0	      0	   1750	    6d6	build-size-x86/py/modstruct.o
     680	      0	      0	    680	    2a8	build-size-x86/py/modsys.o
    1674	      0	      4	   1678	    68e	build-size-x86/py/modthread.o
     504	      0	      0	    504	    1f8	build-size-x86/py/moduerrno.o
@@ -157,7 +157,7 @@
    4338	      0	      0	   4338	   10f2	build-size-x86/py/objset.o
     160	      0	      0	    160	     a0	build-size-x86/py/objsingleton.o
     477	      0	      0	    477	    1dd	build-size-x86/py/objslice.o
-   1456	      0	      0	   1456	    5b0	build-size-x86/py/objstringio.o
+   1472	      0	      0	   1472	    5c0	build-size-x86/py/objstringio.o
   17036	      0	      0	  17036	   428c	build-size-x86/py/objstr.o
    2060	      0	      0	   2060	    80c	build-size-x86/py/objstrunicode.o
    1990	      0	      0	   1990	    7c6	build-size-x86/py/objtuple.o
@@ -169,7 +169,7 @@
    6292	      0	      0	   6292	   1894	build-size-x86/py/parse.o
    1354	      0	      0	   1354	    54a	build-size-x86/py/persistentcode.o
       0	      0	      0	      0	      0	build-size-x86/py/pystack.o
-  10822	      0	      0	  10822	   2a46	build-size-x86/py/qstr.o
+  10853	      0	      0	  10853	   2a65	build-size-x86/py/qstr.o
     742	      0	      0	    742	    2e6	build-size-x86/py/reader.o
    1538	      0	      0	   1538	    602	build-size-x86/py/repl.o
    9460	      0	      0	   9460	   24f4	build-size-x86/py/runtime.o
@@ -180,7 +180,7 @@
    4769	      0	      0	   4769	   12a1	build-size-x86/py/showbc.o
     286	      0	      0	    286	    11e	build-size-x86/py/smallint.o
     350	      0	      0	    350	    15e	build-size-x86/py/stackctrl.o
-   3502	      0	      0	   3502	    dae	build-size-x86/py/stream.o
+   3929	      0	      0	   3929	    f59	build-size-x86/py/stream.o
    1176	      0	      0	   1176	    498	build-size-x86/py/unicode.o
    8475	      0	      0	   8475	   211b	build-size-x86/py/vm.o
    2048	      0	      0	   2048	    800	build-size-x86/py/vstr.o
@@ -188,7 +188,7 @@
     998	      0	    120	   1118	    45e	build-size-x86/unix_mphal.o
 
    text	   data	    bss	    dec	    hex	filename
- 225237	   4640	   1232	 231109	  386c5	micropython.size-arm
+ 225573	   4640	   1232	 231445	  38815	micropython.size-arm
 
    text	   data	    bss	    dec	    hex	filename
     202	      0	      0	    202	     ca	build-size-arm/alloc.o
@@ -229,7 +229,7 @@
       0	      0	      0	      0	      0	build-size-arm/extmod/vfs_reader.o
      44	      0	      0	     44	     2c	build-size-arm/extmod/virtpin.o
       4	      0	      0	      4	      4	build-size-arm/fatfs_port.o
-   1020	      0	      0	   1020	    3fc	build-size-arm/file.o
+   1028	      0	      0	   1028	    404	build-size-arm/file.o
     110	      0	      0	    110	     6e	build-size-arm/gccollect.o
     343	      0	      0	    343	    157	build-size-arm/input.o
     314	      0	      0	    314	    13a	build-size-arm/lib/berkeley-db-1.xx/btree/bt_close.o
@@ -288,7 +288,7 @@
     545	      0	      0	    545	    221	build-size-arm/py/builtinevex.o
       0	      0	      0	      0	      0	build-size-arm/py/builtinhelp.o
    1348	      0	      0	   1348	    544	build-size-arm/py/builtinimport.o
-  12960	      0	      0	  12960	   32a0	build-size-arm/py/compile.o
+  12956	      0	      0	  12956	   329c	build-size-arm/py/compile.o
    3048	      0	      0	   3048	    be8	build-size-arm/py/emitbc.o
     186	      0	      0	    186	     ba	build-size-arm/py/emitcommon.o
     224	      0	      4	    228	     e4	build-size-arm/py/emitglue.o
@@ -333,11 +333,11 @@
     384	      0	      0	    384	    180	build-size-arm/py/objboundmeth.o
     170	      0	      0	    170	     aa	build-size-arm/py/objcell.o
     441	      0	      0	    441	    1b9	build-size-arm/py/objclosure.o
-   1391	      0	      0	   1391	    56f	build-size-arm/py/objcomplex.o
+   1393	      0	      0	   1393	    571	build-size-arm/py/objcomplex.o
     415	      0	      0	    415	    19f	build-size-arm/py/objdeque.o
    2558	      0	      0	   2558	    9fe	build-size-arm/py/objdict.o
     224	      0	      0	    224	     e0	build-size-arm/py/objenumerate.o
-   2698	      0	      0	   2698	    a8a	build-size-arm/py/objexcept.o
+   2702	      0	      0	   2702	    a8e	build-size-arm/py/objexcept.o
     188	      0	      0	    188	     bc	build-size-arm/py/objfilter.o
    1466	      0	      0	   1466	    5ba	build-size-arm/py/objfloat.o
    1510	      0	      0	   1510	    5e6	build-size-arm/py/objfun.o
@@ -355,12 +355,12 @@
     258	      0	      0	    258	    102	build-size-arm/py/objobject.o
      64	      0	      0	     64	     40	build-size-arm/py/objpolyiter.o
     320	      0	      0	    320	    140	build-size-arm/py/objproperty.o
-    677	      0	      0	    677	    2a5	build-size-arm/py/objrange.o
+    679	      0	      0	    679	    2a7	build-size-arm/py/objrange.o
     190	      0	      0	    190	     be	build-size-arm/py/objreversed.o
    2297	      0	      0	   2297	    8f9	build-size-arm/py/objset.o
      91	      0	      0	     91	     5b	build-size-arm/py/objsingleton.o
-    244	      0	      0	    244	     f4	build-size-arm/py/objslice.o
-    934	      0	      0	    934	    3a6	build-size-arm/py/objstringio.o
+    246	      0	      0	    246	     f6	build-size-arm/py/objslice.o
+    950	      0	      0	    950	    3b6	build-size-arm/py/objstringio.o
   10749	      0	      0	  10749	   29fd	build-size-arm/py/objstr.o
    1476	      0	      0	   1476	    5c4	build-size-arm/py/objstrunicode.o
    1124	      0	      0	   1124	    464	build-size-arm/py/objtuple.o
@@ -372,7 +372,7 @@
    4341	      0	      0	   4341	   10f5	build-size-arm/py/parse.o
     765	      0	      0	    765	    2fd	build-size-arm/py/persistentcode.o
       0	      0	      0	      0	      0	build-size-arm/py/pystack.o
-  10034	      0	      0	  10034	   2732	build-size-arm/py/qstr.o
+  10065	      0	      0	  10065	   2751	build-size-arm/py/qstr.o
     300	      0	      0	    300	    12c	build-size-arm/py/reader.o
    1018	      0	      0	   1018	    3fa	build-size-arm/py/repl.o
    5569	      0	      0	   5569	   15c1	build-size-arm/py/runtime.o
@@ -383,7 +383,7 @@
    3834	      0	      0	   3834	    efa	build-size-arm/py/showbc.o
     138	      0	      0	    138	     8a	build-size-arm/py/smallint.o
     124	      0	      0	    124	     7c	build-size-arm/py/stackctrl.o
-   1885	      0	      0	   1885	    75d	build-size-arm/py/stream.o
+   2145	      0	      0	   2145	    861	build-size-arm/py/stream.o
     534	      0	      0	    534	    216	build-size-arm/py/unicode.o
    5919	      0	      0	   5919	   171f	build-size-arm/py/vm.o
     772	      0	      0	    772	    304	build-size-arm/py/vstr.o

x86_32: bin +496, stream.o +427
arm thumb2: bin +336, stream.o +260

@pfalcon
Copy link
Owner Author

pfalcon commented Jul 29, 2018

Option 1a (stream support for pack_into()):

--- SIZE-v1.9.4-414-g199838743.master-sizable	2018-07-29 16:39:53.052562001 +0300
+++ SIZE-v1.9.4-418-g5d14b019f.cmp-ustruct-stream	2018-07-29 16:49:52.863499204 +0300
@@ -1,5 +1,5 @@
    text	   data	    bss	    dec	    hex	filename
- 358231	   4992	   1396	 364619	  5904b	micropython.size-x86
+ 358651	   4992	   1396	 365039	  591ef	micropython.size-x86
 
    text	   data	    bss	    dec	    hex	filename
     488	      0	      0	    488	    1e8	build-size-x86/alloc.o
@@ -110,7 +110,7 @@
     700	      4	      0	    704	    2c0	build-size-x86/py/modio.o
    3628	      0	      0	   3628	    e2c	build-size-x86/py/modmath.o
    1099	      0	      0	   1099	    44b	build-size-x86/py/modmicropython.o
-   1742	      0	      0	   1742	    6ce	build-size-x86/py/modstruct.o
+   2335	      0	      0	   2335	    91f	build-size-x86/py/modstruct.o
     680	      0	      0	    680	    2a8	build-size-x86/py/modsys.o
    1674	      0	      4	   1678	    68e	build-size-x86/py/modthread.o
     504	      0	      0	    504	    1f8	build-size-x86/py/moduerrno.o
@@ -169,7 +169,7 @@
    6292	      0	      0	   6292	   1894	build-size-x86/py/parse.o
    1354	      0	      0	   1354	    54a	build-size-x86/py/persistentcode.o
       0	      0	      0	      0	      0	build-size-x86/py/pystack.o
-  10822	      0	      0	  10822	   2a46	build-size-x86/py/qstr.o
+  10837	      0	      0	  10837	   2a55	build-size-x86/py/qstr.o
     742	      0	      0	    742	    2e6	build-size-x86/py/reader.o
    1538	      0	      0	   1538	    602	build-size-x86/py/repl.o
    9460	      0	      0	   9460	   24f4	build-size-x86/py/runtime.o
@@ -188,7 +188,7 @@
     998	      0	    120	   1118	    45e	build-size-x86/unix_mphal.o
 
    text	   data	    bss	    dec	    hex	filename
- 225237	   4640	   1232	 231109	  386c5	micropython.size-arm
+ 225457	   4640	   1232	 231329	  387a1	micropython.size-arm
 
    text	   data	    bss	    dec	    hex	filename
     202	      0	      0	    202	     ca	build-size-arm/alloc.o
@@ -313,7 +313,7 @@
     392	      4	      0	    396	    18c	build-size-arm/py/modio.o
    1815	      0	      0	   1815	    717	build-size-arm/py/modmath.o
     669	      0	      0	    669	    29d	build-size-arm/py/modmicropython.o
-   1057	      0	      0	   1057	    421	build-size-arm/py/modstruct.o
+   1389	      0	      0	   1389	    56d	build-size-arm/py/modstruct.o
     464	      0	      0	    464	    1d0	build-size-arm/py/modsys.o
    1014	      0	      4	   1018	    3fa	build-size-arm/py/modthread.o
     432	      0	      0	    432	    1b0	build-size-arm/py/moduerrno.o
@@ -337,7 +337,7 @@
     415	      0	      0	    415	    19f	build-size-arm/py/objdeque.o
    2558	      0	      0	   2558	    9fe	build-size-arm/py/objdict.o
     224	      0	      0	    224	     e0	build-size-arm/py/objenumerate.o
-   2698	      0	      0	   2698	    a8a	build-size-arm/py/objexcept.o
+   2702	      0	      0	   2702	    a8e	build-size-arm/py/objexcept.o
     188	      0	      0	    188	     bc	build-size-arm/py/objfilter.o
    1466	      0	      0	   1466	    5ba	build-size-arm/py/objfloat.o
    1510	      0	      0	   1510	    5e6	build-size-arm/py/objfun.o
@@ -372,7 +372,7 @@
    4341	      0	      0	   4341	   10f5	build-size-arm/py/parse.o
     765	      0	      0	    765	    2fd	build-size-arm/py/persistentcode.o
       0	      0	      0	      0	      0	build-size-arm/py/pystack.o
-  10034	      0	      0	  10034	   2732	build-size-arm/py/qstr.o
+  10049	      0	      0	  10049	   2741	build-size-arm/py/qstr.o
     300	      0	      0	    300	    12c	build-size-arm/py/reader.o
    1018	      0	      0	   1018	    3fa	build-size-arm/py/repl.o
    5569	      0	      0	   5569	   15c1	build-size-arm/py/runtime.o

x86_32: bin +420
arm thumb2: bin +220

@pfalcon
Copy link
Owner Author

pfalcon commented Jul 29, 2018

Option 1a (separate pack_s() function):

--- SIZE-v1.9.4-414-g199838743.master-sizable	2018-07-29 16:39:53.052562001 +0300
+++ SIZE-v1.9.4-417-ga632d0bb8.HEAD	2018-07-29 17:18:53.227521229 +0300
@@ -1,5 +1,5 @@
    text	   data	    bss	    dec	    hex	filename
- 358231	   4992	   1396	 364619	  5904b	micropython.size-x86
+ 358747	   4992	   1396	 365135	  5924f	micropython.size-x86
 
    text	   data	    bss	    dec	    hex	filename
     488	      0	      0	    488	    1e8	build-size-x86/alloc.o
@@ -110,7 +110,7 @@
     700	      4	      0	    704	    2c0	build-size-x86/py/modio.o
    3628	      0	      0	   3628	    e2c	build-size-x86/py/modmath.o
    1099	      0	      0	   1099	    44b	build-size-x86/py/modmicropython.o
-   1742	      0	      0	   1742	    6ce	build-size-x86/py/modstruct.o
+   2222	      0	      0	   2222	    8ae	build-size-x86/py/modstruct.o
     680	      0	      0	    680	    2a8	build-size-x86/py/modsys.o
    1674	      0	      4	   1678	    68e	build-size-x86/py/modthread.o
     504	      0	      0	    504	    1f8	build-size-x86/py/moduerrno.o
@@ -169,7 +169,7 @@
    6292	      0	      0	   6292	   1894	build-size-x86/py/parse.o
    1354	      0	      0	   1354	    54a	build-size-x86/py/persistentcode.o
       0	      0	      0	      0	      0	build-size-x86/py/pystack.o
-  10822	      0	      0	  10822	   2a46	build-size-x86/py/qstr.o
+  10851	      0	      0	  10851	   2a63	build-size-x86/py/qstr.o
     742	      0	      0	    742	    2e6	build-size-x86/py/reader.o
    1538	      0	      0	   1538	    602	build-size-x86/py/repl.o
    9460	      0	      0	   9460	   24f4	build-size-x86/py/runtime.o
@@ -188,7 +188,7 @@
     998	      0	    120	   1118	    45e	build-size-x86/unix_mphal.o
 
    text	   data	    bss	    dec	    hex	filename
- 225237	   4640	   1232	 231109	  386c5	micropython.size-arm
+ 225549	   4640	   1232	 231421	  387fd	micropython.size-arm
 
    text	   data	    bss	    dec	    hex	filename
     202	      0	      0	    202	     ca	build-size-arm/alloc.o
@@ -288,14 +288,14 @@
     545	      0	      0	    545	    221	build-size-arm/py/builtinevex.o
       0	      0	      0	      0	      0	build-size-arm/py/builtinhelp.o
    1348	      0	      0	   1348	    544	build-size-arm/py/builtinimport.o
-  12960	      0	      0	  12960	   32a0	build-size-arm/py/compile.o
+  12956	      0	      0	  12956	   329c	build-size-arm/py/compile.o
    3048	      0	      0	   3048	    be8	build-size-arm/py/emitbc.o
     186	      0	      0	    186	     ba	build-size-arm/py/emitcommon.o
     224	      0	      4	    228	     e4	build-size-arm/py/emitglue.o
       0	      0	      0	      0	      0	build-size-arm/py/emitinlinethumb.o
       0	      0	      0	      0	      0	build-size-arm/py/emitinlinextensa.o
       0	      0	      0	      0	      0	build-size-arm/py/emitnarm.o
-   9698	      0	      0	   9698	   25e2	build-size-arm/py/emitnthumb.o
+   9706	      0	      0	   9706	   25ea	build-size-arm/py/emitnthumb.o
       0	      0	      0	      0	      0	build-size-arm/py/emitnx64.o
       0	      0	      0	      0	      0	build-size-arm/py/emitnx86.o
       0	      0	      0	      0	      0	build-size-arm/py/emitnxtensa.o
@@ -313,7 +313,7 @@
     392	      4	      0	    396	    18c	build-size-arm/py/modio.o
    1815	      0	      0	   1815	    717	build-size-arm/py/modmath.o
     669	      0	      0	    669	    29d	build-size-arm/py/modmicropython.o
-   1057	      0	      0	   1057	    421	build-size-arm/py/modstruct.o
+   1313	      0	      0	   1313	    521	build-size-arm/py/modstruct.o
     464	      0	      0	    464	    1d0	build-size-arm/py/modsys.o
    1014	      0	      4	   1018	    3fa	build-size-arm/py/modthread.o
     432	      0	      0	    432	    1b0	build-size-arm/py/moduerrno.o
@@ -333,11 +333,11 @@
     384	      0	      0	    384	    180	build-size-arm/py/objboundmeth.o
     170	      0	      0	    170	     aa	build-size-arm/py/objcell.o
     441	      0	      0	    441	    1b9	build-size-arm/py/objclosure.o
-   1391	      0	      0	   1391	    56f	build-size-arm/py/objcomplex.o
+   1393	      0	      0	   1393	    571	build-size-arm/py/objcomplex.o
     415	      0	      0	    415	    19f	build-size-arm/py/objdeque.o
    2558	      0	      0	   2558	    9fe	build-size-arm/py/objdict.o
     224	      0	      0	    224	     e0	build-size-arm/py/objenumerate.o
-   2698	      0	      0	   2698	    a8a	build-size-arm/py/objexcept.o
+   2702	      0	      0	   2702	    a8e	build-size-arm/py/objexcept.o
     188	      0	      0	    188	     bc	build-size-arm/py/objfilter.o
    1466	      0	      0	   1466	    5ba	build-size-arm/py/objfloat.o
    1510	      0	      0	   1510	    5e6	build-size-arm/py/objfun.o
@@ -355,11 +355,11 @@
     258	      0	      0	    258	    102	build-size-arm/py/objobject.o
      64	      0	      0	     64	     40	build-size-arm/py/objpolyiter.o
     320	      0	      0	    320	    140	build-size-arm/py/objproperty.o
-    677	      0	      0	    677	    2a5	build-size-arm/py/objrange.o
+    679	      0	      0	    679	    2a7	build-size-arm/py/objrange.o
     190	      0	      0	    190	     be	build-size-arm/py/objreversed.o
    2297	      0	      0	   2297	    8f9	build-size-arm/py/objset.o
      91	      0	      0	     91	     5b	build-size-arm/py/objsingleton.o
-    244	      0	      0	    244	     f4	build-size-arm/py/objslice.o
+    246	      0	      0	    246	     f6	build-size-arm/py/objslice.o
     934	      0	      0	    934	    3a6	build-size-arm/py/objstringio.o
   10749	      0	      0	  10749	   29fd	build-size-arm/py/objstr.o
    1476	      0	      0	   1476	    5c4	build-size-arm/py/objstrunicode.o
@@ -372,7 +372,7 @@
    4341	      0	      0	   4341	   10f5	build-size-arm/py/parse.o
     765	      0	      0	    765	    2fd	build-size-arm/py/persistentcode.o
       0	      0	      0	      0	      0	build-size-arm/py/pystack.o
-  10034	      0	      0	  10034	   2732	build-size-arm/py/qstr.o
+  10063	      0	      0	  10063	   274f	build-size-arm/py/qstr.o
     300	      0	      0	    300	    12c	build-size-arm/py/reader.o
    1018	      0	      0	   1018	    3fa	build-size-arm/py/repl.o
    5569	      0	      0	   5569	   15c1	build-size-arm/py/runtime.o

x86_32: bin +516
arm thumb2: bin +312

@pfalcon
Copy link
Owner Author

pfalcon commented Jul 29, 2018

So, as can be seen, an option to reuse ustruct's unpack/pack_into() gives a hundred bytes of code savings, but that's the least comfortable to use option. And introduction just the new pack_s() function leads to about same overhead as introducing 2 readbin/writebin methods.

@pfalcon
Copy link
Owner Author

pfalcon commented Jul 29, 2018

Summary: if fitting the new functionality behind the existing API was the only requirement, the unpack/pack_into() way would even give an edge on the code size.

But that never was a requirement. Instead such was an introduction of new, clear and easy to use API. In that regard, excursion into fitting it back into ustruct module confirmed intuition that it would lead to poor compromise, long ago expressed in micropython/micropython#3382 .

pfalcon added a commit that referenced this issue Aug 4, 2018
Comparing to pack_into(fmt, buf, offset, v1, ...), it has signature of:
pack_s(fmt, stream, v1, ...), in other words accepts stream instead of
buffer object, and omits offset param.

See #10 for the discussion.
pfalcon added a commit that referenced this issue Aug 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant