Ticket #2496: videoPerformance_java.patch

File videoPerformance_java.patch, 10.5 KB (added by boyanl, 15 years ago)
  • modules/org.sophie2.base.commons/src/main/java/org/sophie2/base/commons/util/bindata/RawBinData.java

    ### Eclipse Workspace Patch 1.0
    #P sophie
     
    1010 
    1111import org.sophie2.base.commons.util.Base64; 
    1212import org.sophie2.core.prolib.annot.Immutable; 
     13import org.sophie2.base.commons.util.bindata.FastByteArrayOutputStream; 
    1314 
    1415/** 
    1516 * Immutable binary data. 
     
    115116         */ 
    116117        public RawBinData(InputStream input, int size) throws IOException { 
    117118                // should read exactly size bytes 
    118                 ByteArrayOutputStream os = new ByteArrayOutputStream(); 
     119                FastByteArrayOutputStream os = new FastByteArrayOutputStream(size); 
    119120                transport(input, os, size); 
    120121                this.data = os.toByteArray(); 
    121122        } 
     
    137138 
    138139        @Override 
    139140        public byte[] getBytes() { 
    140                 byte[] copyOfData = new byte[this.data.length]; 
    141                 System.arraycopy(this.data, 0, copyOfData, 0, this.data.length); 
    142                 return copyOfData; 
     141                return this.data; 
    143142        } 
    144143 
    145144 
  • modules/org.sophie2.main.media.natlib/src/main/java/org/sophie2/main/media/natlib/decoder/NativeMediaHandler.java

     
    106106                                // If there are some requests for AudioChunks... 
    107107                                if (!reqQueue.isEmpty()) { 
    108108                                        lastRequest = reqQueue.poll(); 
    109                                         SophieLog.debug("Fetching request for position " + lastRequest); 
     109                                        //SophieLog.debug("Fetching request for position " + lastRequest); 
    110110 
    111111                                        // If the requested chunk is not in the cache retrieve it 
    112112                                        // from the natives and put it in the cache. 
     
    135135                                         
    136136                                        //Prefetch: 
    137137                                        if (nextNeeded != -1) { 
    138                                                 SophieLog.debug("Prefetching: " + nextNeeded); 
     138                                                //SophieLog.debug("Prefetching: " + nextNeeded); 
    139139                                                this.audioCache.put(nextNeeded, getNativeAudio(nextNeeded)); 
    140140                                        } else { 
    141141                                                // If there is no request yet just sleep for a while... 
     
    249249                this.lastFrame = getVideoBridge().getFrame((int) millis % getModLen()); 
    250250                assert this.lastFrame != null; 
    251251                this.lastFrameMillis = millis; 
     252                 
    252253                return this.lastFrame; 
    253254        } 
    254255 
  • modules/org.sophie2.base.commons/src/main/java/org/sophie2/base/commons/util/BinReader.java

     
    2323public final class BinReader { 
    2424        private final BinData data; 
    2525        private int pos; 
     26        private byte[] bytes; 
    2627 
    2728        /** 
    2829         * Constructor 
     
    3132        public BinReader(BinData data) { 
    3233                this.data = data; 
    3334                this.pos = 0; 
     35                 
     36                /* 
     37                 * Small optimization - instead of performing a virtual call to read each byte, 
     38                 * fetch them all at once at construction and then simply index 
     39                 * We force the data to be kept in memory but RawBinData is stored in main memory anyways 
     40                 */ 
     41                this.bytes = data.getBytes(); 
    3442        } 
    3543 
    3644        /** 
     
    9199        private byte readByte() { 
    92100                assert this.pos < this.data.getSize() : 
    93101                        "pos=" + this.pos + " ,size=" + this.data.getSize(); 
    94                 return this.data.getByte(this.pos++); 
     102                 
     103                return this.bytes[this.pos++]; 
    95104        } 
    96105 
    97106        private byte[] readArray(int len) { 
    98107                // TODO may be written faster if needed 
    99108                byte[] res = new byte[len]; 
    100                 for (int i = 0; i < len; ++i) { 
    101                         res[i] = readByte(); 
    102                 } 
     109                //copy bytes [pos, pos+len) 
     110                System.arraycopy(this.bytes, this.pos, res, 0, len); 
     111                this.pos += len; 
     112                 
    103113                return res; 
    104114        } 
    105115 
  • modules/org.sophie2.base.commons/src/main/java/org/sophie2/base/commons/util/BinWriter.java

     
    3737                } 
    3838                this.data[this.size++] = b; 
    3939        } 
     40         
     41        private void addBytes(byte[] b) { 
     42                if (this.size + b.length > this.data.length) { 
     43                        /* 
     44                         *  We want to set the new size to the closest power of 2, which is >= this.size + this.length 
     45                         *  Instead of looping through the powers and checking we do it via some bitwise operations 
     46                         */ 
     47                        int newLen = this.size + b.length - 1; 
     48                        newLen |= newLen >> 1; 
     49                        newLen |= newLen >> 2; 
     50                        newLen |= newLen >> 4; 
     51                        newLen |= newLen >> 8; 
     52                        newLen |= newLen >> 16; 
     53                        ++newLen; 
     54                         
     55                        byte[] oldData = this.data; 
     56                        byte[] newData = new byte[newLen]; 
     57                        System.arraycopy(oldData, 0, newData, 0, this.size); 
     58                        this.data = newData; 
     59                } 
     60                System.arraycopy (b, 0, this.data, this.size, b.length); 
     61                this.size += b.length; 
     62        } 
    4063 
    4164        /** 
    4265         * Writes an encoded int. 
     
    5679        public void writeBinData(BinData value) { 
    5780                writeInt(value.getSize()); 
    5881                byte[] bytes = value.getBytes();  
    59                 for (int i = 0; i < value.getSize(); ++i) { 
    60                         addByte(bytes[i]); 
    61                 } 
     82                addBytes (bytes); 
    6283        } 
    6384 
    6485        /** 
  • modules/org.sophie2.base.commons/src/main/java/org/sophie2/base/commons/util/bindata/FastByteArrayOutputStream.java

     
     1package org.sophie2.base.commons.util.bindata; 
     2 
     3import java.io.OutputStream; 
     4 
     5/** 
     6 * ByteArrayOutputStream implementation that doesn't synchronize methods 
     7 * and doesn't copy the data on toByteArray(). 
     8 */ 
     9public class FastByteArrayOutputStream extends OutputStream { 
     10    /** 
     11     * The buffer  
     12     */ 
     13    protected byte[] buf = null; 
     14    /** 
     15     * The size of actual data written in the buffer 
     16     */ 
     17    protected int size = 0; 
     18 
     19    /** 
     20     * Constructs a stream with buffer capacity size 8K 
     21     */ 
     22    public FastByteArrayOutputStream() { 
     23        this(8 * 1024); 
     24    } 
     25 
     26    /** 
     27     * Constructs a stream with the given initial size 
     28     * @param initSize  
     29     *                                  The given initial size 
     30     */ 
     31    public FastByteArrayOutputStream(int initSize) { 
     32        this.size = 0; 
     33        this.buf = new byte[initSize]; 
     34    } 
     35 
     36    /** 
     37     * Ensures that we have a large enough buffer for the given size. 
     38     * Marked final for easier inlining  
     39     */ 
     40    private final void verifyBufferSize(int sz) { 
     41        if (sz > this.buf.length) { 
     42            byte[] old = this.buf; 
     43            this.buf = new byte[Math.max(sz, 2 * this.buf.length )]; 
     44            System.arraycopy(old, 0, this.buf, 0, old.length); 
     45            old = null; 
     46        } 
     47    } 
     48 
     49    /** 
     50     * @return 
     51     *                  Returns the size of the written data. 
     52     */ 
     53    public int getSize() { 
     54        return this.size; 
     55    } 
     56 
     57    /** 
     58     * @return  
     59         *                      Returns the byte array containing the written data. Note that this 
     60         *                      array will almost always be larger than the amount of data actually 
     61         *                      written. Thus you should use the [0, size) interval of the returned array 
     62     */ 
     63    public byte[] toByteArray() { 
     64        return this.buf; 
     65    } 
     66 
     67    @Override 
     68        public final void write(byte b[]) { 
     69        verifyBufferSize(this.size + b.length); 
     70        System.arraycopy(b, 0, this.buf, this.size, b.length); 
     71        this.size += b.length; 
     72    } 
     73 
     74    @Override 
     75        public final void write(byte b[], int off, int len) { 
     76        verifyBufferSize(this.size + len); 
     77        System.arraycopy(b, off, this.buf, this.size, len); 
     78        this.size += len; 
     79    } 
     80 
     81    @Override 
     82        public final void write(int b) { 
     83        verifyBufferSize(this.size + 1); 
     84        this.buf[this.size++] = (byte) b; 
     85    } 
     86 
     87    /** 
     88     * Resets the size counter of the data, effectively writing over the old data when you  
     89     * perform writes. 
     90     */ 
     91    public void reset() { 
     92        this.size = 0; 
     93    } 
     94} 
  • modules/org.sophie2.extra.func.spellcheck/src/test/java/org/sophie2/extra/func/spellcheck/SpellCheckDemoTest.java

     
    327327                                String textStr = modelText.toString(); 
    328328 
    329329                                List<String> misspelled = new LinkedList<String> (); 
    330                                 //SpellCheckUtility.getMisspelledWords (misspelled, null, textStr); 
     330                                SpellCheckUtility.getMisspelledWords (misspelled, null, textStr, null); 
    331331 
    332332                                DefaultListModel model = (DefaultListModel) listMisspelled.getModel(); 
    333333 
  • modules/org.sophie2.base.commons/src/main/java/org/sophie2/base/commons/util/bindata/BinData.java

     
    126126         *             if occurs :) 
    127127         */ 
    128128        public static void transport(InputStream from, OutputStream to) throws IOException { 
    129                 byte[] buffer = new byte[1 << 14]; 
     129                byte[] buffer = new byte[1 << 16]; 
    130130                int read; 
    131131                while ((read = from.read(buffer)) != -1) { 
    132132                        to.write(buffer, 0, read); 
     
    144144         *             if occurs 
    145145         */ 
    146146        public static void transport(Reader reader, Writer writer) throws IOException { 
    147                 char[] buffer = new char[1 << 14]; 
     147                char[] buffer = new char[1 << 16]; 
    148148                int read; 
    149149                while ((read = reader.read(buffer)) != -1) { 
    150150                        writer.write(buffer, 0, read); 
     
    169169         */ 
    170170        protected void transport(InputStream from, OutputStream to, int size) throws IOException { 
    171171                assert size > 0; 
    172                 byte[] buffer = new byte[1 << 14]; 
     172                byte[] buffer = new byte[1 << 16]; 
    173173                int remaining = size; 
    174174                int read; 
    175175                while ((read = from.read(buffer, 0, Math.min(remaining, buffer.length))) != -1