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

Splitting with empty string as separator causes high memory usage #4

Closed
Esgorhannoth opened this issue Dec 2, 2023 · 3 comments
Closed

Comments

@Esgorhannoth
Copy link

"test" "" split

causes high memory usage and heavy swapping, so that PC sometimes even freezes (Windows 10 x64, 8 GB RAM)

My intention was to get separate characters, but according to nim's split documentation, with empty separator nim's split returns unmodified string. Somehow split in mn does neither.

@h3rald
Copy link
Owner

h3rald commented Dec 2, 2023

This is really weird, because the split symbol can be used with an empty separator in min:

image

...but in mn it hangs. The annoying thing is that the implementation is essentially the same, but perhaps in min if the pcre library is included the behavior changes.

Perhaps the easiest thing would be to handle this as a special case in mn and if the separator is an empty string try to use something else in Nim to split the string, maybe create a sequence via an iterator or something.

@Esgorhannoth
Copy link
Author

Esgorhannoth commented Dec 2, 2023

Seems like Nim problem. I have written a simple program to test my suggestion:

import
     strutils

proc main =
    var i = 0
    echo "hello".split("")    ## compiles, but assertion (sep.len > 0) error at runtime 
    #but
    for e in "hello".split(""):  ## infinite loop
        i.inc
        echo i       ## to confirm that something happens
        echo e

main()

Possible solution:

 def.symbol("split") do (i: In):
    let vals = i.expect("'sym", "'sym")
    let sep = vals[0].getString
    let s = vals[1].getString
    var q = newSeq[MnValue](0)
    if sep.len > 0:
      for e in s.split(sep):
        q.add e.newVal
    else:
      for e in s:
        q.add e.newVal
    i.push q.newVal

@h3rald h3rald closed this as completed in b028b43 Dec 2, 2023
@h3rald
Copy link
Owner

h3rald commented Dec 2, 2023

Fixed on master 😊👍

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

No branches or pull requests

2 participants