From f6723e5d4cc312c1e1ba7c020ad550119ee10e3e Mon Sep 17 00:00:00 2001 From: Jack Gerrits Date: Mon, 11 Feb 2019 18:03:06 -0500 Subject: [PATCH 1/4] Small fixes to json parser --- vowpalwabbit/CMakeLists.txt | 2 +- vowpalwabbit/parse_example_json.cc | 5 --- vowpalwabbit/parse_example_json.h | 54 ++++++++++++------------------ vowpalwabbit/vw_core.vcxproj | 43 ++++++++++++------------ 4 files changed, 44 insertions(+), 60 deletions(-) delete mode 100644 vowpalwabbit/parse_example_json.cc diff --git a/vowpalwabbit/CMakeLists.txt b/vowpalwabbit/CMakeLists.txt index 06f580bbcf4..b262041754a 100644 --- a/vowpalwabbit/CMakeLists.txt +++ b/vowpalwabbit/CMakeLists.txt @@ -49,7 +49,7 @@ set(vw_all_sources cbify.cc explore_eval.cc topk.cc stagewise_poly.cc log_multi.cc recall_tree.cc active.cc active_cover.cc cs_active.cc kernel_svm.cc best_constant.cc ftrl.cc svrg.cc lrqfa.cc interact.cc comp_io.cc interactions.cc vw_validate.cc audit_regressor.cc gen_cs_example.cc cb_explore.cc - action_score.cc cb_explore_adf.cc OjaNewton.cc parse_example_json.cc baseline.cc classweight.cc + action_score.cc cb_explore_adf.cc OjaNewton.cc baseline.cc classweight.cc vw_exception.cc no_label.cc ) diff --git a/vowpalwabbit/parse_example_json.cc b/vowpalwabbit/parse_example_json.cc deleted file mode 100644 index 82757fc89d1..00000000000 --- a/vowpalwabbit/parse_example_json.cc +++ /dev/null @@ -1,5 +0,0 @@ -/* -Copyright (c) by respective owners including Yahoo!, Microsoft, and -individual contributors. All rights reserved. Released under a BSD -license as described in the file LICENSE. -*/ diff --git a/vowpalwabbit/parse_example_json.h b/vowpalwabbit/parse_example_json.h index 00542132316..7e8bd9a39a2 100644 --- a/vowpalwabbit/parse_example_json.h +++ b/vowpalwabbit/parse_example_json.h @@ -29,7 +29,9 @@ license as described in the file LICENSE. #include "cb.h" #include "best_constant.h" -#include + +#include +#include // portability fun #ifndef _WIN32 @@ -848,16 +850,10 @@ class BoolToBoolState : public BaseState struct DecisionServiceInteraction { std::string eventId; - std::vector actions; - std::vector probabilities; - - float probabilityOfDrop; - + float probabilityOfDrop = 0.f; bool skipLearn{false}; - - DecisionServiceInteraction() : probabilityOfDrop(0.f) {} }; template @@ -937,8 +933,11 @@ class DecisionServiceState : public BaseState template struct Context { +private: + std::unique_ptr error_ptr; + +public: vw* all; - std::stringstream* error_ptr; // last "": encountered const char* key; @@ -948,7 +947,7 @@ struct Context BaseState* previous_state; // the path of namespaces - v_array> namespace_path; + std::vector> namespace_path; v_array* examples; example* ex; @@ -980,18 +979,10 @@ struct Context BaseState* root_state; - Context() : error_ptr(nullptr) + Context() { - namespace_path = v_init>(); - current_state = root_state = &default_state; - } - - ~Context() - { - namespace_path.delete_v(); - - if (error_ptr) - delete error_ptr; + current_state = &default_state; + root_state = &default_state; } void init(vw* pall) @@ -1001,13 +992,12 @@ struct Context key_length = 1; previous_state = nullptr; label_object_state.init(pall); - error_ptr = nullptr; } std::stringstream& error() { if (!error_ptr) - error_ptr = new std::stringstream; + error_ptr.reset(new std::stringstream{}); return *error_ptr; } @@ -1038,19 +1028,19 @@ struct Context if (ns.feature_count > 0) { auto feature_group = ns.feature_group; - // avoid duplicate insertion - for (unsigned char ns_char : ex->indices) - if (ns_char == feature_group) - goto done; - - ex->indices.push_back(feature_group); + // Do not insert feature_group if it already exists. + if(std::find(ex->indices.begin(), ex->indices.end(), feature_group) == ex->indices.end()) + { + ex->indices.push_back(feature_group); + } } - done: - return namespace_path.pop().return_state; + auto return_state = namespace_path.back().return_state; + namespace_path.pop_back(); + return return_state; } - Namespace& CurrentNamespace() { return *(namespace_path._end - 1); } + Namespace& CurrentNamespace() { return namespace_path.back(); } bool TransitionState(BaseState* next_state) { diff --git a/vowpalwabbit/vw_core.vcxproj b/vowpalwabbit/vw_core.vcxproj index 7e7515c689e..cdfaff0ed3e 100644 --- a/vowpalwabbit/vw_core.vcxproj +++ b/vowpalwabbit/vw_core.vcxproj @@ -67,7 +67,7 @@ false - @@ -125,32 +125,32 @@ win32\make_config_h.exe - $(SolutionDir)out\target\$(Configuration)\$(PlatformShortName)\ $(SolutionDir)out\int\$(Configuration)\$(PlatformShortName)\$(ProjectName)\ - @@ -296,7 +296,6 @@ - From 65a2b3bcacf393cc3cf8d4588dad3cd0c34fe21a Mon Sep 17 00:00:00 2001 From: Jack Gerrits Date: Mon, 11 Feb 2019 21:10:29 -0500 Subject: [PATCH 2/4] Use std::find --- vowpalwabbit/parser.cc | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/vowpalwabbit/parser.cc b/vowpalwabbit/parser.cc index 93a8af2a4ab..988e6deb231 100644 --- a/vowpalwabbit/parser.cc +++ b/vowpalwabbit/parser.cc @@ -142,14 +142,6 @@ uint32_t cache_numbits(io_buf* buf, int filepointer) return cache_numbits; } -bool member(v_array ids, int id) -{ - for (size_t i = 0; i < ids.size(); i++) - if (ids[i] == id) - return true; - return false; -} - void reset_source(vw& all, size_t numbits) { io_buf* input = all.p->input; @@ -171,7 +163,10 @@ void reset_source(vw& all, size_t numbits) else { int fd = input->files.pop(); - if (!member(all.final_prediction_sink, (size_t)fd)) + auto& fps = all.final_prediction_sink; + + // If the current popped file is not in the list of final predictions sinks, close it. + if(std::find(fps.begin(), fps.end(), fd) == fps.end()) io_buf::close_file_or_socket(fd); } input->open_file(all.p->output->finalname.begin(), all.stdin_off, io_buf::READ); // pushing is merged into From d599e90278c5c6782242abfd2abdc5f5940ba40d Mon Sep 17 00:00:00 2001 From: Jack Gerrits Date: Mon, 11 Feb 2019 21:11:47 -0500 Subject: [PATCH 3/4] remove unused function --- vowpalwabbit/ect.cc | 8 -------- 1 file changed, 8 deletions(-) diff --git a/vowpalwabbit/ect.cc b/vowpalwabbit/ect.cc index ba2956148e3..564b9d6ef46 100644 --- a/vowpalwabbit/ect.cc +++ b/vowpalwabbit/ect.cc @@ -217,14 +217,6 @@ uint32_t ect_predict(ect& e, single_learner& base, example& ec) return id + 1; } -bool member(size_t t, v_array ar) -{ - for (size_t i = 0; i < ar.size(); i++) - if (ar[i] == t) - return true; - return false; -} - void ect_train(ect& e, single_learner& base, example& ec) { if (e.k == 1) // nothing to do From f2b9cec317d11e6363ca45611793d81da45a2be0 Mon Sep 17 00:00:00 2001 From: Jack Gerrits Date: Fri, 15 Feb 2019 09:54:11 -0500 Subject: [PATCH 4/4] Make file sync variable const --- vowpalwabbit/parser.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vowpalwabbit/parser.cc b/vowpalwabbit/parser.cc index 988e6deb231..8036238dc7b 100644 --- a/vowpalwabbit/parser.cc +++ b/vowpalwabbit/parser.cc @@ -163,10 +163,10 @@ void reset_source(vw& all, size_t numbits) else { int fd = input->files.pop(); - auto& fps = all.final_prediction_sink; + const auto& fps = all.final_prediction_sink; // If the current popped file is not in the list of final predictions sinks, close it. - if(std::find(fps.begin(), fps.end(), fd) == fps.end()) + if(std::find(fps.cbegin(), fps.cend(), fd) == fps.cend()) io_buf::close_file_or_socket(fd); } input->open_file(all.p->output->finalname.begin(), all.stdin_off, io_buf::READ); // pushing is merged into