Skip to content

Commit 4b78367

Browse files
committed
just use binding table
1 parent 716e779 commit 4b78367

File tree

10 files changed

+50
-156
lines changed

10 files changed

+50
-156
lines changed

src/ast.c

Lines changed: 22 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#include <stdio.h>
99
#include <string.h>
1010

11-
#include "support/strhash.h"
12-
1311
#ifdef _OS_WINDOWS_
1412
#include <malloc.h>
1513
#endif
@@ -218,55 +216,17 @@ static value_t fl_nothrow_julia_global(fl_context_t *fl_ctx, value_t *args, uint
218216
decode_restriction_value(pku) : jl_atomic_load_relaxed(&b->value)) != NULL ? fl_ctx->T : fl_ctx->F;
219217
}
220218

221-
static htable_t *rebuild_counter_table(jl_module_t *m) JL_NOTSAFEPOINT
222-
{
223-
htable_t *counter_table = m->counter_table = htable_new((htable_t *)malloc_s(sizeof(htable_t)), 0);
224-
jl_svec_t *t = jl_atomic_load_relaxed(&m->bindings);
225-
for (size_t i = 0; i < jl_svec_len(t); i++) {
226-
jl_binding_t *b = (jl_binding_t*)jl_svecref(t, i);
227-
if ((void*)b == jl_nothing) {
228-
continue;
229-
}
230-
char *globalref_name = jl_symbol_name(b->globalref->name);
231-
if (is_canonicalized_anonfn_typename(globalref_name)) {
232-
int should_free = 1;
233-
// copy globalref_name into the buffer until we hit a `#` character
234-
char *delim = strchr(&globalref_name[1], '#');
235-
assert(delim != NULL);
236-
size_t len = delim - globalref_name - 1;
237-
assert(len > 0);
238-
char *enclosing_function_name = (char*)calloc_s(len + 1);
239-
memcpy(enclosing_function_name, &globalref_name[1], len);
240-
// check if the enclosing function name is already in the counter table
241-
if (strhash_get(counter_table, enclosing_function_name) == HT_NOTFOUND) {
242-
strhash_put(counter_table, enclosing_function_name, (void*)((uintptr_t)HT_NOTFOUND + 1));
243-
should_free = 0;
244-
}
245-
char *pint = strrchr(globalref_name, '#');
246-
assert(pint != NULL);
247-
int counter = atoi(pint + 1);
248-
int max_seen_so_far = ((uint32_t)(uintptr_t)strhash_get(counter_table, enclosing_function_name) - (uintptr_t)HT_NOTFOUND - 1);
249-
if (counter >= max_seen_so_far) {
250-
strhash_put(counter_table, enclosing_function_name, (void*)((uintptr_t)counter + 1 + (uintptr_t)HT_NOTFOUND + 1));
251-
}
252-
if (should_free) {
253-
free(enclosing_function_name);
254-
}
255-
}
256-
}
257-
return counter_table;
258-
}
259-
260-
// used to generate a unique suffix for a given symbol (e.g. variable or type name)
219+
// Used to generate a unique suffix for a given symbol (e.g. variable or type name)
261220
// first argument contains a stack of method definitions seen so far by `closure-convert` in flisp.
262221
// if the top of the stack is non-NIL, we use it to augment the suffix so that it becomes
263-
// of the form $top_level_method_name##$counter, where counter is stored in a per-module
264-
// side table indexed by top-level method name.
265-
// this ensures that precompile statements are a bit more stable across different versions
222+
// of the form $top_level_method_name##$counter, where `counter` is the smallest integer
223+
// such that the resulting name is not already defined in the current module's bindings.
224+
// If the top of the stack is NIL, we simply return the current module's counter.
225+
// This ensures that precompile statements are a bit more stable across different versions
266226
// of a codebase. see #53719
267-
static value_t fl_current_module_counter(fl_context_t *fl_ctx, value_t *args, uint32_t nargs) JL_NOTSAFEPOINT
227+
static value_t fl_module_unique_name(fl_context_t *fl_ctx, value_t *args, uint32_t nargs)
268228
{
269-
argcount(fl_ctx, "current-julia-module-counter", nargs, 1);
229+
argcount(fl_ctx, "julia-module-unique-name", nargs, 1);
270230
jl_ast_context_t *ctx = jl_ast_ctx(fl_ctx);
271231
jl_module_t *m = ctx->module;
272232
assert(m != NULL);
@@ -275,26 +235,25 @@ static value_t fl_current_module_counter(fl_context_t *fl_ctx, value_t *args, ui
275235
value_t parsed_method_stack = args[0];
276236
if (parsed_method_stack != fl_ctx->NIL) {
277237
value_t bottom_stack_symbol = fl_applyn(fl_ctx, 1, symbol_value(symbol(fl_ctx, "last")), parsed_method_stack);
278-
funcname = symbol_name(fl_ctx, bottom_stack_symbol);
238+
funcname = tosymbol(fl_ctx, bottom_stack_symbol, "julia-module-unique-name")->name;
279239
}
280-
char buf[(funcname != NULL ? strlen(funcname) : 0) + 20];
240+
size_t sz = funcname != NULL ? strlen(funcname) + 32 : 32; // 32 is enough for the suffix
241+
char *buf = (char*)alloca(sz);
281242
if (funcname != NULL && strchr(funcname, '#') == NULL) {
282-
jl_mutex_lock_nogc(&m->lock);
283-
htable_t *counter_table = m->counter_table;
284-
if (counter_table == NULL) {
285-
counter_table = rebuild_counter_table(m);
286-
}
287-
// try to find the function name in the module's counter table, if it's not found, add it
288-
if (strhash_get(counter_table, funcname) == HT_NOTFOUND) {
289-
strhash_put(counter_table, funcname, (void*)((uintptr_t)HT_NOTFOUND + 1));
243+
for (int i = 0; ; i++) {
244+
snprintf(buf, sz, "%s##%d", funcname, i);
245+
jl_sym_t *sym = jl_symbol(buf);
246+
JL_LOCK(&m->lock);
247+
if (jl_get_module_binding(m, sym, 0) == NULL) { // make sure this name is not already taken
248+
jl_get_module_binding(m, sym, 1); // create the binding
249+
JL_UNLOCK(&m->lock);
250+
return symbol(fl_ctx, buf);
251+
}
252+
JL_UNLOCK(&m->lock);
290253
}
291-
uint32_t nxt = ((uint32_t)(uintptr_t)strhash_get(counter_table, funcname) - (uintptr_t)HT_NOTFOUND - 1);
292-
snprintf(buf, sizeof(buf), "%s##%d", funcname, nxt);
293-
strhash_put(counter_table, funcname, (void*)(nxt + 1 + (uintptr_t)HT_NOTFOUND + 1));
294-
jl_mutex_unlock_nogc(&m->lock);
295254
}
296255
else {
297-
snprintf(buf, sizeof(buf), "%d", jl_module_next_counter(ctx->module));
256+
snprintf(buf, sz, "%d", jl_module_next_counter(m));
298257
}
299258
return symbol(fl_ctx, buf);
300259
}
@@ -329,7 +288,7 @@ static jl_value_t *scm_to_julia_(fl_context_t *fl_ctx, value_t e, jl_module_t *m
329288
static const builtinspec_t julia_flisp_ast_ext[] = {
330289
{ "defined-julia-global", fl_defined_julia_global }, // TODO: can we kill this safepoint
331290
{ "nothrow-julia-global", fl_nothrow_julia_global },
332-
{ "current-julia-module-counter", fl_current_module_counter },
291+
{ "current-julia-module-counter", fl_module_unique_name },
333292
{ "julia-scalar?", fl_julia_scalar },
334293
{ NULL, NULL }
335294
};

src/init.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,6 @@ static NOINLINE void _finish_julia_init(JL_IMAGE_SEARCH rel, jl_ptls_t ptls, jl_
869869
jl_load(jl_core_module, "boot.jl");
870870
post_boot_hooks();
871871
}
872-
jl_main_module->counter_table = NULL;
873872

874873
if (jl_base_module == NULL) {
875874
// nthreads > 1 requires code in Base

src/julia-syntax.scm

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,11 @@
225225
(if lb (list lb ub) (list ub))
226226
(if lb (list lb '(core Any)) '())))))
227227

228-
;; check if x is of the form (method <name>) or (method (outerref <name>))
229228
(define (is-method? x)
230229
(if (and (pair? x) (eq? (car x) 'method))
231230
(let ((name (cadr x)))
232-
(if (and (pair? name) (eq? (car name) 'outerref))
233-
(let ((name (cadr name)))
231+
(if (and (pair? name) (eq? (car name) 'globalref))
232+
(let ((name (caddr name)))
234233
(if (symbol? name)
235234
#t
236235
#f))
@@ -3553,7 +3552,7 @@ f(x) = yt(x)
35533552

35543553
(define (convert-lambda lam fname interp capt-sp opaq parsed-method-stack)
35553554
(let ((body (add-box-inits-to-body
3556-
lam (cl-convert- (cadddr lam) fname lam (table) (table) #f interp opaq parsed-method-stack (table) (vinfo-to-table (car (lam:vinfo lam)))))))
3555+
lam (cl-convert (cadddr lam) fname lam (table) (table) #f interp opaq parsed-method-stack (table) (vinfo-to-table (car (lam:vinfo lam)))))))
35573556
`(lambda ,(lam:args lam)
35583557
(,(clear-capture-bits (car (lam:vinfo lam)))
35593558
()
@@ -3646,7 +3645,7 @@ f(x) = yt(x)
36463645
(equal? rhs0 '(the_exception)))
36473646
rhs0
36483647
(make-ssavalue)))
3649-
(rhs (convert-for-type-decl rhs1 (cl-convert- vt fname lam #f #f #f interp opaq parsed-method-stack (table) locals) #t lam))
3648+
(rhs (convert-for-type-decl rhs1 (cl-convert vt fname lam #f #f #f interp opaq parsed-method-stack (table) locals) #t lam))
36503649
(ex (cond (closed `(call (core setfield!)
36513650
,(if interp
36523651
`($ ,var)
@@ -3933,14 +3932,14 @@ f(x) = yt(x)
39333932
(define (map-cl-convert exprs fname lam namemap defined toplevel interp opaq parsed-method-stack (globals (table)) (locals (table)))
39343933
(if toplevel
39353934
(map (lambda (x)
3936-
(let ((tl (lift-toplevel (cl-convert- x fname lam namemap defined
3935+
(let ((tl (lift-toplevel (cl-convert x fname lam namemap defined
39373936
(and toplevel (toplevel-preserving? x))
39383937
interp opaq parsed-method-stack globals locals))))
39393938
(if (null? (cdr tl))
39403939
(car tl)
39413940
`(block ,@(cdr tl) ,(car tl)))))
39423941
exprs)
3943-
(map (lambda (x) (cl-convert- x fname lam namemap defined #f interp opaq parsed-method-stack globals locals)) exprs)))
3942+
(map (lambda (x) (cl-convert x fname lam namemap defined #f interp opaq parsed-method-stack globals locals)) exprs)))
39443943

39453944
(define (prepare-lambda! lam)
39463945
;; mark all non-arguments as assigned, since locals that are never assigned
@@ -3955,7 +3954,7 @@ f(x) = yt(x)
39553954
(char=? (string.char str 0) #\#)
39563955
(char-numeric? (string.char str 1))))
39573956

3958-
(define (cl-convert-- e fname lam namemap defined toplevel interp opaq parsed-method-stack (globals (table)) (locals (table)))
3957+
(define (cl-convert- e fname lam namemap defined toplevel interp opaq parsed-method-stack (globals (table)) (locals (table)))
39593958
(if (and (not lam)
39603959
(not (and (pair? e) (memq (car e) '(lambda method macro opaque_closure)))))
39613960
(if (atom? e) e
@@ -3978,7 +3977,7 @@ f(x) = yt(x)
39783977
(val (if (equal? typ '(core Any))
39793978
val
39803979
`(call (core typeassert) ,val
3981-
,(cl-convert- typ fname lam namemap defined toplevel interp opaq parsed-method-stack globals locals)))))
3980+
,(cl-convert typ fname lam namemap defined toplevel interp opaq parsed-method-stack globals locals)))))
39823981
`(block
39833982
,@(if (eq? box access) '() `((= ,access ,box)))
39843983
,undefcheck
@@ -4010,7 +4009,7 @@ f(x) = yt(x)
40104009
e)
40114010
((=)
40124011
(let ((var (cadr e))
4013-
(rhs (cl-convert- (caddr e) fname lam namemap defined toplevel interp opaq parsed-method-stack globals locals)))
4012+
(rhs (cl-convert (caddr e) fname lam namemap defined toplevel interp opaq parsed-method-stack globals locals)))
40144013
(convert-assignment var rhs fname lam interp opaq parsed-method-stack globals locals)))
40154014
((local-def) ;; make new Box for local declaration of defined variable
40164015
(let ((vi (get locals (cadr e) #f)))
@@ -4100,14 +4099,14 @@ f(x) = yt(x)
41004099
((null? cvs)
41014100
`(block
41024101
,@sp-inits
4103-
(method ,(cadr e) ,(cl-convert-
4102+
(method ,(cadr e) ,(cl-convert
41044103
;; anonymous functions with keyword args generate global
41054104
;; functions that refer to the type of a local function
41064105
(rename-sig-types sig namemap)
41074106
fname lam namemap defined toplevel interp opaq parsed-method-stack globals locals)
41084107
,(let ((body (add-box-inits-to-body
41094108
lam2
4110-
(cl-convert- (cadddr lam2) 'anon lam2 (table) (table) #f interp opaq parsed-method-stack (table)
4109+
(cl-convert (cadddr lam2) 'anon lam2 (table) (table) #f interp opaq parsed-method-stack (table)
41114110
(vinfo-to-table (car (lam:vinfo lam2)))))))
41124111
`(lambda ,(cadr lam2)
41134112
(,(clear-capture-bits (car vis))
@@ -4119,7 +4118,7 @@ f(x) = yt(x)
41194118
(newlam (compact-and-renumber (linearize (car exprs)) 'none 0)))
41204119
`(toplevel-butfirst
41214120
(block ,@sp-inits
4122-
(method ,(cadr e) ,(cl-convert- sig fname lam namemap defined toplevel interp opaq parsed-method-stack globals locals)
4121+
(method ,(cadr e) ,(cl-convert sig fname lam namemap defined toplevel interp opaq parsed-method-stack globals locals)
41234122
,(julia-bq-macro newlam)))
41244123
,@top-stmts))))
41254124

@@ -4224,7 +4223,7 @@ f(x) = yt(x)
42244223
(append (map (lambda (gs tvar)
42254224
(make-assignment gs `(call (core TypeVar) ',tvar (core Any))))
42264225
closure-param-syms closure-param-names)
4227-
`((method #f ,(cl-convert- arg-defs fname lam namemap defined toplevel interp opaq parsed-method-stack globals locals)
4226+
`((method #f ,(cl-convert arg-defs fname lam namemap defined toplevel interp opaq parsed-method-stack globals locals)
42284227
,(convert-lambda lam2
42294228
(if iskw
42304229
(caddr (lam:args lam2))
@@ -4284,15 +4283,15 @@ f(x) = yt(x)
42844283
(block ,@body))))
42854284
;; remaining `::` expressions are type assertions
42864285
((|::|)
4287-
(cl-convert- `(call (core typeassert) ,@(cdr e)) fname lam namemap defined toplevel interp opaq parsed-method-stack globals locals))
4286+
(cl-convert `(call (core typeassert) ,@(cdr e)) fname lam namemap defined toplevel interp opaq parsed-method-stack globals locals))
42884287
;; remaining `decl` expressions are only type assertions if the
42894288
;; argument is global or a non-symbol.
42904289
((decl)
42914290
(cond ((and (symbol? (cadr e))
42924291
(local-in? (cadr e) lam locals))
42934292
'(null))
42944293
(else
4295-
(cl-convert-
4294+
(cl-convert
42964295
(let ((ref (binding-to-globalref (cadr e))))
42974296
(if ref
42984297
(begin
@@ -4305,21 +4304,17 @@ f(x) = yt(x)
43054304
fname lam namemap defined toplevel interp opaq parsed-method-stack globals locals))))
43064305
;; `with-static-parameters` expressions can be removed now; used only by analyze-vars
43074306
((with-static-parameters)
4308-
(cl-convert- (cadr e) fname lam namemap defined toplevel interp opaq parsed-method-stack globals locals))
4307+
(cl-convert (cadr e) fname lam namemap defined toplevel interp opaq parsed-method-stack globals locals))
43094308
(else
43104309
(cons (car e)
43114310
(map-cl-convert (cdr e) fname lam namemap defined toplevel interp opaq parsed-method-stack globals locals))))))))
43124311

4313-
;; wrapper for `cl-convert--`
4314-
(define (cl-convert- e fname lam namemap defined toplevel interp opaq parsed-method-stack (globals (table)) (locals (table)))
4312+
;; wrapper for `cl-convert-`
4313+
(define (cl-convert e fname lam namemap defined toplevel interp opaq (parsed-method-stack '()) (globals (table)) (locals (table)))
43154314
(if (is-method? e)
43164315
(let ((name (method-expr-name e)))
4317-
(cl-convert-- e fname lam namemap defined toplevel interp opaq (cons name parsed-method-stack) globals locals))
4318-
(cl-convert-- e fname lam namemap defined toplevel interp opaq parsed-method-stack globals locals)))
4319-
4320-
(define (cl-convert e fname lam namemap defined toplevel interp opaq (globals (table)) (locals (table)))
4321-
(let ((parsed-method-stack '()))
4322-
(cl-convert-- e fname lam namemap defined toplevel interp opaq parsed-method-stack globals locals)))
4316+
(cl-convert- e fname lam namemap defined toplevel interp opaq (cons name parsed-method-stack) globals locals))
4317+
(cl-convert- e fname lam namemap defined toplevel interp opaq parsed-method-stack globals locals)))
43234318

43244319
(define (closure-convert e) (cl-convert e #f #f (table) (table) #f #f #f))
43254320

src/julia.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,6 @@ typedef struct _jl_module_t {
715715
int8_t max_methods;
716716
jl_mutex_t lock;
717717
intptr_t hash;
718-
htable_t *counter_table;
719718
} jl_module_t;
720719

721720
struct _jl_globalref_t {

src/julia_internal.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -948,26 +948,29 @@ STATIC_INLINE jl_ptr_kind_union_t jl_walk_binding_inplace(jl_binding_t **bnd, jl
948948
}
949949
#endif
950950

951+
STATIC_INLINE int is10digit(char c) JL_NOTSAFEPOINT
952+
{
953+
return (c >= '0' && c <= '9');
954+
}
955+
951956
STATIC_INLINE int is_anonfn_typename(char *name)
952957
{
953958
if (name[0] != '#' || name[1] == '#')
954959
return 0;
955960
char *other = strrchr(name, '#');
956-
return other > &name[1] && isdigit(other[1]);
961+
return other > &name[1] && is10digit(other[1]);
957962
}
958963

959964
// Returns true for typenames of anounymous functions that have been canonicalized (i.e.
960965
// we mangled the name of the outermost enclosing function in their name).
961966
STATIC_INLINE int is_canonicalized_anonfn_typename(char *name) JL_NOTSAFEPOINT
962967
{
963-
if (name[0] != '#')
964-
return 0;
965968
char *delim = strchr(&name[1], '#');
966969
if (delim == NULL)
967970
return 0;
968971
if (delim[1] != '#')
969972
return 0;
970-
if (!isdigit(delim[2]))
973+
if (!is10digit(delim[2]))
971974
return 0;
972975
return 1;
973976
}

src/module.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ JL_DLLEXPORT jl_module_t *jl_new_module_(jl_sym_t *name, jl_module_t *parent, ui
6666
jl_module_public(m, name, 1);
6767
JL_GC_POP();
6868
}
69-
m->counter_table = NULL;
7069
return m;
7170
}
7271

src/staticdata.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ External links:
7474
#include <stdio.h> // printf
7575
#include <inttypes.h> // PRIxPTR
7676

77-
#include "support/strhash.h"
78-
7977
#include "julia.h"
8078
#include "julia_internal.h"
8179
#include "julia_gcext.h"

src/support/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ JCXXFLAGS += $(CXXFLAGS)
88
JCPPFLAGS += $(CPPFLAGS)
99
JLDFLAGS += $(LDFLAGS)
1010

11-
SRCS := hashing timefuncs ptrhash strhash operators utf8 ios htable bitvector \
11+
SRCS := hashing timefuncs ptrhash operators utf8 ios htable bitvector \
1212
int2str libsupportinit arraylist strtod rle
1313
ifeq ($(OS),WINNT)
1414
SRCS += asprintf strptime

src/support/strhash.c

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

0 commit comments

Comments
 (0)