Skip to content

Add matmul operator (@) as per PEP 465 (attempt 2) - #4947

Closed
dpgeorge wants to merge 5 commits into
micropython:masterfrom
dpgeorge:py-token-optimise-order
Closed

dpgeorge wants to merge 5 commits into
micropython:masterfrom
dpgeorge:py-token-optimise-order

Conversation

@dpgeorge

Copy link
Copy Markdown
Member

Based on #4740, add support for __matmul__, with further optimisations to reduce code size.

The first 3 commits here apply some space-saving optimisations to the lexer/parser/compiler and achieve a change in code size of:

   bare-arm:  -120 -0.179% 
minimal x86:  -112 -0.073% 
   unix x64:  -184 -0.037% 
unix nanbox:   -96 -0.022% 
      stm32:   -92 -0.025% 
     cc3200:   -88 -0.047% 
    esp8266:  -164 -0.025% 
      esp32:  -232 -0.020% [incl -80(data)]
        nrf:  -112 -0.077% 
       samd:  -104 -0.102% 

Then adding support for matmul changes code size by:

   bare-arm:   +16 +0.024% 
minimal x86:   +48 +0.031% 
   unix x64:  +144 +0.029% 
unix nanbox:  +160 +0.036% 
      stm32:   +40 +0.011% 
     cc3200:   +24 +0.013% 
    esp8266:   +92 +0.014% 
      esp32:   +96 +0.008% [incl +64(data)]
        nrf:   +36 +0.025% 
       samd:   +24 +0.024% 

So that the total change in code size for the whole PR here is:

   bare-arm:  -104 -0.156% 
minimal x86:   -64 -0.041% 
   unix x64:   -40 -0.008% 
unix nanbox:   +64 +0.014% 
      stm32:   -52 -0.014% 
     cc3200:   -64 -0.034% 
    esp8266:   -72 -0.011% 
      esp32:  -136 -0.012% [incl -16(data)]
        nrf:   -76 -0.052% 
       samd:   -80 -0.079% 

@dpgeorge

Copy link
Copy Markdown
Member Author

To make progress towards MicroPython supporting Python 3.5, adding the matmul operator is important because it's a really "low level" part of the language, being a new token and modifications to the grammar.

I don't think it's sensible to make it configurable because 1) it would make the grammar and lexer complicated/messy; 2) no other operators are configurable; 3) it's not a feature that can be "dynamically plugged in" via an import.

And matmul can be useful as a general purpose user-defined operator, it doesn't have to be just for numpy use, as argued in #1329 (comment)

@dpgeorge

Copy link
Copy Markdown
Member Author

Note: coverage decrease here is because now there are less total lines in the code base, with the same number of lines covered by tests, so percentage coverage dropped.

@stinos

stinos commented Jul 25, 2019

Copy link
Copy Markdown
Contributor

I don't think it's sensible to make it configurable because 1) it would make the grammar and lexer complicated/messy; 2) no other operators are configurable; 3) it's not a feature that can be "dynamically plugged in" via an import.

Makes sense. Would be like having /, but // being configurable: messy.

@dpgeorge

Copy link
Copy Markdown
Member Author

Would be like having /, but // being configurable: messy.

Right. Floats are configurable, but you can still use the / operator (that's always there).

@dpgeorge

Copy link
Copy Markdown
Member Author

Merged in 6ce7c05 through fe4e1fe

@dpgeorge dpgeorge closed this Sep 26, 2019
@dpgeorge
dpgeorge deleted the py-token-optimise-order branch September 26, 2019 05:19
tannewt pushed a commit to tannewt/circuitpython that referenced this pull request Jul 9, 2021
Fix bytes() and bytearray() input validation
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

Successfully merging this pull request may close these issues.

2 participants