Project

General

Profile

Feature #445

Add new Irccd.File.prototype.lines

Added by David Demelier over 3 years ago. Updated over 3 years ago.

Status:
Closed
Priority:
Normal
Category:
libirccd-js
Target version:
Start date:
03/30/2016
Due date:
% Done:

0%

Estimated time:
3.00 h (Total: 6.00 h)
Spent time:
MFD after:
MFD revision:
Branch:
default
Bookmark:
@
Platform:

Description

The current Irccd.File.prototype.readline is slow due to its use of std::fgetc.

This prototype of code seems to work better:

void File::lines(duk::ContextPtr ctx)
{
    duk::push(ctx, duk::Array{});

    std::string buffer;
    unsigned total = 0;

    /* Remove trailing \r for CRLF line style */
    auto clear = [] (std::string input) -> std::string {
        if (input.length() > 0 && input.back() == '\r')
            input.pop_back();

        return input;
    };

    while (!std::feof(m_stream)) {
        auto pos = buffer.find('\n');

        if (pos != std::string::npos) {
            duk::putProperty(ctx, -1, total++, clear(buffer.substr(0, pos)));
            buffer.erase(0, pos + 1);
        }

        char tmp[256];
        if (std::fgets(tmp, sizeof (tmp), m_stream) == nullptr && std::ferror(m_stream))
            throw std::runtime_error("failure...");

        buffer += tmp;
    }

    // Missing '\n' in end of file
    if (!buffer.empty())
        duk::putProperty(ctx, -1, total++, clear(buffer));
}

In JavaScript, it takes about 290ms in debug in constrast with 1500ms with a readline loop. In release it is reduced to 200ms. The file has 330000 entries.


Subtasks

Change request #448: Plugin hangman: improve hangman performancesNewDavid Demelier

Actions

History

#1

Updated by David Demelier over 3 years ago

I think it's even possible to make shorter by looping over the std::fgets result.

#2

Updated by David Demelier over 3 years ago

  • Status changed from New to Resolved
#3

Updated by David Demelier over 3 years ago

  • Status changed from Resolved to Closed

Also available in: Atom PDF