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

Docs for core data structures #232

Closed

Conversation

olynch
Copy link
Collaborator

@olynch olynch commented Aug 16, 2024

Work in progress.

@olynch olynch marked this pull request as draft August 16, 2024 22:59
@olynch olynch marked this pull request as ready for review August 16, 2024 23:17
@olynch
Copy link
Collaborator Author

olynch commented Aug 16, 2024

@0x0f0f0f I think this is ready to merge, as long as I have not said anything erroneous in the docstrings. If I have more docstrings, I'll open a new PR; I think it's best to not have an uber-PR.

src/EGraphs/egraph.jl Outdated Show resolved Hide resolved
"""
hashcons the constants in the e-graph

For performance reasons, the "head" of an e-node is hashed and the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For performance reasons, the "head" of an e-node is hashed and the
For performance reasons, the "head" of an e-node is hashed to be stored in a flat vector of unsigned integers ([`VecExpr`](@ref)) and the

hashcons the constants in the e-graph

For performance reasons, the "head" of an e-node is hashed and the
value is stored in this Dict.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
value is stored in this Dict.
actual Julia object of the e-node operation is stored in this Dict.

src/vecexpr.jl Outdated
Comment on lines 43 to 49
The meaning of the bitflags `isexpr` and `iscall` can be best understood through looking at
the source for `to_expr(g::EGraph, n::VecExpr)` in `src/EGraphs/egraph.jl`. Namely,
e-nodes for which `isexpr` is false have no arguments; their only "data" is their head.
E-nodes for which `isexpr` is true and `iscall` is also true correspond to
`Expr(:call, head, args...)` expressions, and e-nodes for which `isexpr` is true but
`iscall` is false correspond to `Expr(head, args...)` expressions. There should
not be `VecExpr`s with `isexpr = false` but `iscall = true`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the TermInterface source. This corresponds exactly to isexpr and iscall from TermInterface.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, OK! I'll update these docs to point to TermInterface.

src/vecexpr.jl Outdated
Comment on lines 51 to 52
The "signature" of an expression seems to in practice be computed as the hash of the head combined
with the number of arguments (the arity). See: [`addexpr!`]() in `src/EGraphs/egraph.jl`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The "signature" of an expression seems to in practice be computed as the hash of the head combined
with the number of arguments (the arity). See: [`addexpr!`]() in `src/EGraphs/egraph.jl`.
The "signature" of an expression is computed as the hash of the head combined
with the number of arguments (the arity). See: [`addexpr!`]() in `src/EGraphs/egraph.jl`.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing link to addexpr!

src/vecexpr.jl Outdated
Comment on lines 53 to 56
Perhaps in the future, signatures could also involve type information, e.g. to disambiguate
overloaded heads? Signatures are used in the `classes_by_op` dictionary in a e-graph,
so that when you are matching for `(a + b)` you can iterate over all of the e-classes
that have some e-node with `(+, 2)` as its signature.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this was more me documenting my own thoughts... I've updated this.

src/vecexpr.jl Outdated
so that when you are matching for `(a + b)` you can iterate over all of the e-classes
that have some e-node with `(+, 2)` as its signature.

It also seems like the signature of a constant is `0`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Comment on lines +5 to +14
Note: this is really more of a stack than a queue, because it is LIFO (last in
first out) rather than FIFO (first in, last out). That is,

```julia
q = UniqueQueue{Int}()
push!(q, 1)
push!(q, 2)
assert(pop!(q) == 2)
assert(pop!(q) == 1)
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. This is modeled after egg's unique queue. We should check egg's source code and see if it's FIFO or LIFO. Could mean a big performance gain if they're going FIFO and we're doing LIFO, we should try to align with their implementation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does seem like they are going FIFO:

https://github.com/egraphs-good/egg/blob/b3a53e9e575dc67ce1e8320e4e488f2873a67482/src/util.rs#L152-171

According to https://doc.rust-lang.org/std/collections/struct.VecDeque.html, push_back and pop_front is FIFO, and also VecDeque is implemented with a ring buffer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, we should fix then

Comment on lines 79 to 92
Potential optimization:

```julia
j = i
while j != uf.parents[j]
j = uf.parents[j]
end
root = j
while i != uf.parents[i]
uf.parents[i] = root
i = uf.parents[i]
end
root
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation detail should go in a comment and not in the docstring

Comment on lines 28 to 32
"""
UnionFind()

Creates a new empty unionfind.
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""
UnionFind()
Creates a new empty unionfind.
"""
"""
UnionFind()
Creates a new empty unionfind.
"""

Can you move it to the struct docstring please?

Comment on lines 595 to 597
"""
Look up a grounded pattern.
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""
Look up a grounded pattern.
"""
"""
Look up a ground pattern.
"""

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should need more detail

@olynch olynch closed this Sep 11, 2024
@olynch olynch mentioned this pull request Sep 11, 2024
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

Successfully merging this pull request may close these issues.

3 participants