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

add no-copy ast.getStrVal for {nkIdent, nkSym, kStrLit..nkTripleStrLit} #17540

Closed

Conversation

timotheecour
Copy link
Member

@timotheecour timotheecour commented Mar 27, 2021

(migrated from #17500 (comment))

add a no-copy getStrVal; see discussion in #17500 (comment)

@timotheecour timotheecour force-pushed the pr_followup_customlit_extras branch 2 times, most recently from 0e871db to 8e33942 Compare March 27, 2021 20:53
@timotheecour timotheecour changed the title [do not review yet] add no-copy ast.getStrVal for {nkIdent, nkSym, kStrLit..nkTripleStrLit} Mar 27, 2021
@timotheecour timotheecour marked this pull request as ready for review March 27, 2021 20:59
@timotheecour timotheecour changed the title add no-copy ast.getStrVal for {nkIdent, nkSym, kStrLit..nkTripleStrLit} add no-copy ast.getStrVal for {nkIdent, nkSym, kStrLit..nkTripleStrLit} Mar 27, 2021
@saem
Copy link
Contributor

saem commented Mar 27, 2021

I don't see why we need to add this to ast, also introducing a global for it?

@timotheecour
Copy link
Member Author

timotheecour commented Mar 27, 2021

I don't see why we need to add this to ast, also introducing a global for it?

it simplifies client code, as you can see in compiler/renderer.nim, and macros.strVal defines something similar (i'm not considering the comments for now because comment node API may change in future as was discussed elsewhere).

The global is harmless, it's just private symbol which simplifies client code and allows safe zero-copy access.

@Clyybber
Copy link
Contributor

Clyybber commented Mar 27, 2021

I don't think it's worth it. The cases where you "need" a proc that gives you the strval regardless of wether it's a sym or and ident are rare (only two instances in renderer.nim).

@timotheecour
Copy link
Member Author

timotheecour commented Mar 27, 2021

no there are many other cases, I just didn't want to do the changes in the same PR.
eg:

    errorUndeclaredIdentifier(c, info.info, if n.kind == nkIdent: n.ident.s
      else: n.strVal)
=>
    errorUndeclaredIdentifier(c, info.info, n.getStrVal)

it'll also simplify cases like this for eg:

    case x.kind
    of nkIdent: id.add(x.ident.s)
    of nkSym: id.add(x.sym.name.s)
    let x = n[0]
    let ident = case x.kind
                of nkIdent: x.ident
                of nkSym: x.sym.name
                of nkClosedSymChoice, nkOpenSymChoice: x[0].sym.name
                else: nil
    if ident != nil and ident.s == "[]":
      let b = newNodeI(nkBracketExpr, n.info)
      # These vars are of type `cstring` to prevent unnecessary string copy.
      var aStrVal: cstring = nil
      var bStrVal: cstring = nil
      # extract strVal from argument ``a``
      case aNode.kind
      of nkStrLit..nkTripleStrLit:
        aStrVal = aNode.strVal.cstring
      of nkIdent:
        aStrVal = aNode.ident.s.cstring
      of nkSym:
        aStrVal = aNode.sym.name.s.cstring
      of nkOpenSymChoice, nkClosedSymChoice:
        aStrVal = aNode[0].sym.name.s.cstring
      else:
        discard
      # extract strVal from argument ``b``
      case bNode.kind
      of nkStrLit..nkTripleStrLit:
        bStrVal = bNode.strVal.cstring
      of nkIdent:
        bStrVal = bNode.ident.s.cstring
      of nkSym:
        bStrVal = bNode.sym.name.s.cstring
      of nkOpenSymChoice, nkClosedSymChoice:
        bStrVal = bNode[0].sym.name.s.cstring
      else:
        discard

as well as debugging

using accessors instead of raw field access is not something particularly surprising.

@Clyybber
Copy link
Contributor

Clyybber commented Mar 27, 2021

I think I'll retract #17500 (comment) then, because the template is still much better than this approach.

as well as debugging

You'd use $ for debugging.

@timotheecour
Copy link
Member Author

timotheecour commented Mar 27, 2021

the template causes a copy, whereas this PR doesn't.

You'd use $ for debugging.

different meaning, different semantics (eg for sym $ may show different results), both are useful; and $ also causes a copy.

@Araq
Copy link
Member

Araq commented Mar 29, 2021

The accessor could simply return the PIdent if it weren't for the strVal case which is however also much rarer. In general you cannot use string literals where an identifier is required in Nim.

@timotheecour timotheecour mentioned this pull request Apr 8, 2021
@timotheecour
Copy link
Member Author

The accessor could simply return the PIdent

=> #17684

In general you cannot use string literals where an identifier is required in Nim.

but there are plenty of existing, valid, cases in compiler where we do end up using the underlying string

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.

4 participants