Showing posts with label performance. Show all posts
Showing posts with label performance. Show all posts

Wednesday, May 4, 2016

Read from file: Length-prefixed protocol buffers

File format

In short it’s binary encoded protobuf message prefixed with the size. For longer description please check Length-prefix framing for protocol buffers.

In most languages the file is read as stream. If you need to open and parse a file which holds lenght-prefixed protobuf messages in Python, you read internet and you find a code which read the whole file. It’s huge bottleneck. The hit in a performance between reading whole file then parsing byte after byte and using BufferedReader was in 1000x. Thus enjoy my little piece of code:

def ReadItm(fname, constructor, size_limit = 0):
    ''' Reads and parses a length prefixed protobuf messages from file. 
        The file MUST not be corrupted. The parsing is equivalent to parseDelimitedFrom.
    '''
    f = None
    if fname.endswith('.gzip'):
        f = gzip.open(fname, 'rb')
    else:
        f = open(fname, 'rb')
    reader = BufferedReader(f)
    bytes_read = 0
    while size_limit<=0 or bytes_read<size_limit:
        buffer = reader.peek(10)
        if len(buffer) == 0:
            break
        (size, position) = decoder._DecodeVarint(buffer, 0)
        reader.read(position)
        itm = constructor()
        itm.ParseFromString(reader.read(size))
        bytes_read = bytes_read + position + size
        yield itm
    f.close()

Tuesday, December 20, 2011

Page Speed

Page Speed is an open-source project started at Google to help developers optimize their web pages by applying web performance best practices. Page Speed started as an open-source browser extension, and is now deployed in third-party products such as Webpagetest.orgShow Slow and Google Webmaster Tools.
So if you want to try Page Speed go: http://code.google.com/intl/pl/speed/page-speed/

Wednesday, September 21, 2011

C++ Profiling

I am writing program for ai-contest right now and noticed that for some reasons and initial conditions my program is working slower and slower. I don't want to change algorithms or design as it is quite well written. So I decided to find bottlenecks of my bot. Here is how I started.

List of C++ profilers:
Both of them are open source. I especially liked the second one.

To be continued.