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

Replace varnames with Identifiers. #5305

Closed
wants to merge 1 commit into from

Conversation

ttsugriy
Copy link
Contributor

This builds on top of #5304 and replaces varnames with Identifier instances to
make the API cleaner, but more importantly open up new possibilities for optimizations
around ways to perform lookups.

@ttsugriy ttsugriy force-pushed the env-identifier branch 2 times, most recently from 06876a2 to ad06c08 Compare June 9, 2018 00:00
@ttsugriy
Copy link
Contributor Author

ttsugriy commented Jun 9, 2018

I've rebased this change to exclude commits that have already been merged. @laurentlb, please take a look.

This makes the API cleaner, but more importantly opens up new
possibilities for optimizations around ways to perform lookups.
@laurentlb
Copy link
Contributor

Can you clarify how this is going to help? Identifier is an AST node, maybe it's better to use it only inside the AST?

For fast lookups, we can add some extra information in Indentifier. For example, we can have an enum (local / global) and an id (int). Inside a function, each identifier has a different id.
This is what we have in the Go implementation: https://github.com/google/skylark/blob/master/syntax/syntax.go#L263

(cc @alandonovan @brandjon)

.entrySet()
.stream()
.collect(
ImmutableMap.toImmutableMap(e -> Identifier.of(e.getKey()), e -> e.getValue())));
Copy link
Contributor

Choose a reason for hiding this comment

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

An Identifer is a syntax node. Only the parser should be in the business of creating them.

@@ -899,7 +905,7 @@ public ConstraintSemantics getConstraintSemantics() {
}

/** Returns all skylark objects in global scope for this RuleClassProvider. */
public Map<String, Object> getTransitiveGlobalBindings() {
public Map<Identifier, Object> getTransitiveGlobalBindings() {
Copy link
Contributor

Choose a reason for hiding this comment

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

It is the job of the static resolver (validation) pass to resolve each identifier to the logical variable to which it refers. For a global variable, you could use a small integer index into a flat array, or the string name of the variable (just as the existing code does) since names within a module are unique. It makes no sense to use Identifiers in the representation of the environment: identifiers are not canonical.

I recommend you first read and understand how the Go implementation resolves identifiers and represents the environment, as it is the clearest expression of the necessary algorithm.

@ttsugriy
Copy link
Contributor Author

@laurentlb, I agree that the ultimate goal is to use an approach similar to what go implementation does. The motivation for this change was:

  1. https://source.bazel.build/bazel/+/master:src/main/java/com/google/devtools/build/lib/syntax/Identifier.java;l=25?q=Identifier.java
  2. to enable certain types of optimizations. For example https://source.bazel.build/bazel/+/master:src/main/java/com/google/devtools/build/lib/syntax/Identifier.java;l=23?q=Identifier.java

If you don't believe that comments above are relevant, should they be updated? I'll also start looking into go implementation to see how hard would it be to port its logic to Bazel's interpreter.

@laurentlb
Copy link
Contributor

Good point. Let's remove these two comments. I don't think they are needed to implement (3).

@ttsugriy
Copy link
Contributor Author

makes sense. Closing this PR.

@ttsugriy ttsugriy closed this Jun 11, 2018
bazel-io pushed a commit that referenced this pull request Jul 24, 2018
Comments are misleading, as discussed on #5305 (comment)

RELNOTES: None.
PiperOrigin-RevId: 205841782
werkt pushed a commit to werkt/bazel that referenced this pull request Aug 2, 2018
Comments are misleading, as discussed on bazelbuild#5305 (comment)

RELNOTES: None.
PiperOrigin-RevId: 205841782
luca-digrazia pushed a commit to luca-digrazia/DatasetCommitsDiffSearch that referenced this pull request Sep 4, 2022
    Comments are misleading, as discussed on bazelbuild/bazel#5305 (comment)

    RELNOTES: None.
    PiperOrigin-RevId: 205841782
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants