src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/AstSerializer.java
author hannesw
Wed, 21 Mar 2018 16:55:34 +0100
changeset 49275 c639a6b33c5c
parent 47216 71c04702a3d5
permissions -rw-r--r--
8199869: Missing copyright headers in nashorn source code Reviewed-by: sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27206
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
     1
/*
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
     2
 * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
     4
 *
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    10
 *
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    15
 * accompanied this code).
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    16
 *
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    20
 *
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    23
 * questions.
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    24
 */
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 27821
diff changeset
    25
package jdk.nashorn.internal.runtime;
27206
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    26
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    27
import java.io.ByteArrayOutputStream;
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    28
import java.io.IOException;
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    29
import java.io.ObjectOutputStream;
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    30
import java.util.zip.Deflater;
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    31
import java.util.zip.DeflaterOutputStream;
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    32
import jdk.nashorn.internal.ir.FunctionNode;
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    33
import jdk.nashorn.internal.runtime.options.Options;
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    34
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    35
/**
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    36
 * This static utility class performs serialization of FunctionNode ASTs to a byte array.
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    37
 * The format is a standard Java serialization stream, deflated.
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    38
 */
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    39
final class AstSerializer {
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    40
    // Experimentally, we concluded that compression level 4 gives a good tradeoff between serialization speed
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    41
    // and size.
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    42
    private static final int COMPRESSION_LEVEL = Options.getIntProperty("nashorn.serialize.compression", 4);
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    43
    static byte[] serialize(final FunctionNode fn) {
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    44
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
27821
87ac5547bbf3 8065769: OOM on Window/Solaris in test compile-octane-splitter.js
hannesw
parents: 27206
diff changeset
    45
        final Deflater deflater = new Deflater(COMPRESSION_LEVEL);
87ac5547bbf3 8065769: OOM on Window/Solaris in test compile-octane-splitter.js
hannesw
parents: 27206
diff changeset
    46
        try (final ObjectOutputStream oout = new ObjectOutputStream(new DeflaterOutputStream(out, deflater))) {
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 27821
diff changeset
    47
            oout.writeObject(fn);
27206
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    48
        } catch (final IOException e) {
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    49
            throw new AssertionError("Unexpected exception serializing function", e);
27821
87ac5547bbf3 8065769: OOM on Window/Solaris in test compile-octane-splitter.js
hannesw
parents: 27206
diff changeset
    50
        } finally {
87ac5547bbf3 8065769: OOM on Window/Solaris in test compile-octane-splitter.js
hannesw
parents: 27206
diff changeset
    51
            deflater.end();
27206
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    52
        }
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    53
        return out.toByteArray();
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    54
    }
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents:
diff changeset
    55
}