Skip to content

internal/reflectlite: do not use the reflect package #4774

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

Closed
wants to merge 2 commits into from

Conversation

aykevl
Copy link
Member

@aykevl aykevl commented Mar 4, 2025

This avoids a dependency on the reflect package.

I've tried to deduplicate as much as reasonably possible, while keeping the code clean. Especially some tricky methods for the reflect.Type type have a shared implementation (using //go:linkname). In other cases, I've simply used the same type using a type alias, in the other direction (the reflect package now imports the internal/reflectlite package which should be safe).

TODO: actually let the runtime package use this package. For that, a bit more work is needed. done

@aykevl aykevl requested review from ydnar and dgryski March 4, 2025 10:37
Copy link

github-actions bot commented Mar 4, 2025

Size difference with the dev branch:

Binary size difference
not the same command!
    tinygo build -size short -o ./build/test.hex -target=pico ./examples/tmc5160/main.go
    go: downloading github.com/orsinium-labs/tinymath v1.1.0
not the same command!
    tinygo build -size short -o ./build/test.hex -target=nano-rp2040 -stack-size 8kb ./examples/net/websocket/dial/
    go: downloading golang.org/x/net v0.33.0
not the same command!
    tinygo build -size short -o ./build/test.hex -target=nano-rp2040 -stack-size 8kb ./examples/net/mqttclient/natiu/
    go: downloading github.com/soypat/natiu-mqtt v0.5.1
not the same command!
    tinygo build -size short -o ./build/test.hex -target=wioterminal -stack-size 8kb ./examples/net/mqttclient/paho/
    go: downloading github.com/eclipse/paho.mqtt.golang v1.2.0
 flash                          ram
 before   after   diff          before   after   diff
  31372   31248   -124  -0.40%    4552    4536    -16  -0.35% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/ssd1289/main.go
  36600   36512    -88  -0.24%    3988    3972    -16  -0.40% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/pcf8523/
  16760   16688    -72  -0.43%    4180    4180      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/adafruit4650
  12352   12280    -72  -0.58%    3360    3360      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/pca9685/main.go
  10640   10568    -72  -0.68%    3336    3336      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/seesaw
  13888   13816    -72  -0.52%    3408    3408      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico     ./examples/sgp30
  10916   10844    -72  -0.66%    3348    3348      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/touch/capacitive
  26216   26144    -72  -0.27%   16420   16420      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/waveshare-epd/epd2in66b/main.go
  12296   12224    -72  -0.59%    3312    3312      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/qmi8658c/main.go
  10916   10844    -72  -0.66%    3296    3296      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/pcf8591/
   9124    9052    -72  -0.79%    3284    3284      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/xpt2046/main.go
  11304   11232    -72  -0.64%    4260    4260      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/irremote/main.go
  12188   12116    -72  -0.59%    3332    3332      0   0.00% tinygo build -size short -o ./build/test.hex -target=badger2040 ./examples/uc8151/main.go
  10540   10468    -72  -0.68%    3364    3364      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/scd4x/main.go
   9984    9912    -72  -0.72%    3304    3304      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/mpu6886/main.go
   9416    9344    -72  -0.76%    3296    3296      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/mpu9150/main.go
  11676   11604    -72  -0.62%    3332    3332      0   0.00% tinygo build -size short -o ./build/test.hex -target=macropad-rp2040 ./examples/sh1106/macropad_spi
   8532    8468    -64  -0.75%    3760    3760      0   0.00% tinygo build -size short -o ./build/test.hex -target=macropad-rp2040 ./examples/encoders/quadrature-interrupt
  22300   22240    -60  -0.27%    3556    3540    -16  -0.45% tinygo build -size short -o ./build/test.hex -target=bluepill ./examples/ds1307/time/main.go
  67668   67608    -60  -0.09%    6360    6344    -16  -0.25% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/gps/i2c/main.go
  68220   68160    -60  -0.09%    6504    6488    -16  -0.25% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/gps/uart/main.go
  27016   26964    -52  -0.19%    5620    5604    -16  -0.28% tinygo build -size short -o ./build/test.hex -target=microbit-v2 ./examples/microbitmatrix/main.go
  27152   27108    -44  -0.16%    3640    3624    -16  -0.44% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/microbitmatrix/main.go
  13348   13304    -44  -0.33%    3292    3292      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/tmc5160/main.go
   9604    9604      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/adxl345/main.go
  13716   13716      0   0.00%    6796    6796      0   0.00% tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/amg88xx
   9104    9104      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/apa102/main.go
  11848   11848      0   0.00%    6580    6580      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/apds9960/proximity/main.go
   9920    9920      0   0.00%    4760    4760      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/apa102/itsybitsy-m0/main.go
   8376    8376      0   0.00%    2320    2320      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/at24cx/main.go
   8164    8164      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bh1750/main.go
   7468    7468      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/blinkm/main.go
  27804   27804      0   0.00%    4780    4780      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmp180/main.go
  12248   12248      0   0.00%    4812    4812      0   0.00% tinygo build -size short -o ./build/test.hex -target=trinket-m0 ./examples/bmp388/main.go
   8364    8364      0   0.00%    3352    3352      0   0.00% tinygo build -size short -o ./build/test.hex -target=bluepill ./examples/ds1307/sram/main.go
   4628    4628      0   0.00%    2280    2280      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/easystepper/main.go
   7308    7308      0   0.00%    2284    2284      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/gc9a01/main.go
   8012    8012      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/hcsr04/main.go
   5840    5840      0   0.00%    2280    2280      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hd44780/customchar/main.go
   5792    5792      0   0.00%    2280    2280      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hd44780/text/main.go
  10568   10568      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/hd44780i2c/main.go
  14748   14748      0   0.00%    6580    6580      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/hts221/main.go
  16216   16216      0   0.00%    2364    2364      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hub75/main.go
  10276   10276      0   0.00%    6924    6924      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/basic
  11180   11180      0   0.00%    4884    4884      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/ili9341/basic
  29644   29644      0   0.00%   38084   38084      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/pyportal_boing
  10300   10300      0   0.00%    6916    6916      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/scroll
  11264   11264      0   0.00%    4876    4876      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/ili9341/scroll
  11772   11772      0   0.00%    4780    4780      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/lis3dh/main.go
  14112   14112      0   0.00%    6580    6580      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/lps22hb/main.go
  26500   26500      0   0.00%    2328    2328      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/lsm303agr/main.go
  12484   12484      0   0.00%    4788    4788      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/lsm6ds3/main.go
  10760   10760      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mag3110/main.go
   9940    9940      0   0.00%    4780    4780      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp23017/main.go
  10380   10380      0   0.00%    4788    4788      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp23017-multiple/main.go
  10276   10276      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp3008/main.go
   8276    8276      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mma8653/main.go
   8184    8184      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mpu6050/main.go
   6268    6268      0   0.00%    3292    3292      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/pcd8544/setbuffer/main.go
   5296    5296      0   0.00%    2284    2284      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/pcd8544/setpixel/main.go
   2841    2841      0   0.00%     558     558      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino ./examples/servo
   8212    8212      0   0.00%    6788    6788      0   0.00% tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/shifter/main.go
   6704    6704      0   0.00%    2288    2288      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1306/i2c_128x32/main.go
   6216    6216      0   0.00%    2284    2284      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1306/spi_128x64/main.go
   5912    5912      0   0.00%    2284    2284      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1331/main.go
   6848    6848      0   0.00%    2284    2284      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/st7735/main.go
   6776    6776      0   0.00%    2284    2284      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/st7789/main.go
  17544   17544      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/thermistor/main.go
  10712   10712      0   0.00%    4540    4540      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-bluefruit ./examples/tone
  10208   10208      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/tm1637/main.go
   9648    9648      0   0.00%    6788    6788      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/touch/resistive/fourwire/main.go
  12548   12548      0   0.00%    6984    6984      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/touch/resistive/pyportal_touchpaint/main.go
  15372   15372      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/vl53l1x/main.go
  13852   13852      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/vl6180x/main.go
  24796   24796      0   0.00%   13728   13728      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-nrf52840-sense ./examples/waveshare-epd/epd1in54/main.go
   6520    6520      0   0.00%    2324    2324      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/waveshare-epd/epd2in13/main.go
   6188    6188      0   0.00%    2316    2316      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/waveshare-epd/epd2in13x/main.go
   6440    6440      0   0.00%    2324    2324      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/waveshare-epd/epd4in2/main.go
   6988    6988      0   0.00%    4780    4780      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/ws2812
   5768    5768      0   0.00%    9522    9522      0   0.00% '-xesppie' is not a recognized feature for this target (ignoring feature)
   1581    1581      0   0.00%     598     598      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino   ./examples/ws2812
   1056    1056      0   0.00%     180     180      0   0.00% tinygo build -size short -o ./build/test.hex -target=digispark ./examples/ws2812
  32256   32256      0   0.00%    4780    4780      0   0.00% tinygo build -size short -o ./build/test.hex -target=trinket-m0 ./examples/bme280/main.go
  17104   17104      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/microphone/main.go
  11640   11640      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/buzzer/main.go
  12820   12820      0   0.00%    4780    4780      0   0.00% tinygo build -size short -o ./build/test.hex -target=trinket-m0 ./examples/veml6070/main.go
   6904    6904      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l293x/simple/main.go
   8804    8804      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l293x/speed/main.go
   6872    6872      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l9110x/simple/main.go
   9316    9316      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l9110x/speed/main.go
   7404    7404      0   0.00%    3324    3324      0   0.00% tinygo build -size short -o ./build/test.hex -target=nucleo-f103rb ./examples/shiftregister/main.go
   6996    6996      0   0.00%    2272    2272      0   0.00% '-xesppie' is not a recognized feature for this target (ignoring feature)
  13372   13372      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/lis2mdl/main.go
   9920    9920      0   0.00%    4764    4764      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/max72xx/main.go
   7316    7316      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/clkout/
   8788    8788      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/ina260/main.go
  13228   13228      0   0.00%    4780    4780      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/ina219/main.go
   9392    9392      0   0.00%    5248    5248      0   0.00% tinygo build -size short -o ./build/test.hex -target=nucleo-l432kc ./examples/aht20/main.go
  10416   10416      0   0.00%    6788    6788      0   0.00% tinygo build -size short -o ./build/test.elf -target=wioterminal ./examples/axp192/m5stack-core2-blinky/
  13260   13260      0   0.00%    4936    4936      0   0.00% tinygo build -size short -o ./build/test.hex -target=nucleo-wl55jc ./examples/sx126x/lora_rxtx/
   8700    8700      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=circuitplay-express ./examples/makeybutton/main.go
   9708    9708      0   0.00%    4764    4764      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/ds18b20/main.go
   7948    7948      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/ttp229/main.go
  12500   12500      0   0.00%    4552    4552      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=nicenano ./examples/sharpmem/main.go
  15928   15944     16   0.10%    4876    4876      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/as560x/main.go
 290824  290840     16   0.01%   20872   20864     -8  -0.04% tinygo build -size short -o ./build/test.hex -target=wioterminal -stack-size 8kb ./examples/net/mqttclient/paho/
 298396  298416     20   0.01%   15908   15900     -8  -0.05% tinygo build -size short -o ./build/test.hex -target=pyportal -stack-size 8kb ./examples/net/http-get/
  29900   29940     40   0.13%    3680    3664    -16  -0.43% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/tmc2209/main.go
  66872   66948     76   0.11%    4816    4808     -8  -0.17% tinygo build -size short -o ./build/test.hex -target=pico ./examples/ndir/main_ndir.go
 251160  251240     80   0.03%   12868   12860     -8  -0.06% tinygo build -size short -o ./build/test.hex -target=nano-rp2040 -stack-size 8kb ./examples/net/websocket/dial/
  66272   66356     84   0.13%    4784    4776     -8  -0.17% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/mcp9808/main.go
 154040  154144    104   0.07%    6548    6540     -8  -0.12% tinygo build -size short -o ./build/test.hex -target=nano-rp2040 -stack-size 8kb ./examples/net/mqttclient/natiu/
 117972  118088    116   0.10%    9564    9556     -8  -0.08% tinygo build -size short -o ./build/test.hex -target=elecrow-rp2350 -stack-size 8kb ./examples/net/ntpclient/
  86404   86528    124   0.14%    5148    5140     -8  -0.16% tinygo build -size short -o ./build/test.hex -target=challenger-rp2040 ./examples/net/ntpclient/
  69980   70120    140   0.20%    6980    6972     -8  -0.11% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/flash/console/spi
  62240   62380    140   0.22%    3784    3776     -8  -0.21% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ndir/main_ndir.go
 118156  118296    140   0.12%    9504    9496     -8  -0.08% tinygo build -size short -o ./build/test.hex -target=elecrow-rp2040 -stack-size 8kb ./examples/net/tlsclient/
  64340   64488    148   0.23%    6204    6196     -8  -0.13% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmi160/main.go
  64048   64196    148   0.23%    6228    6220     -8  -0.13% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmp280/main.go
 263492  263640    148   0.06%   46752   46744     -8  -0.02% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/slideshow
  68952   69100    148   0.21%    6204    6196     -8  -0.13% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp2515/main.go
  62744   62892    148   0.24%    5948    5940     -8  -0.13% tinygo build -size short -o ./build/test.hex -target=feather-nrf52840 ./examples/is31fl3731/main.go
  77244   77392    148   0.19%    6344    6336     -8  -0.13% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/dht/main.go
  70936   71084    148   0.21%    6348    6340     -8  -0.13% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/time/
  65308   65456    148   0.23%    6260    6252     -8  -0.13% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/ndir/main_ndir.go
 102872  103020    148   0.14%    9816    9808     -8  -0.08% tinygo build -size short -o ./build/test.hex -target=metro-m4-airlift -stack-size 8kb ./examples/net/socket/
 111232  111380    148   0.13%    7784    7776     -8  -0.10% tinygo build -size short -o ./build/test.hex -target=arduino-mkrwifi1010 -stack-size 8kb ./examples/net/tlsclient/
  61472   61628    156   0.25%    6188    6180     -8  -0.13% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/adt7410/main.go
  69720   69876    156   0.22%    6376    6368     -8  -0.13% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/ds3231/main.go
  66724   66880    156   0.23%    9020    9012     -8  -0.09% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/flash/console/qspi
  57480   57636    156   0.27%    3688    3680     -8  -0.22% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht3x/main.go
  57444   57600    156   0.27%    3688    3680     -8  -0.22% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/shtc3/main.go
  71408   71564    156   0.22%    6352    6344     -8  -0.13% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/alarm/
  71312   71468    156   0.22%    6352    6344     -8  -0.13% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/timer/
  61748   61904    156   0.25%    8236    8228     -8  -0.10% tinygo build -size short -o ./build/test.hex -target=feather-m4 ./examples/i2csoft/adt7410/
  82636   82792    156   0.19%    6592    6584     -8  -0.12% tinygo build -size short -o ./build/test.hex -target=nucleo-wl55jc ./examples/lora/lorawan/atcmd/
  60324   60480    156   0.26%    5968    5960     -8  -0.13% tinygo build -size short -o ./build/test.hex -target=feather-nrf52840 ./examples/max6675/main.go
 119272  119428    156   0.13%    7848    7840     -8  -0.10% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 -stack-size 8kb ./examples/net/tcpclient/
 116280  116436    156   0.13%   13124   13116     -8  -0.06% tinygo build -size short -o ./build/test.hex -target=wioterminal -stack-size 8kb ./examples/net/webclient/
  57448   57612    164   0.29%    3696    3688     -8  -0.22% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht4x/main.go
  73932   74096    164   0.22%   10764   10756     -8  -0.07% tinygo build -size short -o ./build/test.hex -target=feather-m4 ./examples/sdcard/console/
 339048  339220    172   0.05%   16512   16504     -8  -0.05% tinygo build -size short -o ./build/test.hex -target=matrixportal-m4 -stack-size 8kb ./examples/net/webstatic/
 287288  287476    188   0.07%   19564   19556     -8  -0.04% tinygo build -size short -o ./build/test.hex -target=wioterminal -stack-size 8kb ./examples/net/webserver/
  70692   70888    196   0.28%    3656    3648     -8  -0.22% tinygo build -size short -o ./build/test.hex -target=pinetime     ./examples/bma42x/main.go
  75644   75864    220   0.29%    7452    7452      0   0.00% tinygo build -size short -o ./build/test.hex -target=p1am-100 ./examples/p1am/main.go
5713642 5717518   3876  -0.00%  857586  857154   -432  -0.00%
@aykevl aykevl force-pushed the reflectlite-rewrite branch from 42a69de to 92f4087 Compare March 4, 2025 11:34
@aykevl
Copy link
Member Author

aykevl commented Mar 4, 2025

Hitting a weird issue with io/fs, this might indicate a deeper problem. Looking into it.

EDIT: I think I fixed it (needed a fix to OptimizeReflectImplements).

@aykevl aykevl force-pushed the reflectlite-rewrite branch from 92f4087 to 7b3e8b3 Compare March 4, 2025 15:08
@@ -11,6 +12,8 @@ type valueFlags uint8
// contained in an interface{} directly, like whether this value was exported at
// all (it is possible to read unexported fields using reflection, but it is not
// possible to modify them).
//
// These flags are shared with the internal/reflectlite package.
const (
valueFlagIndirect valueFlags = 1 << iota
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these are shared, should these constants = their reflectlite counterparts?

Copy link
Contributor

@ydnar ydnar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM. I look forward to rebasing my iter PR on top of this.

@ydnar
Copy link
Contributor

ydnar commented Mar 9, 2025

@aykevl can you look at #4787? It’s an alternative implementation to this, with the Go 1.24 (iter) additions and updates to package runtime.

@aykevl aykevl force-pushed the reflectlite-rewrite branch 3 times, most recently from d97dfbf to 47486dc Compare March 13, 2025 09:18
@aykevl aykevl marked this pull request as ready for review March 13, 2025 09:24
@aykevl
Copy link
Member Author

aykevl commented Mar 13, 2025

Oops, should have tested this one:

panic: todo: internal/reflectlite.Type.String
FAIL	reflect	2.158s
aykevl added 2 commits March 13, 2025 11:13
This avoids a dependency on the reflect package.

I've tried to deduplicate as much as reasonably possible, while keeping
the code clean. Especially some tricky methods for the reflect.Type type
have a shared implementation (using //go:linkname). In other cases, I've
simply used the same type using a type alias, in the other direction
(the reflect package now imports the internal/reflectlite package
instead of the other way around).
This avoids a circular dependency.
@aykevl aykevl force-pushed the reflectlite-rewrite branch from 47486dc to 8bff065 Compare March 13, 2025 10:14
@aykevl
Copy link
Member Author

aykevl commented Mar 13, 2025

Fixed the above panic.

@deadprogram
Copy link
Member

@ydnar and @dgryski are we cool to merge this PR and continue work on iter and friends using this implementation?

@ydnar
Copy link
Contributor

ydnar commented Mar 14, 2025

Can we evaluate this vs the approach I took?

@deadprogram
Copy link
Member

At least from the binaries that had size increase, here is what I see:

This PR:

 flash                          ram
 before   after   diff          before   after   diff
64340   64488    148   0.23%    6204    6196     -8  -0.13% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmi160/main.go
64048   64196    148   0.23%    6228    6220     -8  -0.13% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmp280/main.go
61472   61628    156   0.25%    6188    6180     -8  -0.13% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/adt7410/main.go
69720   69876    156   0.22%    6376    6368     -8  -0.13% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/ds3231/main.go
70692   70888    196   0.28%    3656    3648     -8  -0.22% tinygo build -size short -o ./build/test.hex -target=pinetime     ./examples/bma42x/main.go

PR #4787

 flash                          ram
 before   after   diff          before   after   diff
63988   64740    752   1.18%    6196    6196      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmi160/main.go
64048   64784    736   1.15%    6228    6228      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmp280/main.go
61472   62224    752   1.22%    6188    6188      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/adt7410/main.go
69720   70480    760   1.09%    6376    6376      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/ds3231/main.go
70692   71412    720   1.02%    3656    3656      0   0.00% tinygo build -size short -o ./build/test.hex -target=pinetime     ./examples/bma42x/main.go
ydnar added a commit to ydnar/tinygo that referenced this pull request Mar 14, 2025
Copy link
Contributor

@ydnar ydnar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My primary feedback is the duplication of const values and structural types between the two packages. Given reflectlite is an internal package, can it export the constants (like kindMask) and structural types (like rawType, elemType, ptrType, etc), to avoid 1:1 duplication between these two packages?

With that and merging of #4806, this should be good to go.

@@ -412,6 +343,7 @@ type Type interface {
}

// Constants for the 'meta' byte.
// These constants are also defined in the internal/reflectlite package.
const (
kindMask = 31 // mask to apply to the meta byte to get the Kind value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given reflectlite is an internal package, can these constants be shared, rather than duplicated? e.g. kindMask = reflectlite.KindMask?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aykevl what do you think? Seems like a good idea to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's definitely possible! I can update this PR if this PR is the one we're going for.

ydnar added a commit to ydnar/tinygo that referenced this pull request Mar 17, 2025
ydnar added a commit to ydnar/tinygo that referenced this pull request Mar 17, 2025
deadprogram pushed a commit that referenced this pull request Mar 17, 2025
@ydnar
Copy link
Contributor

ydnar commented Mar 17, 2025

Implemented in #4787.

@ydnar ydnar closed this Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants