Skip to content

Commit

Permalink
use hashmap facility in the template cache to speed up Template::Data…
Browse files Browse the repository at this point in the history
…::set
  • Loading branch information
Oneplus committed Sep 15, 2014
1 parent ab31d56 commit 78d7b85
Show file tree
Hide file tree
Showing 6 changed files with 432 additions and 371 deletions.
13 changes: 0 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,6 @@ compiler:
before_install:
- sudo apt-get install gfortran
- sudo apt-get install cmake
env:
global:
- secure: "VdY9DQK8PdZ5cBpn9qG+8KqyC7BFuYPNwU4f5n19nto62V6ifU5XOLBkxCF36bSF8C4Nf0y0uDdj4gqMnL7OqgwFjucBRQLOuk/10uuy3azEjGzTxePgXlYw15XmMEpWy3hvxEfFqvonJ0g9+fZjKeEmbASVixIbWpYN/pIy2jQ="

addons:
coverity_scan:
project:
name: "HIT-SCIR/ltp"
description: "Language Technology Platform"
notification_email: [email protected]
build_command_prepend: "./configure"
build_command: "make -j4"
branch_pattern: master

script:
- ./configure
Expand Down
8 changes: 4 additions & 4 deletions src/postagger/extractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ int Extractor::extract1o(Instance * inst, int idx, std::vector< StringVec > & ca
int length = inst->forms[idx].size(); length = (length < 5 ? length : 5);
data.set( "len", strutils::to_str(length));

data.set( "ch-1,n", (idx-1 < 0 ? BOC : inst->chars[idx-1][inst->chars[idx-1].size()-1]));
data.set( "ch-0,0", inst->chars[idx][0] );
data.set( "ch-0,n", inst->chars[idx][inst->chars[idx].size()-1]);
data.set( "ch+1,0", (idx+1 >= len ? EOC : inst->chars[idx+1][0]));
// data.set( "ch-1,n", (idx-1 < 0 ? BOC : inst->chars[idx-1][inst->chars[idx-1].size()-1]));
// data.set( "ch-0,0", inst->chars[idx][0] );
// data.set( "ch-0,n", inst->chars[idx][inst->chars[idx].size()-1]);
// data.set( "ch+1,0", (idx+1 >= len ? EOC : inst->chars[idx+1][0]));

string feat;
feat.reserve(1024);
Expand Down
70 changes: 56 additions & 14 deletions src/unittest/utils_template_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,40 @@ using namespace std;
using namespace ltp::utility;

TEST(template_unittest, test_template_basic) {
Template templates("1={test}");
Template T("1={test}");
Template::Data data;
data.set("test","basic");
string feat;
templates.render(data,feat);
T.render(data,feat);
EXPECT_STREQ("1=basic",feat.c_str());
}

TEST(template_unittest, test_template_space) {
Template templates("1={test}");
Template T("1={test}");
Template::Data data;
data.set("test"," ");
string feat;
templates.render(data,feat);
T.render(data,feat);
EXPECT_STREQ("1= ",feat.c_str());
}


TEST(template_unittest, test_template_empty) {
Template templates("1={test}");
Template T("1={test}");
Template::Data data;
data.set("test","");
string feat;
templates.render(data,feat);
T.render(data,feat);
EXPECT_STREQ("1=",feat.c_str());
}

TEST(template_DeathTest, test_template_null) {
Template templates("1={test}");
TEST(template_unittest, test_template_concate) {
/*Template T("1={slot1}{slot2}");
Template::Data data;
// Till now (2014-04-30) this usage will result in a core
// dump in LTP
char * ptr = NULL;
data.set("test", ptr);
data.set("slot1", "-");
data.set("slot2", "+");
string feat;
ASSERT_DEATH(templates.render(data, feat), "");
T.render(data, feat);
EXPECT_STREQ("1=-+", feat.c_str());*/
}

TEST(template_unittest, test_template_chinese) {
Expand All @@ -64,6 +62,50 @@ TEST(template_unittest, test_template_duplicate) {
EXPECT_STREQ("template-with-two-{brackets}", feat.c_str());
}

TEST(template_unittest, test_template_efficiency) {
std::vector<Template *> repos;
repos.push_back(new Template("1={w0}"));
repos.push_back(new Template("2={p0}"));
repos.push_back(new Template("3={w0}-{p0}"));
repos.push_back(new Template("4={w1}"));
repos.push_back(new Template("5={p1}"));
repos.push_back(new Template("6={w1}-{p1}"));

string payload;
payload.reserve(128);
long start_time = clock();
int kNumRepeats = 1024 * 1024;
int kNumTemplates = repos.size();

for (int t = 0; t < 1024 * 1024; ++ t) {
Template::Data data;
data.set("w0", "am");
data.set("p0", "v");
data.set("w1", "I");
data.set("p1", "r");
for (int i = 0; i < repos.size(); ++ i) {
Template* T = repos[i];
T->render(data, payload);
}
}

long throughput_per_millisecond = ((kNumRepeats * kNumTemplates)
/ ((clock() -start_time) / 1000));
std::cout << throughput_per_millisecond << std::endl;
}


TEST(template_DeathTest, test_template_null) {
Template T("1={test}");
Template::Data data;
// Till now (2014-04-30) this usage will result in a core
// dump in LTP
char* ptr = NULL;
data.set("test", ptr);
string feat;
ASSERT_DEATH(T.render(data, feat), "");
}

int main(int argc, char ** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
Expand Down
Loading

0 comments on commit 78d7b85

Please sign in to comment.