Skip to content

Commit a23afcd

Browse files
WizKidfacebook-github-bot
authored andcommitted
Replace LowStringPtrOrId with LazyStringData
Summary: - LowStringPtrOrId took advantage of the 3 low bits all always being 0. - With PackedPtr that will not be true. - But these are roughly never used (only used from Reflection). - So just lookupLitstrId every time we get them. Which is very cheap. Reviewed By: jano Differential Revision: D76876470 fbshipit-source-id: ded0db0556530af09d6ce2419ab1013dc4576f1b
1 parent 8a3ab3d commit a23afcd

16 files changed

Lines changed: 106 additions & 192 deletions

hphp/hhbbc/emit.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ EmitBcInfo emit_bytecode(EmitUnitState& euState, UnitEmitter& ue, FuncEmitter& f
678678
return ret;
679679
}
680680

681-
void emit_locals_and_params(FuncEmitter& fe, const php::Func& func,
681+
void emit_locals_and_params(UnitEmitter& ue, FuncEmitter& fe, const php::Func& func,
682682
const EmitBcInfo& info) {
683683
Id id = 0;
684684
for (auto const& loc : func.locals) {
@@ -690,8 +690,8 @@ void emit_locals_and_params(FuncEmitter& fe, const php::Func& func,
690690
Func::ParamInfo pinfo;
691691
pinfo.defaultValue = param.defaultValue;
692692
pinfo.typeConstraints = param.typeConstraints;
693-
pinfo.userType = param.userTypeConstraint;
694-
pinfo.phpCode = param.phpCode;
693+
pinfo.userType = ue.mergeLitstr(param.userTypeConstraint);
694+
pinfo.phpCode = ue.mergeLitstr(param.phpCode);
695695
pinfo.userAttributes = param.userAttributes;
696696
if (param.inout) pinfo.setFlag(Func::ParamInfo::Flags::InOut);
697697
if (param.outOnly) pinfo.setFlag(Func::ParamInfo::Flags::OutOnly);
@@ -957,17 +957,17 @@ void emit_ehent_tree(FuncEmitter& fe, const php::WideFunc& func,
957957
fe.setEHTabIsSorted();
958958
}
959959

960-
void emit_finish_func(EmitUnitState& state, FuncEmitter& fe,
960+
void emit_finish_func(EmitUnitState& state, UnitEmitter& ue, FuncEmitter& fe,
961961
php::WideFunc& wf, const EmitBcInfo& info) {
962962
auto const& func = *wf;
963963
if (info.containsCalls) fe.containsCalls = true;
964964

965-
emit_locals_and_params(fe, func, info);
965+
emit_locals_and_params(ue, fe, func, info);
966966
emit_ehent_tree(fe, wf, info);
967967
wf.blocks().clear();
968968

969969
fe.userAttributes = func.userAttributes;
970-
fe.retUserType = func.returnUserType;
970+
fe.retUserType = ue.mergeLitstr(func.returnUserType);
971971
fe.retTypeConstraints = func.retTypeConstraints;
972972
fe.typeParamNames = std::vector<LowStringPtr>(
973973
func.typeParamNames.begin(),
@@ -1056,7 +1056,7 @@ void emit_func(EmitUnitState& state, UnitEmitter& ue,
10561056
emit_init_func(ue, fe, f);
10571057
auto func = php::WideFunc::mut(&f);
10581058
auto const info = emit_bytecode(state, ue, fe, func);
1059-
emit_finish_func(state, fe, func, info);
1059+
emit_finish_func(state, ue, fe, func, info);
10601060
}
10611061

10621062
void emit_class(EmitUnitState& state, UnitEmitter& ue, PreClassEmitter* pce,

hphp/hhbbc/parse.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -619,10 +619,10 @@ void add_frame_variables(php::Func& func, const FuncEmitter& fe, const UnitEmitt
619619
php::Param {
620620
param.defaultValue,
621621
NoBlockId,
622-
param.userType.ptr(ue),
622+
param.userType.get(ue),
623623
param.typeConstraints,
624624
param.userAttributes,
625-
param.phpCode.ptr(ue),
625+
param.phpCode.get(ue),
626626
param.isInOut(),
627627
param.isOutOnly(),
628628
param.isReadonly(),
@@ -679,7 +679,7 @@ std::unique_ptr<php::Func> parse_func(ParseUnitState& puState,
679679
AttrPersistent);
680680

681681
ret->userAttributes = fe.userAttributes;
682-
ret->returnUserType = fe.retUserType.ptr(ue);
682+
ret->returnUserType = fe.retUserType.get(ue);
683683
ret->retTypeConstraints = fe.retTypeConstraints;
684684
for (auto const& typeParam : fe.typeParamNames) {
685685
ret->typeParamNames.emplace_back(typeParam);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| HipHop for PHP |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.01 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_01.txt |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| license@php.net so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#include "hphp/runtime/base/lazy-string-data.h"
18+
19+
#include "hphp/runtime/base/string-data.h"
20+
#include "hphp/runtime/vm/unit.h"
21+
#include "hphp/runtime/vm/unit-emitter.h"
22+
23+
namespace HPHP {
24+
25+
const StringData* LazyStringData::get(const Unit* unit) const {
26+
if (m_id != kInvalidId) {
27+
return unit->lookupLitstrId(m_id);
28+
}
29+
return nullptr;
30+
}
31+
32+
const StringData* LazyStringData::get(const UnitEmitter& ue) const {
33+
if (m_id != kInvalidId) {
34+
return ue.lookupLitstrId(m_id);
35+
}
36+
return nullptr;
37+
}
38+
39+
}
Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,36 @@
1616

1717
#pragma once
1818

19-
#include "hphp/util/low-ptr.h"
2019
#include "hphp/runtime/base/types.h"
2120

2221
namespace HPHP {
2322

23+
struct StringData;
2424
struct Unit;
2525
struct UnitEmitter;
2626

27-
struct LowStringPtrOrId {
27+
struct LazyStringData {
2828

29-
using storage_type = LowPtr<const StringData>::storage_type;
29+
LazyStringData(): m_id(kInvalidId) {}
30+
LazyStringData(Id id): m_id(id) {}
3031

31-
LowStringPtrOrId(): m_s(0) {}
32-
explicit LowStringPtrOrId(const LowStringPtr str) : HPHP::LowStringPtrOrId(str.get()) {}
33-
explicit LowStringPtrOrId(const StringData* str);
34-
explicit LowStringPtrOrId(Id id);
35-
LowStringPtrOrId(const LowStringPtrOrId& other);
32+
LazyStringData& operator=(Id id) {
33+
m_id = id;
34+
return *this;
35+
}
3636

37-
LowStringPtrOrId& operator=(const StringData* str);
38-
LowStringPtrOrId& operator=(const LowStringPtrOrId& other);
37+
const Id id() const { return m_id; }
3938

40-
Id id() const;
41-
const StringData* rawPtr() const;
42-
const StringData* ptr(const Unit* unit) const;
43-
const StringData* ptr(const UnitEmitter& ue) const;
39+
const StringData* get(const Unit* unit) const;
40+
const StringData* get(const UnitEmitter& ue) const;
4441

45-
private:
46-
mutable std::atomic<storage_type> m_s;
47-
};
42+
template <typename SerDe>
43+
void serde(SerDe& sd) {
44+
sd(m_id);
45+
}
4846

49-
template<>
50-
struct BlobEncoderHelper<LowStringPtrOrId> {
51-
static void serde(BlobEncoder&, const LowStringPtrOrId&);
52-
static void serde(BlobDecoder&, LowStringPtrOrId&);
47+
private:
48+
Id m_id;
5349
};
5450

55-
} // namespace HPHP
51+
}

hphp/runtime/base/low-string-ptr-or-id.cpp

Lines changed: 0 additions & 128 deletions
This file was deleted.

hphp/runtime/ext/reflection/ext_reflection.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ Variant default_arg_from_php_code(const Func::ParamInfo& fpi,
164164

165165
try {
166166
return g_context->getEvaledArg(
167-
fpi.phpCode.ptr(func->unit()),
167+
fpi.phpCode.get(func->unit()),
168168
// We use cls() instead of implCls() because we want the namespace and
169169
// class context for which the closure is scoped, not that of the
170170
// Closure subclass (which, among other things, is always globally
@@ -824,7 +824,7 @@ static Array get_function_param_info(const Func* func) {
824824
: staticEmptyString();
825825

826826
param.set(s_type, make_tv<KindOfPersistentString>(type));
827-
auto userType = fpi.userType.ptr(func->unit());
827+
auto userType = fpi.userType.get(func->unit());
828828
const StringData* typeHint = userType
829829
? userType
830830
: staticEmptyString();
@@ -853,7 +853,7 @@ static Array get_function_param_info(const Func* func) {
853853
param.set(s_type_hint_nullable, make_tv<KindOfBoolean>(false));
854854
}
855855

856-
if (auto phpCode = fpi.phpCode.ptr(func->unit())) {
856+
if (auto phpCode = fpi.phpCode.get(func->unit())) {
857857
Variant v = default_arg_from_php_code(fpi, func, i);
858858
param.set(s_default, v);
859859
param.set(s_defaultText, make_tv<KindOfPersistentString>(phpCode));

hphp/runtime/vm/as.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,7 +2240,7 @@ void parse_parameter_list(AsmState& as) {
22402240
}
22412241

22422242
auto [userType, typeConstraints] = parse_type_info(as);
2243-
param.userType = userType;
2243+
param.userType = as.ue->mergeLitstr(userType);
22442244
param.typeConstraints = TypeIntersectionConstraint(
22452245
std::move(typeConstraints)
22462246
);
@@ -2273,7 +2273,9 @@ void parse_parameter_list(AsmState& as) {
22732273
ch = as.in.getc();
22742274
if (ch == '(') {
22752275
String str = parse_long_string(as);
2276-
parse_default_value(param, makeStaticString(str));
2276+
auto v = makeStaticString(str);
2277+
auto id = as.ue->mergeLitstr(v);
2278+
parse_default_value(param, id, v);
22772279
as.in.expectWs(')');
22782280
as.in.skipWhitespace();
22792281
ch = as.in.getc();
@@ -2389,7 +2391,7 @@ void parse_function(AsmState& as) {
23892391
as.fe = as.ue->newFuncEmitter(sname);
23902392
as.fe->init(line0, line1, attrs, nullptr, as.ue->isSystemLib());
23912393

2392-
as.fe->retUserType = userType;
2394+
as.fe->retUserType = as.ue->mergeLitstr(userType);
23932395
as.fe->retTypeConstraints = TypeIntersectionConstraint(
23942396
std::move(typeConstraints)
23952397
);
@@ -2444,7 +2446,7 @@ void parse_method(AsmState& as) {
24442446
as.pce->addMethod(as.fe);
24452447
as.fe->init(line0, line1, attrs, nullptr, as.ue->isSystemLib());
24462448

2447-
as.fe->retUserType = userType;
2449+
as.fe->retUserType = as.ue->mergeLitstr(userType);
24482450
as.fe->retTypeConstraints = TypeIntersectionConstraint(
24492451
std::move(typeConstraints)
24502452
);
@@ -3312,8 +3314,8 @@ std::unique_ptr<UnitEmitter> assemble_string(
33123314
return ue;
33133315
}
33143316

3315-
void parse_default_value(Func::ParamInfo& param, const StringData* str) {
3316-
param.phpCode = str;
3317+
void parse_default_value(Func::ParamInfo& param, Id id, const StringData* str) {
3318+
param.phpCode = id;
33173319
TypedValue tv;
33183320
tvWriteUninit(tv);
33193321
if (str->size() == 4) {

hphp/runtime/vm/as.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,12 @@ void fixup_default_values(T& state, FuncEmitter* fe) {
171171
VariableSerializer vs(VariableSerializer::Type::PHPOutput);
172172
auto str = vs.serialize(tvAsCVarRef(&dv), true);
173173
pi.defaultValue = dv;
174-
pi.phpCode = makeStaticString(str.get());
174+
pi.phpCode = state.ue->mergeLitstr(makeStaticString(str.get()));
175175
}
176176
}
177177
}
178178

179-
void parse_default_value(Func::ParamInfo& param, const StringData* str);
179+
void parse_default_value(Func::ParamInfo& param, Id id, const StringData* str);
180180

181181
// Sets output on success; throws on failure.
182182
void ParseRepoAuthType(folly::StringPiece input, RepoAuthType& output);

0 commit comments

Comments
 (0)