Skip to content

Comments

shell: move position of last_nl in struct shell_backend_ctx_flags#103597

Merged
aescolar merged 1 commit intozephyrproject-rtos:mainfrom
maass-hamburg:shell_backend_flags_improve_order
Feb 25, 2026
Merged

shell: move position of last_nl in struct shell_backend_ctx_flags#103597
aescolar merged 1 commit intozephyrproject-rtos:mainfrom
maass-hamburg:shell_backend_flags_improve_order

Conversation

@maass-hamburg
Copy link
Member

last_nl is 8 bits long, put it at the beginning of the packed struct, so it can be read and written with one intruction, as it is now aligned.

`last_nl` is 8 bits long, put it at the beginning of the packed struct, so
it can be read and written with one intruction, as it is now aligned.

Signed-off-by: Fin Maaß <[email protected]>
@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 5, 2026

@maass-hamburg maass-hamburg marked this pull request as ready for review February 6, 2026 00:35
@zephyrbot zephyrbot added area: Shell Shell subsystem size: XS A PR changing only a single line of code labels Feb 6, 2026
@jakub-uC
Copy link
Contributor

jakub-uC commented Feb 13, 2026

Thanks for the suggestion! Just a quick note: this is a bitfield within a uint32_t, not a packed struct, so the compiler will likely handle it as a single word access regardless of the order. Do you happen to have any benchmarks or assembly output showing that this specific alignment improves efficiency? I suspect the impact might be negligible here.

@maass-hamburg
Copy link
Member Author

for RISCV:

before:

	return sh->ctx->ctx.flags.last_nl;
40009bd8:	29072683          	lw	a3,656(a4)
40009bdc:	0036d693          	srli	a3,a3,0x3
40009be0:	0ff6f693          	zext.b	a3,a3
	if ((z_flag_last_nl_get(sh) == 0U) ||
40009be4:	00068a63          	beqz	a3,40009bf8 <state_collect+0x210>
40009be8:	29072683          	lw	a3,656(a4)
40009bec:	0036d693          	srli	a3,a3,0x3
40009bf0:	0ff6f693          	zext.b	a3,a3
40009bf4:	f6d794e3          	bne	a5,a3,40009b5c <state_collect+0x174>
	sh->ctx->ctx.flags.last_nl = val;
40009bf8:	00379693          	slli	a3,a5,0x3
40009bfc:	29075783          	lhu	a5,656(a4)
40009c00:	8077f793          	andi	a5,a5,-2041
40009c04:	00d7e7b3          	or	a5,a5,a3
40009c08:	28f71823          	sh	a5,656(a4)

after:

	return sh->ctx->ctx.flags.last_nl;
40009afc:	28874683          	lbu	a3,648(a4)
	if ((z_flag_last_nl_get(sh) == 0U) ||
40009b00:	00068663          	beqz	a3,40009b0c <shell_process+0x224>
40009b04:	28874683          	lbu	a3,648(a4)
40009b08:	f6d79ce3          	bne	a5,a3,40009a80 <shell_process+0x198>
	sh->ctx->ctx.flags.last_nl = val;
40009b0c:	28f70423          	sb	a5,648(a4)

build cmd:
west build -p -b litex_vexriscv samples/subsys/shell/shell_module/ -- -DCONFIG_OUTPUT_DISASSEMBLY=y

As far as I know a compiler would never change the order of the members of a struct itself.

@aescolar aescolar merged commit 7ae79ef into zephyrproject-rtos:main Feb 25, 2026
33 of 34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: Shell Shell subsystem size: XS A PR changing only a single line of code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants