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

Elide memtrace when disabled #97

Merged
merged 7 commits into from
Feb 11, 2025
Merged

Elide memtrace when disabled #97

merged 7 commits into from
Feb 11, 2025

Conversation

Nycto
Copy link
Collaborator

@Nycto Nycto commented Jan 24, 2025

This change does some code organization, but most importantly it completely removes the memory tracing code when it isn't enabled. This is likely causing inflated memory consumption for games using the bindings at the moment, as it allocates memory for the memtrace regardless of whether tracing is enabled.

@Nycto Nycto requested a review from ninovanhooff January 24, 2025 02:31
Copy link
Collaborator

@ninovanhooff ninovanhooff left a comment

Choose a reason for hiding this comment

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

Hey, I'd welcome this change and the tests pass, as the github action shows. The example app doesn't seem to compile tho.

CC: ../../src/playdate/api.nim
CC: playdate_example.nim
/Users/ninovanhooff/PlaydateProjects/playdate-nim/src/playdate/api.nim:20:135: error: incompatible function pointer types passing 'void (*const)(const char *, ...)' to parameter of type 'tyProc__YfDRPZutFzFrjPOw19ct0Pg' (aka 'void (*)(char *, ...)') [-Wincompatible-function-pointer-types]
   20 |                 nimln_(20);             _ZN8initreqs11initPrereqsE4procI7pointer7pointer4uIntE4procI7cstringE((*(*playdateAPIX60gensym42__p0).system).realloc, (*(*playdateAPIX60gensym42__p0).system).logToConsole);
      |                                                                                                                                                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/ninovanhooff/.cache/nim/playdate_example_d/simulator/@mplaydate_example.nim.c:515:177: note: passing argument to parameter 'log_p1' here
  515 | N_LIB_PRIVATE N_NIMCALL(void, _ZN8initreqs11initPrereqsE4procI7pointer7pointer4uIntE4procI7cstringE)(tyProc__K8zkGJyGTJQIxluyMf4zXw realloc_p0, tyProc__YfDRPZutFzFrjPOw19ct0Pg log_p1);
      |                                                                                                                                                                                 ^
1 error generated.
Error: execution of an external compiler program 'clang -c -w -ferror-limit=3 -DTARGET_EXTENSION=1 -Wall -Wno-unknown-pragmas -Wdouble-promotion -I/Users/ninovanhooff/Developer/PlaydateSDK/C_API -arch x86_64 -arch arm64 -DTARGET_SIMULATOR=1 -Wstrict-prototypes -g   -I/Users/ninovanhooff/.choosenim/toolchains/nim-2.0.10/lib -I/Users/ninovanhooff/PlaydateProjects/playdate-nim/playdate_example/src -o /Users/ninovanhooff/.cache/nim/playdate_example_d/simulator/@mplaydate_example.nim.c.o /Users/ninovanhooff/.cache/nim/playdate_example_d/simulator/@mplaydate_example.nim.c' failed with exit code: 1

Perhaps there is some confusion regarding logToConsole from the bindings/system module and the exposed system module?

@ninovanhooff
Copy link
Collaborator

Let's see if #90 is fixed with this change

@Nycto
Copy link
Collaborator Author

Nycto commented Feb 4, 2025

Oof. This looks like a Nim compiler bug related to compatibility with GCC 14. You can find a similar issue here: nim-lang/Nim#23665

@Nycto
Copy link
Collaborator Author

Nycto commented Feb 5, 2025

I’ve got a fix for this, I think. Let me do some testing and I’ll push

This was causing an error in GCC 14 related to incompatible pointer
types -- 'void (*)(char *, ...)' instead of 'void (* const)(const char *, ...)'
@Nycto Nycto requested a review from ninovanhooff February 5, 2025 15:08
@Nycto
Copy link
Collaborator Author

Nycto commented Feb 5, 2025

Pushed an update here to fix the underlying c type of the function parameter to be a const. This fixes it for me locally — @ninovanhooff can you give it a test?

@ninovanhooff
Copy link
Collaborator

ninovanhooff commented Feb 9, 2025

Unfortunately doesn't work yet for me on Mac. I did check your latest ConstChar change was pulled and did a nimble clean. Also deliberately introduced a typo in bindings/system.nim to make sure I was looking at the code that was actually used.

CC: setup
CC: playdate_example.nim
/Users/ninovanhooff/PlaydateProjects/playdate-nim/src/playdate/api.nim:20:135: error: incompatible function pointer types passing 'void (*const)(const char *, ...)' to parameter of type 'tyProc__YfDRPZutFzFrjPOw19ct0Pg' (aka 'void (*)(char *, ...)') [-Wincompatible-function-pointer-types]
   20 |                 nimln_(20);             _ZN8initreqs11initPrereqsE4procI7pointer7pointer4uIntE4procI7cstringE((*(*playdateAPIX60gensym42__p0).system).realloc, (*(*playdateAPIX60gensym42__p0).system).logToConsole);
      |                                                                                                                                                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/ninovanhooff/.cache/nim/playdate_example_d/simulator/@mplaydate_example.nim.c:515:177: note: passing argument to parameter 'log_p1' here
  515 | N_LIB_PRIVATE N_NIMCALL(void, _ZN8initreqs11initPrereqsE4procI7pointer7pointer4uIntE4procI7cstringE)(tyProc__K8zkGJyGTJQIxluyMf4zXw realloc_p0, tyProc__YfDRPZutFzFrjPOw19ct0Pg log_p1);
      |                                                                                                                                                                                 ^
1 error generated.
Error: execution of an external compiler program 'clang -c -w -ferror-limit=3 -DTARGET_EXTENSION=1 -Wall -Wno-unknown-pragmas -Wdouble-promotion -I/Users/ninovanhooff/Developer/PlaydateSDK/C_API -arch x86_64 -arch arm64 -DTARGET_SIMULATOR=1 -Wstrict-prototypes -g   -I/Users/ninovanhooff/.choosenim/toolchains/nim-2.0.10/lib -I/Users/ninovanhooff/PlaydateProjects/playdate-nim/playdate_example/src -o /Users/ninovanhooff/.cache/nim/playdate_example_d/simulator/@mplaydate_example.nim.c.o /Users/ninovanhooff/.cache/nim/playdate_example_d/simulator/@mplaydate_example.nim.c' failed with exit code: 1

Regarding GCC 14; I can't remember when I installed that, and it seems in fact I did not. note that clang is used.

It is supplied by the Xcode toolchain, and might be updated automatically when XCode is updated.
Going with clang seems to be the recommended route; from what I'm reading online.
Apple even goes as far as to alias it:

❯ which gcc
/usr/bin/gcc
❯ gcc -v
Apple clang version 16.0.0 (clang-1600.0.26.3)
Target: arm64-apple-darwin23.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Note: I also performed a Sanity check on Windows, where GCC 10.3.0 is installed. Indeed, it works fine there

@Nycto
Copy link
Collaborator Author

Nycto commented Feb 9, 2025

Good to know — I tested against gcc. I’ll test against clang next

@ninovanhooff
Copy link
Collaborator

Hold the phone... I decided to add a github action that compiles the example project on mac os.
To my surprise, it succeeded. While it is based on the dev branch. Meaning that the changes in this PR would not be required. And unexplicably a local build of the code now succeeds as well. I'll have to look at this with a fresh head.

@ninovanhooff
Copy link
Collaborator

Ok, so I organised my thoughts. The reason my action was succeeding is that it was based on samdze/dev and not Nycto:elideMemtrace.

So that means we have a passing baseline run
https://github.com/ninovanhooff/playdate-nim/actions/runs/13228444045

And a failing run that is based on the elide branch
https://github.com/ninovanhooff/playdate-nim/actions/runs/13228717184/job/36922898833

@ninovanhooff
Copy link
Collaborator

ninovanhooff commented Feb 9, 2025

You may test fixes by branching off Nycto#1
Although, running a clang environment locally might give you the same result in a more practical way.

Unfortunately, it seems the action compiles the nim sources rather than installing prebuilt, and doesn't cache the results. So the run might take some time

@Nycto
Copy link
Collaborator Author

Nycto commented Feb 11, 2025

Good news! This was an easy fix. I just needed to share the same type definition everywhere it’s used.

Copy link
Collaborator

@ninovanhooff ninovanhooff left a comment

Choose a reason for hiding this comment

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

Excellent! Thanks for this contribution! Let's merge it

@Nycto Nycto merged commit 65ecc3c into samdze:dev Feb 11, 2025
8 checks passed
@Nycto Nycto deleted the elideMemtrace branch February 11, 2025 14:28
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