Char []到Byte []以便在Web中优化输出(java) [英] Char[] to Byte[] for output optimize in web (java)

查看:59
本文介绍了Char []到Byte []以便在Web中优化输出(java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在infoq的经验分享演示中找到。它声称,如果在Servlet中将String转换为byte [],则将提高QPS(每秒查询数?)。
该代码示例显示了以下比较:

I just find in an experence share presentation from infoq. It claims that if you convert the String to byte[] in servlet, it will increase the QPS (Queries per Second?). The code example shows the comparison:

private static String content = "…94k…";
protected doGet(…){
response.getWrite().print(content);
}



之后



After

private static String content = "…94k…";
Private static byte[] bytes = content.getBytes();
protected doGet(…){
response.getOutputStream().write(bytes);
}



之前的结果




  • 页面大小(K)94

  • 最大QPS 1800


    • 页面大小(K)94

    • 最大QPS 3500

    有人可以解释为什么对其进行优化吗?我相信它是真的。

    Can anyone explain why it was optimized? I trust it to be true.

    更新

    误导。我需要解释一下,原始演示文稿仅以此为例。他们实际上通过这种方式重构了速度引擎。

    In case I cause any misleading. I need explain that the original presentation only uses this as an example. They actually refactor the velocity engine by this way. BUt this source code is a bit long.

    实际上,在演示文稿中并没有暗示它们如何详细执行。但是我找到了一些线索。

    Actually in the presentation didn't imply how they do it in detail. But I found some lead.

    在ASTText.java中,他们缓存了byte [] ctext而不是char [] ctext,这提高了性能很多!!

    In ASTText.java, they cached the byte[] ctext instead of char[] ctext , which boosts the performance a lot~!

    就像上面的方法一样。这很有道理,对吧?

    Just like the way above. It makes a lot of sense,right?

    (但是肯定他们也应该重构Node接口。Writer不能写byte []。这意味着要使用OutputStream!)

    (BUT definitely they should also refactor the Node interface. Writer cannot write byte[]. Which means using OutputStream instead!)

    根据Perception的建议,Write最终会委托给StreamEncoder。并且StreamEncoder写入将首先将char []更改为byte []。然后将其委托给OutputSteam进行实际写入。您可以轻松地参考源代码并对其进行证明。
    考虑到每次显示页面都将调用render方法,因此可节省大量成本。

    As Perception adviced actually a Write finally delegate to a StreamEncoder. And StreamEncoder write will first change char[] into byte[]. And then delegate it to the OutputSteam to do the real write. You can easily refer to the source code and prove it. Considering render method will be called each time for showing the page, the saving of cost will be considerable.

    StreamEncoder.class

     public class ASTText extends SimpleNode {
                private char[] ctext;
            /**
             * @param id
             */
            public ASTText(int id) {
                super (id);
            }
    
            /**
             * @param p
             * @param id
             */
            public ASTText(Parser p, int id) {
                super (p, id);
            }
    
            /**
             * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.node.ParserVisitor, java.lang.Object)
             */
            public Object jjtAccept(ParserVisitor visitor, Object data) {
                return visitor.visit(this , data);
            }
    
        /**
         * @see org.apache.velocity.runtime.parser.node.SimpleNode#init(org.apache.velocity.context.InternalContextAdapter, java.lang.Object)
         */
        public Object init(InternalContextAdapter context, Object data)
                throws TemplateInitException {
            Token t = getFirstToken();
    
            String text = NodeUtils.tokenLiteral(t);
    
            ctext = text.toCharArray();
    
            return data;
        }
    
        /**
         * @see org.apache.velocity.runtime.parser.node.SimpleNode#render(org.apache.velocity.context.InternalContextAdapter, java.io.Writer)
         */
        public boolean render(InternalContextAdapter context, Writer writer)
                throws IOException {
            if (context.getAllowRendering()) {
                writer.write(ctext);
            }
            return true;
        }
    }
    


    推荐答案

    由于您没有调用相同的输出方法,因此在第二个示例中,避免了在将String写入输出流之前将String转换为字节的开销。但是,这些情况不是很现实,因为Web应用程序的动态性质无法将所有数据模型预先转换为字节流。 而且,现在没有严肃的体系结构可以直接写入HTTP输出流。

    Apart from the fact that you aren't calling the same output methods, in your second example you avoid the overhead of converting the String to bytes before writing it to the output stream. These scenarios are not very realistic though, the dynamic nature of web applications precludes pre-converting all your data models into byte streams. And, there are no serious architectures out there now where you will be writing directly to the HTTP output stream like this.

    这篇关于Char []到Byte []以便在Web中优化输出(java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆