Skip to content

Optimize jsmntok_t memory footprint via configurable field types - #248

Open
metalalive wants to merge 1 commit into
zserge:masterfrom
metalalive:feature/custom-token-size
Open

Optimize jsmntok_t memory footprint via configurable field types#248
metalalive wants to merge 1 commit into
zserge:masterfrom
metalalive:feature/custom-token-size

Conversation

@metalalive

Copy link
Copy Markdown

Description

This patch introduces build-time configuration macros for jsmntok_t field types to reduce memory usage on
resource-constrained targets as described in the issue 239.

Changes

  • added macros for token fields (e.g. JSMN_TOKEN_START_TYPE) with default fallback to int.
  • refactored jsmntok_t struct to utilize these macros, allowing users to override types (e.g., short, int8_t) before
    including jsmn.h.
  • moved jsmntype_t type to the end of the struct and applied bit-field sizing (: 8) to improve struct packing and alignment.
  • updated README.md with usage examples for memory-constrained environments.

Impact

Size of a jsmntok_t instance can be reduced , enabling more token array items in limited RAM environments without changing the API.

Test Result

I don't find any CI flow (GitHub Action ?) in this repo , allow me to post my test result at here .

make test with custom config.mk

cc -g -DJSMN_TOKEN_START_TYPE=char -DJSMN_TOKEN_END_TYPE=char -DJSMN_TOKEN_SIZE_TYPE=char  test/tests.c -o test/test_default
./test/test_default

PASSED: 16
FAILED: 0
cc -DJSMN_STRICT=1 -g -DJSMN_TOKEN_START_TYPE=char -DJSMN_TOKEN_END_TYPE=char -DJSMN_TOKEN_SIZE_TYPE=char  test/tests.c -o test/test_strict
./test/test_strict

PASSED: 16
FAILED: 0
cc -DJSMN_PARENT_LINKS=1 -g -DJSMN_TOKEN_START_TYPE=char -DJSMN_TOKEN_END_TYPE=char -DJSMN_TOKEN_SIZE_TYPE=char  test/tests.c -o test/test_links
./test/test_links

PASSED: 16
FAILED: 0
cc -DJSMN_STRICT=1 -DJSMN_PARENT_LINKS=1 -g -DJSMN_TOKEN_START_TYPE=char -DJSMN_TOKEN_END_TYPE=char -DJSMN_TOKEN_SIZE_TYPE=char  test/tests.c -o test/test_strict_links
./test/test_strict_links

PASSED: 16
FAILED: 0

memory check with the test executable

valgrind --leak-check=full ./test/test_default 
==39382== Memcheck, a memory error detector
==39382== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==39382== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info
==39382== Command: ./test/test_default
==39382== 

PASSED: 16
FAILED: 0
==39382== 
==39382== HEAP SUMMARY:
==39382==     in use at exit: 0 bytes in 0 blocks
==39382==   total heap usage: 37 allocs, 37 frees, 1,504 bytes allocated
==39382== 
==39382== All heap blocks were freed -- no leaks are possible
==39382== 
==39382== For lists of detected and suppressed errors, rerun with: -s
==39382== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Signed-off-by: T.H. <7197142+metalalive@users.noreply.github.com>
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.

1 participant