v icon indicating copy to clipboard operation
v copied to clipboard

`str1 + str2 + str2` should be optimized

Open medvednikov opened this issue 6 years ago • 12 comments

Concatenating multiple strings should always result in only one allocation.

medvednikov avatar Jun 19 '19 11:06 medvednikov

Have you thought about python style string formatting "hello {} {}".format(firstName, lastName) or something similar? Edit: I guess we already have concatenation (+) and interpolation with variables ($var) so don't need the complexity

Also for joining and splitting arrays ",".join(array) or array.split(",") ?

joe-conigliaro avatar Jun 20 '19 04:06 joe-conigliaro

Edit: I guess we already have concatenation (+) and interpolation with variables ($var) so don't need the complexity

You've answered your question correctly :)

Also for joining and splitting arrays ",".join(array) or array.split(",") ?

str_array.join(','), but no array splits.

medvednikov avatar Jun 20 '19 09:06 medvednikov

Is it now str1 + str2 + str3 or str1 << str2 << str3 ?

gslicer avatar Sep 24 '19 13:09 gslicer

@gslicer the first one

Delta456 avatar Sep 24 '19 15:09 Delta456

@Delta456

@gslicer the first one

That seems again inconsistent with the Array notation where it is stated (according to docs): "<< is an operator that appends a value to the end of the array. It can also append an entire array. "

So I would expect string (which is a [byte|rune]-Array to support the same notation - why not let + be pure mathematical?

When there was something I liked in C++ it was the "<<" operator on iostreams (which were basically byte-streams or var-length strings ;)

gslicer avatar Sep 24 '19 21:09 gslicer

string is not an array.

medvednikov avatar Sep 24 '19 21:09 medvednikov

I'm sorry @medvednikov but in current implementation string looks like an array to me (and you even called the variable representing a string "arr" which resambles Array):

module strings

pub fn repeat(c byte, n int) string {
	if n <= 0 {
		return ''
	}
	//mut arr := malloc(n + 1)
	mut arr := [byte(0)].repeat(n + 1)
	for i := 0; i < n; i++ {
		arr[i] = c
	}
	arr[n] = `\0`
	return string(arr, n)
}

My point is, + could be used only for arithmetic operations, and << for concatenation (of any kind) - this would be more streamlined with the "simple" and "do similar stuff with similar syntax" approach, don't you think?

gslicer avatar Sep 25 '19 08:09 gslicer

No, it's not an array. String is an immutable struct with a pointer to string data, len, and capacity integers.

In this function arr refers to []byte from which a string is generated via string().

medvednikov avatar Sep 25 '19 11:09 medvednikov

By the way thanks for referring to this function, it needs to be updated :)

medvednikov avatar Sep 25 '19 11:09 medvednikov

Things like that may be nice, but not only it's additional complexity in the compiler, but also it's not even obvious that there are such optimizations, so it's one more random thing to learn. I could say that maybe it's worth making it more general, like make operators variadic. But that's just complicating something that is already simple. I would say that ''.join(s1,s2,s3) is fine, and even ''.join(s1,s2) is fine. Like if only in C++ there was no + for std::string, so much of code would be automatically optimized, you would just call concat(or whatever it would be called). So I think it's fine to remove operator overloading all together, because it inevitably leads to slow code. Let's not repeat the same mistakes.

adler-amorette avatar Feb 22 '22 13:02 adler-amorette

Related: https://github.com/vlang/v/pull/22188

esquerbatua avatar Sep 26 '24 22:09 esquerbatua

Things like this should be simple in the new compiler. The IL will make optimizations much simpler.

JalonSolov avatar Sep 26 '24 22:09 JalonSolov