Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#19 TextBlockInstruction perf improvement

closed-accepted
Nicholas Cull
None
5
2001-06-12
2001-06-06
Attila Szegedi
No

I've patched TextBlockInstruction so it evaluates
faster. I have changed internal representation of its
data from String to char[]. This way, the process()
method will call PrintWriter.print(char[]) instead of
PrintWriter.print(String) -- the Writer will need not
extract the characters from the String, effectively
sparing one "new char[]" and one "System.arraycopy()"
per process() call. I expect this to improve
throughput for templates that contain many and/or
large text blocks. The change is completely backward
compatible, as the interface of TextBlockInstruction
has not changed, only a private field's type and the
setText() method internals have changed.

Discussion

  • Attila Szegedi
    Attila Szegedi
    2001-06-06

    Better-performing TextBlockInstruction

     
  • Attila Szegedi
    Attila Szegedi
    2001-06-10

    Logged In: YES
    user_id=52489

    It is faster because Writer.write(String) (at least in Sun
    implementation) delegates to Writer.write(char[]) after it
    has:
    - allocated a char[] buffer to hold the String's characters
    (unless the String is less than 1024 characters, in which
    case it uses a cached char[] buffer)
    - called String.getChars() to extract the characters into
    the buffer.

    By calling directly into Writer.write(char[]) we spare the
    time and memory required to allocate the char[] buffer and
    perform a System.arraycopy inside String.getChars() on
    every template evaluation.

    While I admit this is not a BIG optimization, it still
    makes sense and comes without a tradeoff.

    Attila.

     
  • Nicholas Cull
    Nicholas Cull
    2001-06-11

    Logged In: YES
    user_id=137221

    I'll check this patch in hopefully in the next few days. It
    will make 1.6 final. I'll also use the same technique for
    the NoParseInstruction class.

    I'm curious: why is this way faster?

    Nicholas.

     
  • Nicholas Cull
    Nicholas Cull
    2001-06-11

    • assigned_to: nobody --> run2000
     
  • Nicholas Cull
    Nicholas Cull
    2001-06-12

    • status: open --> closed-accepted
     
  • Nicholas Cull
    Nicholas Cull
    2001-06-12

    Logged In: YES
    user_id=137221

    Patch checked in 12 June 2001, for FreeMarker 1.6 final.