nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.63k stars 1.47k forks source link

Minimize memory allocations when assigning multiple values to string variables. #1585

Closed Varriount closed 1 year ago

Varriount commented 10 years ago

Currently, whenever a string value is assigned to a variable, the string value is copied into a new piece of memory, regardless of the contents of the previous value. This can be optimized in certain cases - if assigning a value to an already initialized string variable, and the value being assigned is the same length or smaller than the already initialized string, than a simple memory copy can be done, rather than a dereference of the old string and creation of a new string.

Test case:

proc main =
  var a = "Hello"
  var b = "World"
  var c = "Hello World"

  echo(repr(a)) # Address 000000000037C050
  echo(repr(b)) # Address 000000000037C078
  echo(repr(c)) # Address 000000000037D050
  echo()
  a = b # Since the contents of B can fit into a, the contents
        # are copied and a new string is not created.
  echo(repr(a))     # Address remains unchanged.

  a = c # Since the contents of C *cannot* fit into a, a new string
        # is created.
  echo(repr(a))     # Address is changed.

main()

Though this means checking the length of the old string variable on each assignment, this overhead is probably much less than that of allocating memory for a new string and garbage collecting the old string.

simonkrauter commented 10 years ago

If I understand it correct, you can do this only when ref count is 1.

Varriount commented 10 years ago

@trustable-code Yes, this only applies to strings with a recount of 1. Thankfully, due to strings being value types, this is the case 95% of the time.

Araq commented 6 years ago

Interestingly, this could be a good idea for the upcoming strings based on destructors too.

metagn commented 1 year ago

We have moves now, in the example the addresses of b and c are both sustained when moving into a

Araq commented 1 year ago

Bites with COW-strings which hopefully will come to Nim, eventually.