-
Notifications
You must be signed in to change notification settings - Fork 32
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
Idea for an efficient string representation #238
Comments
An efficient string representation will be a huge improvement! Currently, we have one We might also be able to achieve an efficient string representation by introducing 2 specialized type Char rune
var _ Term = Char(0) // Behaves as if it's a single character Atom. type List []Term
var _ Term = List(nil) // Behaves as if it's a `./2` or `Atom("[]")`. Let's see how other implementations implement strings. Other than strings, |
I've been thinking about this. It could be better if we generalize compound terms, not lists. type Compound interface {
Term
Arg(n int) Term
}
type Term interface {
...
PrincipalFunctor() Compound // e.g. '.'/2
} That way, we should be able to make any structures, even type CharacterList string
var _ Compound = CharacterList("") |
Did you consider partial strings? Here is an explanation. |
As of type Compound interface {
Functor() Atom
Arity() int
Arg(n int) Term
}
|
Wow, this sounds amazing, thank you a lot for working on this! Two small points I noticed:
|
How expensive is
You are supporting the In Scryer, partial strings are constant for above operation and do not involve any extra space. |
@triska Thank you!
|
@UWN It's constant. A Go string is a slice of bytes and the slice points to a section of the underlying array of bytes.
// https://github.com/golang/go/blob/846c378b8c0cebd2d8522a5693b45ca95b018a78/src/runtime/slice.go#L15
type slice struct {
array unsafe.Pointer
len int
cap int
} So, |
OK, but where does that |
It's stored in the heap in this case but it's not because it's a slice. In Go, the compiler decides whether an object gets stored in the heap or the stack and it always stores The compiler will store multi-word objects like slice (3 words + padding) in the stack if the conditions are met. |
Looks good. So these details are just in the compiler and not in your code. |
I had a crazy idea recently. Not sure if it would work, but it could maybe be a relatively low-effort way to get faster strings.
The gist is:
[]rune
rune(-1)
could mean[]
,rune(-5)
could mean variableVar("_5")
This would let us take advantage of Go's slices for a lot of common list operations.
Potential issues I could see:
Maybe a slightly less crazy representation would be:
and store the variable ID as a local index instead of the global ID, so
rune(-5)
would refer tovars[5]
or something like that.BTW, this project got a shoutout in @triska's latest video! https://youtu.be/CvLsVfq6cks?t=4625
Congrats 🎉
The text was updated successfully, but these errors were encountered: