Feature idea

5 views
Skip to first unread message

Vitali Lovich

unread,
Apr 27, 2009, 3:33:30 AM4/27/09
to Google-Web-Toolkit-Contributors
Kinda like with GCC, allow detection of constant values (i.e. __builtin_constant_p).  This way, you could do something like

void addParameter (HashMap h, int size, String key, Object value)
{
   if (GWT.isConstantValue(h, null)) {
      if (GWT.isConstantValue(size, 0))
         size = 10;
      h = new HashMap(size);
   }
   h.put(key, value).
}

& you could have the performance of

void addParameter (HashMap h?, int size?, String key, Object value)

as if you wrote overloaded methods without needing to write several different methods that just supply default values back & forth.  Sometimes, it's also possible to use a better algorithm if parameters have a known constant value.

Ray Cromwell

unread,
Apr 27, 2009, 3:45:05 AM4/27/09
to Google-Web-Tool...@googlegroups.com
+1

I suggested a similar feature a few days ago privately, I called it
GWT.isLiteral(), since the underlying check is if its an AST literal
in the compiler, although in my example, you can't do value
comparisons, just assertions on the literal. The value checks would be
done via traditional operators.

Vitali Lovich

unread,
Apr 27, 2009, 4:49:48 AM4/27/09
to Google-Web-Tool...@googlegroups.com
Yeah, probably from a compiler's perspective, the value check may be more complicated (the value may only be available after the AST stage depending on when optimization is done)

It can possibly bloat the resultant code (since I believe you may have to build different implementations of each function and you may get overhead, even if you inline).  However, I think this might be an OK tradeoff since the assumption would be that you are optimizing for speed as opposed to size if you use something like GWT.isLiteral.

Joel Webber

unread,
Apr 27, 2009, 9:30:28 AM4/27/09
to Google-Web-Tool...@googlegroups.com, Lex Spoon, Scott Blum
Cool, I had no idea GCC provided that information. I won't claim to be one of the compiler gurus, but this sounds pretty feasible to me, and to Ray's point, might be a useful way of loosening the GWT.create() magic a bit.

@Lex, Scott: What do you think? Is this relatively easy and useful?

Bruce Johnson

unread,
Apr 27, 2009, 12:23:20 PM4/27/09
to Google-Web-Tool...@googlegroups.com, Lex Spoon, Scott Blum
It seems like the general functionality (i.e. GWT.isLiteral()) ought to be implemented at the same time as method cloning. That's what makes me excited about the whole thing. Otherwise, at least for me, it's hard to wrap my head around how you could usefully emit different code for the same method for different call sites. Maybe I'm missing something.

A similar but related concept that I think would be the best first step is a parameter annotation called @MustBeLiteral. Then any method could become a "GWT.create()" indirectly because you'd be able to write:

public <T> T myFactory(@MustBeLiteral Class<? extends T> classLit) {
  T t = GWT.create(classLit);
  // do some stuff to tweak the t instance
  return t;
}

That seems way useful and perhaps the easiest of everything to implement.

Ray Cromwell

unread,
Apr 27, 2009, 2:26:16 PM4/27/09
to Google-Web-Tool...@googlegroups.com, Lex Spoon, Scott Blum
One possibility is to use hidden method parameters.

Let L = {the set of all method parameters which are referenced by
GWT.isLiteral() in the body}

For each parameter P in L, add a new parameter to the method, PiL (P
is Literal).

that is, in Vitali's example:
void addParameter (HashMap h, int size, String key, Object value)

becomes

void addParameter (HashMap h, int size, String key, Object value,
boolean hIL, boolean sizeIL)

now for each GWT.isLiteral/isConstanValue call, replace with the
corresponding hidden parameter

void addParameter (HashMap h, int size, String key, Object value,
boolean hIL, boolean sizeIL) {
if(hIl) {
if(sizeIL) {
...
}
}
}

finally, for each callsite C that invokes the method, if any parameter
P evaluates to a literal, replace the callsite with the hidden
parameters statically evaluated

addParameter(h, size, value)

with
(e.g)
addParameter(h, size, value, true, false)


-Ray
Reply all
Reply to author
Forward
0 new messages