Skip to content

Conversation

ZoomRmc
Copy link
Contributor

@ZoomRmc ZoomRmc commented Apr 2, 2025

Adds system.setLenUninit for the string type. Allows setting length without initializing new memory on growth.

@ZoomRmc
Copy link
Contributor Author

ZoomRmc commented Apr 3, 2025

systr.setLengthStrUninit fails with mm:refc on Linux.
Seems my naïve approach is wrong.
Can this be made to work without introducing new compiler procs?

@arnetheduck
Copy link
Contributor

arnetheduck commented Apr 3, 2025

the same for seq would be nice, ie strings are pretty niche at the end of the day.
never mind, forgot that it was already fixed :)

@arnetheduck
Copy link
Contributor

ping, would be nice to see this PR merged

@ZoomRmc
Copy link
Contributor Author

ZoomRmc commented Sep 14, 2025

I think I was stuck on not being sure if it's done and both string versions actually work, since some of CI errors at the time were unrelated.

Those string/seq impls is a mess of conditionals and I don't trust locally-run tests.

@ZoomRmc ZoomRmc marked this pull request as draft September 16, 2025 15:22
@ZoomRmc
Copy link
Contributor Author

ZoomRmc commented Sep 16, 2025

Nim/lib/system.nim

Lines 2316 to 2326 in 2e45f61

when notJSnotNims:
when defined(nimSeqsV2):
{.noSideEffect.}:
let str = unsafeAddr s
setLengthStrV2Uninit(cast[ptr NimStringV2](str)[], newlen)
else:
{.noSideEffect.}:
when hasAlloc:
setLengthStrUninit(s, newlen)
else:
s.setLen(n)

Adding a hasAlloc check and and else-downgrade to setLen for the old string branch was necessary for this to compile. I'm not sure if it's correct as I'm not sure what constitutes the string type at this point.

@ZoomRmc ZoomRmc marked this pull request as ready for review September 16, 2025 23:07
@Araq
Copy link
Member

Araq commented Sep 18, 2025

Apart from my code review remarks, seems fine.

@ZoomRmc
Copy link
Contributor Author

ZoomRmc commented Sep 18, 2025

So I went with expanding that early return conditional at line 232 a bit and added some internal docs. If it's still not up your alley I can change the code to whatever.

If it's ok as is, do you want me to squash it?

BTW, you can't currently build docs with --docInternal for system due to assertion defect in semtypes:

...assertions.nim(34)       raiseAssert
Error: unhandled exception: semtypes.nim(2471, 7) `c.graph.sysTypes[tySequence] == nil`  [AssertionDefect]```

- Required for a follow-up to nim-lang#15951
- Accompanies nim-lang#19727 but for strings
- Expands `stdlib/tstring` with tests for `setLen` and `setLenUninit`
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.

3 participants