This post follows on from the previous one. (Go Interface Allocation) After realizing the allocation from assigning strings to interfaces (common in gSuneido) I started thinking about how this could be improved. I had a few ideas:
- predefine all 256 one byte strings (common when looping over a string by character)
- implement a "small string" type (i.e. a one byte size and 15 byte array in 16 bytes, the same space as a Go string pointer + size)
- intern certain strings with the new Go unique package (unique.Make returns a Handle which is a pointer so it could be stored in an interface without allocation)
Unfortunately, the last two require introducing new string types. That's feasible (gSuneido already has an additional SuConcat string type to optimize concatenation) but it would take some work.
Luckily the first idea was easy and didn't require a new string type.
func SuStr1(s string) Value { if len(s) == 1 { return SuStr1s[s[0]] } return SuStr(s) } var SuStr1s = [256]Value{ SuStr("\x00"), SuStr("\x01"), SuStr("\x02"), SuStr("\x03"), ... SuStr("\xfc"), SuStr("\xfd"), SuStr("\xfe"), SuStr("\xff"), }
It didn't make much difference overall but it was easy to see the improvements on specific types of code. For a simple change I think it was worth it. (Thanks to "AI" for auto-completing most of that 256 element array.)
No comments:
Post a Comment